利用dbms_obfuscation_toolkit保護資料(轉)

zhanghjgnu發表於2008-06-12

為了保護敏感資料,oracle8i開始提供一個資料加密包:dbms_obfuscation_toolkit.利用這個包,我們可以對資料進行DES,Triple DES或者MD5加密.本文就此講解如何使用以及使用過程需要注意的問題.

1. dbms_obfuscation_toolkit簡介
dbms_obfuscation_toolkit
主要有一下幾個儲存過程:-

DESGETKEY --
產生金鑰,用於DES演算法

DES3GETKEY --
產生金鑰,用於Triple DES演算法

DESENCRYPT --
DES演算法加密資料

DESDECRYPT --
DES演算法解密資料
DES3ENCRYPT --
Triple DES演算法加密資料
DES3DECRYPT --
DES演算法解密資料
MD5 --
MD5演算法加密資料

2.準備資料表在開始前,我們先建立表users:

SYS AS SYSDBA on 2008-03-05 15:58:26 at ORCL>create table users(

2 userid varchar2(50) primary key,

3 password varchar2(64),

4 encrypted varchar2(64)

5 );

已建立表格.

SYS AS SYSDBA on 2008-03-05 16:00:20 at ORCL>insert into users values('user1','user1234',null)

已建立1個資料列.

SYS AS SYSDBA on 2008-03-05 16:00:25 at ORCL>insert into users values('user2','abcd1234',null);

已建立1個資料列.

SYS AS SYSDBA on 2008-03-05 16:00:42 at ORCL>insert into users values('user3','oracle12',null)

已建立1個資料列.

commit

3.建立包PG_ENCRYPT_DECRYPT

SYS AS SYSDBA on 2008-03-05 16:01:43 at ORCL>create or replace package pg_encrypt_decrypt is

2 ikey varchar2(8):='oracle9i';

3 function gen_raw_key(ikey in varchar2) return raw;

4 function decrypt_3key_mode(ivalue in raw,imode in pls_integer) return varchar2;

5 function encrypt_3key_mode(ivalue in varchar2,imode in pls_integer) return raw;

6 end;

7 /

已建立套裝程式.

SYS AS SYSDBA on 2008-03-05 16:29:39 at ORCL>create or replace package body pg_encrypt_decrypt

is

2 function gen_raw_key(ikey in varchar2) return raw as

3 rawkey raw(240):='';

4 begin

5 for i in 1..length(ikey) loop

6 rawkey:=rawkey||hextoraw(to_char(ascii(substr(ikey,i,1))));

7 end loop;

8 return rawkey;

9 end;

10

11 function decrypt_3key_mode(ivalue in raw,imode in pls_integer) return varchar2 as

12 vdecrypted varchar2(4000);

13 rawkey raw(240):='';

14 begin

15 rawkey:=gen_raw_key(ikey);

16 vdecrypted:=dbms_obfuscation_toolkit.des3decrypt(

17 utl_raw.cast_to_varchar2(ivalue),key_string=>rawkey,which=>imode);

18 return vdecrypted;

19 end;

20

21 function encrypt_3key_mode(ivalue in varchar2,imode in pls_integer) return raw

22 is

23 vencrypted varchar2(4000);

24 vencryptedraw raw(2048);

25 rawkey raw(240):='';

26 begin

27 rawkey:=gen_raw_key(ikey);

28 vencrypted:=dbms_obfuscation_toolkit.des3encrypt(ivalue,key_string=>rawkey,which=>imode)

;

29 vencryptedraw:=utl_raw.cast_to_raw(vencrypted);

30 return vencryptedraw;

31 end;

32 end;

33

34 /

已建立套裝程式主體.

SYS AS SYSDBA on 2008-03-05 16:29:39 at ORCL>create or replace package body pg_encrypt_decrypt

is

2 function gen_raw_key(ikey in varchar2) return raw as

3 rawkey raw(240):='';

4 begin

5 for i in 1..length(ikey) loop

6 rawkey:=rawkey||hextoraw(to_char(ascii(substr(ikey,i,1))));

7 end loop;

8 return rawkey;

9 end;

10

11 function decrypt_3key_mode(ivalue in raw,imode in pls_integer) return varchar2 as

12 vdecrypted varchar2(4000);

13 rawkey raw(240):='';

14 begin

15 rawkey:=gen_raw_key(ikey);

16 vdecrypted:=dbms_obfuscation_toolkit.des3decrypt(

17 utl_raw.cast_to_varchar2(ivalue),key_string=>rawkey,which=>imode);

18 return vdecrypted;

19 end;

20

21 function encrypt_3key_mode(ivalue in varchar2,imode in pls_integer) return raw

22 is

23 vencrypted varchar2(4000);

24 vencryptedraw raw(2048);

25 rawkey raw(240):='';

26 begin

27 rawkey:=gen_raw_key(ikey);

28 vencrypted:=dbms_obfuscation_toolkit.des3encrypt(ivalue,key_string=>rawkey,which=>imode)

;

29 vencryptedraw:=utl_raw.cast_to_raw(vencrypted);

30 return vencryptedraw;

31 end;

32 end;

33

34 /

已建立套裝程式主體.

SYS AS SYSDBA on 2008-03-05 16:29:57 at ORCL>update users set encrypted=pg_encrypt_decrypt.enc

rypt_3key_mode(password,1);

已更新3個資料列.

SYS AS SYSDBA on 2008-03-05 16:32:37 at ORCL>commit;

確認完成.

SYS AS SYSDBA on 2008-03-05 16:34:35 at ORCL>column encrypted format a20

SYS AS SYSDBA on 2008-03-05 16:34:41 at ORCL>/

USERID PASSWORD ENCRYPTED

---------- ---------- --------------------

user1 user1234 69EF3A211A0F2C32

user2 abcd1234 CF7562203F6CEDE5

user3 oracle12 65D71D7148FA001D

SYS AS SYSDBA on 2008-03-05 16:34:42 at ORCL>select userid,password,pg_encrypt_decrypt.decrypt_3key_mode(encrypted,1) decrypted from users;

USERID PASSWORD

---------- ----------

DECRYPTED

--------------------------------------------------------------------------------

user1 user1234

user1234

user2 abcd1234

abcd1234

user3 oracle12

oracle12

6.金鑰的儲存不管我們用什麼樣的加密演算法,有一點非常重要的是:金鑰的儲存.金鑰就是一把鑰匙,因為加密演算法是公開的,所以你無論如何加密,只要我知道你的金鑰,我就可以解密,那麼你的加密就沒有效果.在本文中,我們的金鑰是這樣定義的:-

iKey varchar2(8):='oracle9i';


oracle9i
就是我們的金鑰.所以,如果只是簡單地把以上程式在oracle上執行一下就使用,那麼任何有許可權登陸的人看到這個程式,就可以知道金鑰.所以簡單的做法是利用Oracle提供的WRAP把整個程式加密,用加密後的文字建立程式.這樣別人就看不到你的原始碼了.把程式儲存為source.sql,Dos命令下輸入:-

wrap iname=f_get_rowid.sql oname=f_get_rowid.plb

就可以了,然後SQL Plus執行f_get_rowid.plb
@f_get_rowid.plb.

[@more@]

create or replace package PG_ENCRYPT_DECRYPT is
iKey varchar2(8):='12345678';
function GEN_RAW_KEY ( iKey in varchar2) return raw;
function DECRYPT_3KEY_MODE(iValue in raw,iMode in pls_integer)return varchar2;
function ENCRYPT_3KEY_MODE(iValue in varchar2,iMode in pls_integer)
return raw;
end;

create or replace package body PG_ENCRYPT_DECRYPT is
function GEN_RAW_KEY ( iKey in varchar2) return raw
as
rawkey raw(240) := '';
begin
for i in 1..length(iKey) loop
rawkey := rawkey||hextoraw(to_char(ascii(substr(iKey, i, 1))));
end loop;
return rawkey;
end;
/* Creating function DECRYPT_3KEY_MODE*/
FUNCTION DECRYPT_3KEY_MODE ( iValue in raw, iMode in pls_integer)return varchar2
as
vDecrypted varchar2(4000);
rawkey raw(240) := '';
begin
rawkey := GEN_RAW_KEY(iKey);-- decrypt input string
vDecrypted := dbms_obfuscation_toolkit.des3decrypt (UTL_RAW.CAST_TO_VARCHAR2(iValue), key_string => rawkey, which => iMode);
return vDecrypted;
end;
/*Creating function ENCRYPT_3KEY_MODE*/
FUNCTION ENCRYPT_3KEY_MODE ( iValue in varchar2, iMode in pls_integer)return raw
as
vEncrypted varchar2(4000);
vEncryptedRaw Raw(2048);
rawkey raw(240) := '';
begin
rawkey := GEN_RAW_KEY(iKey);-- encrypt input string
vEncrypted := dbms_obfuscation_toolkit.des3encrypt (iValue, key_string => rawkey, which => iMode);-- convert to raw as out
vEncryptedRaw := UTL_RAW.CAST_TO_RAW(vEncrypted);
return vEncryptedRaw;
end;
end;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/11754099/viewspace-1005558/,如需轉載,請註明出處,否則將追究法律責任。

相關文章