dbms_obfuscation_toolkit(數據加密解密)

該包可以加密解密應用數據;還可以生成密碼校驗和;

– 1,DESEncrypt
/*
該過程用於DES算法時,對輸入數據進行加密,並生成加密格式的數據;
*/
dbms_obfuscation_toolkit.DESEncrypt(input_string ,key => ,encrypted_data => out );

declare
encrypted_string varchar2(100);
begin
dbms_obfuscation_toolkit.DESEncrypt(input_string =>’scottsco’,key_string =>’2dabc123’,encrypted_string =>encrypted_string);
dbms_output.put_line(encrypted_string);
end;

– 2,DESDecrypt
/*
該過程用於DES算法時,對生成的數據進行解密,解密祕鑰必須與加密祕鑰完全一致;
*/

dbms_obfuscation_toolkit.DESDecrypt(input => ,key => ,decrypted_data => );

declare
encrypted_string varchar2(100);
decrypted_string varchar2(100);
begin
dbms_obfuscation_toolkit.DESEncrypt(
input_string =>’SCOTTsco’,
key_string =>’1234abcd’,
encrypted_string =>encrypted_string );
dbms_obfuscation_toolkit.DESDecrypt(
input_string =>encrypted_string,
key_string =>’1234abcd’,
decrypted_string =>decrypted_string);
dbms_output.put_line(decrypted_string);
end;

– 3,DES3Encrypt
/*
該過程用於DES3算法時,對輸入數據進行加密,並生成加密格式的數據.
輸入數據必須是8的倍數,祕鑰不能少於16個字符;
*/
dbms_obfuscation_toolkit.DES3Encrypt(,input => ,key => ,encrypted_data => );

declare
input varchar2(8):= ‘中國你好’;
key varchar2(16):= ‘1234567890abcdef’;
str varchar2(100);
begin
dbms_obfuscation_toolkit.DES3Encrypt(
input_string => input,
key_string =>key,
encrypted_string =>str);
dbms_output.put_line(str);
end;

– 4,DES3Dncrypt
/*
該過程用於DES3算法時,對生成的數據進行解密,解密祕鑰必須與加密祕鑰完全一致;
*/

declare
input varchar2(8):= ‘中國你好’;
key varchar2(16):= ‘1234567890abcdef’;
str_in varchar2(100);
str_out varchar2(100);
begin
dbms_obfuscation_toolkit.DES3Encrypt(
input_string =>input,
key_string =>key,
encrypted_string =>str_in );
dbms_obfuscation_toolkit.DES3Decrypt(
input_string =>str_in,
key_string =>key,
decrypted_string =>str_out);
dbms_output.put_line(str_out);
end;

– 5,DESGetKey
/*
該過程用於DES算法時,返回祕鑰
*/

dbms_obfuscation_toolkit.DESGetKey(seed => ,key => );

declare
key raw(100);
begin
dbms_obfuscation_toolkit.DES3GetKey(seed=>’scottsco’,key=>key );
dbms_output.put_line(key);
end;

**-- 6,DES3GetKey**

/*
該過程用於DES3算法時,返回祕鑰
*/

dbms_obfuscation_toolkit.DES3GetKey(seed =>,key => );

declare
input raw:= ‘中國你好’;
key raw:= ‘1234567890abcdef’;
begin
dbms_obfuscation_toolkit.DES3GetKey(seed=>input,key=>key );
dbms_output.put_line(key);
end;

–7,md5
/*
使用該過程生成密碼校驗和,防止黑客連環破解加密數據;
*/
dbms_obfuscation_toolkit.MD5(input => ,checksum => );

 declare
   input varchar2(8):= '中國你好';
   key varchar2(16):= '1234567890abcdef';
   str varchar2(100);
   str1 varchar2(100);
 begin
   dbms_obfuscation_toolkit.DESEncrypt(input_string=>input, key_string=>key,
   encrypted_string =>str );
   dbms_output.put_line('加密結果爲:' || str);
   dbms_obfuscation_toolkit.MD5(input_string=>input ,checksum_string =>str1 );
   dbms_output.put_line('校驗和結果爲:' || str1);
 end;

/*
dbms_obfscation_toolkit案例
*/

–建立過程send_message

create or replace procedure send_message(message varchar2)
is
flag int ;
key varchar2(16):= ‘1234567899874561’;
checksum varchar2(100) ;
encrypted_string varchar2(100);
begin
dbms_obfuscation_toolkit.DESEncrypt(
input_string =>message,
key_string =>key,
encrypted_string =>encrypted_string );
flag := dbms_pipe.create_pipe(‘encrypted_string’) ;
if flag =0 then
dbms_pipe.pack_message(encrypted_string);
flag := dbms_pipe.send_message(‘encrypted_string’);
end if;

dbms_obfuscation_toolkit.MD5(input_string =>message,checksum_string => checksum );
flag := dbms_pipe.create_pipe(‘checksum’) ;
if flag =0 then
dbms_pipe.pack_message(checksum);
flag := dbms_pipe.send_message(‘checksum’);
end if;
end;

–建立過程recieve_message
create or replace procedure recieve_message
is
flag int;
key varchar2(16):= ‘1234567899874561’;
encrypted varchar2(100);
decrypted varchar2(100);
sourse_checksum varchar2(100);
dest_checksum varchar2(100);
begin
–接受加密消息
flag:= dbms_pipe.receive_message(‘encrypted_string’);
if flag = 0 then
dbms_pipe.unpack_message(encrypted);
flag:= dbms_pipe.remove_pipe(‘encrypted_string’);
end if ;
–接受校驗和
flag:= dbms_pipe.receive_message(‘checksum’);
if flag = 0 then
dbms_pipe.unpack_message(sourse_checksum);
flag:= dbms_pipe.remove_pipe(‘checksum’);
end if ;
–解壓
dbms_obfuscation_toolkit.DESDecrypt(
input_string =>encrypted ,
key_string =>key ,
decrypted_string => decrypted);
–生成校驗和
dbms_obfuscation_toolkit.MD5(input_string =>decrypted ,checksum_string =>dest_checksum );
–判斷兩種校驗和是否一致
if trim(dest_checksum) = trim(sourse_checksum) then
dbms_output.put_line(decrypted);
else
dbms_output.put_line(‘數據被破壞……’);
end if;
end;

–調用這個過程
declare
begin
send_message(‘中國你好中國萬歲’);
recieve_message;
end;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章