Using DES Encryption


The new DBMS_OBFUSCATION_TOOLKIT allows sophisticated encryption but insists that the text to be encrypted be a multiple of 8 bytes. As suggested by an Ask Tom post, a simple wrapper allows any length text to be encrypted whilst still satisfying this requirement.

function encrypt_data(p_text varchar2, p_key varchar2) return varchar2 is
  v_text varchar2(4000);
  v_enc varchar2(4000);
begin
    v_text := rpad( p_text, (trunc(length(p_text)/8)+1)*8, chr(0));
    sys.dbms_obfuscation_toolkit.desencrypt(
           input_string => v_text,
            key_string => p_key,
            encrypted_string=>v_enc);
    return v_enc;
  end if;
end;

function decrypt_data(p_text varchar2,p_key varchar2) return varchar2 is
  v_text varchar2(4000);
begin
    sys.dbms_obfuscation_toolkit.desdecrypt(
           input_string => p_text,
            key_string => p_key,
            decrypted_string=> v_text);
    return rtrim(v_text,chr(0));
end;