dbms_obfuscation_toolkit(資料加密解密)

風靈使發表於2018-10-21

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

1,DESEncrypt

dbms_obfuscation_toolkit.DESEncrypt(input_string ,key => ,encrypted_data => out );
 /*
該過程用於DES演算法時,對輸入資料進行加密,並生成加密格式的資料;
*/
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

dbms_obfuscation_toolkit.DESDecrypt(input => ,key => ,decrypted_data => );
 /*
該過程用於DES演算法時,對生成的資料進行解密,解密祕鑰必須與加密祕鑰完全一致;
*/
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

dbms_obfuscation_toolkit.DES3Encrypt(,input => ,key => ,encrypted_data => );
/*
該過程用於DES3演算法時,對輸入資料進行加密,並生成加密格式的資料.
輸入資料必須是8的倍數,祕鑰不能少於16個字元;
*/
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

dbms_obfuscation_toolkit.DESGetKey(seed => ,key => );
/*
該過程用於DES演算法時,返回祕鑰
*/
declare
key raw(100);
begin
dbms_obfuscation_toolkit.DES3GetKey(seed=>’scottsco’,key=>key );
dbms_output.put_line(key);
end;

6,DES3GetKey

dbms_obfuscation_toolkit.DES3GetKey(seed =>,key => );
/*
該過程用於DES3演算法時,返回祕鑰
*/
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;

相關文章