这是使用较旧的 dbms_obfuscation_toolkit 进行的加密/解密:
create or replace function crypt(p_str in varchar2, p_key in varchar2) return varchar2
as
l_data varchar2(255);
begin
l_data := rpad(p_str, (trunc(length(p_str)/8)+1)*8,chr(0));
dbms_obfuscation_toolkit.DESEncrypt
(input_string=>l_data,
key_string=>p_key,
encrypted_string=>l_data);
return l_data;
end;
对于解密:
create or replace function decrypt(p_str in varchar2, p_key in varchar2) return varchar2
as
l_data varchar2(255);
begin
dbms_obfuscation_tookit.DESDecrypt
(input_string=>p_str,
key_string=>p_key,
decrypted_string=>l_data);
return rtrim(l_data,chr(0));
end;
和用法:
declare
l_data varchar2(100);
l_key varchar2(100);
l_encrypted varchar2(100);
l_decrypted varchar2(100);
begin
l_data := 'This is secret!!!';
l_key := 'My secret key';
dbms_output.put_line(l_data);
l_encrypted := crypt(l_data, l_key);
dbms_output.put_line(l_encrypted);
l_decrypted := decrypt(l_encrypted, l_key);
dbms_output.put_line(l_decrypted);
end;