分类: Oracle
2007-11-09 19:30:51
oracle9.1的加密解密包
双向加密算法
dbms_obfuscation_toolkit.desencrypt
dbms_obfuscation_toolkit.desdecrypt
dbms_obfuscation_toolkit.DES3Encrypt
dbms_obfuscation_toolkit.DES3Decrypt
单向加密算法
dbms_obfuscation_toolkit.md5
具体用法参见以下函数
--MD5加密
create or replace function
encrypt_md5(p_text varchar2) return varchar2 is
begin
--添加RawToHex(UTL_RAW.CAST_TO_RAW())为得是加密后的输出数据规范些
--input_string必须要有,否则没有办法区分到底是调用那个md5
return RawToHex(UTL_RAW.CAST_TO_RAW(dbms_obfuscation_toolkit.MD5(input_string=>p_text)));
end;
--DES加密
create or replace function
encrypt_des(p_text varchar2, p_key varchar2) return varchar2 is
begin
return UTL_RAW.CAST_TO_RAW(dbms_obfuscation_toolkit.DESENCRYPT(input_string => p_text,
key_string => p_key));
end;
--DES解密
create or replace function decrypt_data(p_text varchar2,p_key varchar2) return varchar2 is
v_text varchar2(4000);
begin
dbms_obfuscation_toolkit.DESDECRYPT(
input_string => UTL_RAW.CAST_TO_varchar2(p_text),
key_string => p_key,
decrypted_string=> v_text);
return v_text;
end;
附录oracle9.2文档自带的DES3加密解密实例:
DECLARE
input_string VARCHAR2(16) := 'tigertigertigert';
raw_input RAW(128) := UTL_RAW.CAST_TO_RAW(input_string);
key_string VARCHAR2(16) := 'scottscottscotts';
raw_key RAW(128) := UTL_RAW.CAST_TO_RAW(key_string);
encrypted_raw RAW(2048);
encrypted_string VARCHAR2(2048);
decrypted_raw RAW(2048);
decrypted_string VARCHAR2(2048);
error_in_input_buffer_length EXCEPTION;
PRAGMA EXCEPTION_INIT(error_in_input_buffer_length, -28232);
INPUT_BUFFER_LENGTH_ERR_MSG VARCHAR2(100) :=
'*** DES INPUT BUFFER NOT A MULTIPLE OF 8 BYTES - IGNORING EXCEPTION ***';
double_encrypt_not_permitted EXCEPTION;
PRAGMA EXCEPTION_INIT(double_encrypt_not_permitted, -28233);
DOUBLE_ENCRYPTION_ERR_MSG VARCHAR2(100) :=
'*** CANNOT DOUBLE ENCRYPT DATA - IGNORING EXCEPTION ***';
-- 1. Begin testing raw data encryption and decryption
BEGIN
dbms_output.put_line('> ========= BEGIN TEST RAW DATA =========');
dbms_output.put_line('> Raw input : ' ||
UTL_RAW.CAST_TO_VARCHAR2(raw_input));
BEGIN
dbms_obfuscation_toolkit.DES3Encrypt(input => raw_input,
key => raw_key, encrypted_data => encrypted_raw );
dbms_output.put_line('> encrypted hex value : ' ||
rawtohex(encrypted_raw));
dbms_obfuscation_toolkit.DES3Decrypt(input => encrypted_raw,
key => raw_key, decrypted_data => decrypted_raw);
dbms_output.put_line('> Decrypted raw output : ' ||
UTL_RAW.CAST_TO_VARCHAR2(decrypted_raw));
dbms_output.put_line('> ');
if UTL_RAW.CAST_TO_VARCHAR2(raw_input) =
UTL_RAW.CAST_TO_VARCHAR2(decrypted_raw) THEN
dbms_output.put_line('> Raw DES3 Encyption and Decryption successful');
END if;
EXCEPTION
WHEN error_in_input_buffer_length THEN
dbms_output.put_line('> ' || INPUT_BUFFER_LENGTH_ERR_MSG);
END;
dbms_output.put_line('> ');
END;
-- 2. Begin testing string data encryption and decryption
dbms_output.put_line('> ========= BEGIN TEST STRING DATA =========');
BEGIN
dbms_output.put_line('> input string : '
|| input_string);
dbms_obfuscation_toolkit.DES3Encrypt(
input_string => input_string,
key_string => key_string,
encrypted_string => encrypted_string );
dbms_output.put_line('> encrypted hex value : ' ||
rawtohex(UTL_RAW.CAST_TO_RAW(encrypted_string)));
dbms_obfuscation_toolkit.DES3Decrypt(
input_string => encrypted_string,
key_string => key_string,
decrypted_string => decrypted_string );
dbms_output.put_line('> decrypted string output : ' ||
decrypted_string);
if input_string = decrypted_string THEN
dbms_output.put_line('> String DES3 Encyption and Decryption
successful');
END if;
EXCEPTION
WHEN error_in_input_buffer_length THEN
dbms_output.put_line(' ' || INPUT_BUFFER_LENGTH_ERR_MSG);
END;
dbms_output.put_line('> ');
END;
不错的冬冬.共享一下!