oracle使用3DES加密

 

CREATE OR REPLACE PACKAGE dbc_cryptor IS
  SYSKEY VARCHAR2(16) := '0000000012345678';
  SYSIV  VARCHAR2(16) :=chr(0)||chr(0)||chr(0)||chr(0)||chr(0)||chr(0)||chr(0)||chr(0);
  FUNCTION encrypt_3des(v_str IN VARCHAR2,
                        v_key IN VARCHAR2 DEFAULT SYSKEY) RETURN RAW;
  FUNCTION decrypt_3des(v_raw IN RAW, v_key IN VARCHAR2 DEFAULT SYSKEY)
    RETURN VARCHAR2;
END;
/

CREATE OR REPLACE PACKAGE BODY dbc_cryptor IS

FUNCTION encrypt_3des(v_str IN VARCHAR2, v_key IN VARCHAR2 DEFAULT SYSKEY) RETURN RAW AS
v_str_raw RAW(2048);
BEGIN

if v_str is null then 
   RETURN NULL;
end if;

if (MOD(LENGTHB(v_str), 8) > 0) then 
   v_str_raw := UTL_RAW.CAST_TO_RAW(RPAD(v_str, (floor(LENGTHB(v_str)/8)+1)*8, chr(0))); 
else 
   v_str_raw := UTL_RAW.CAST_TO_RAW(v_str);
end if;

if v_key = SYSKEY then 
   return DBMS_OBFUSCATION_TOOLKIT.DES3Encrypt(input => v_str_raw, key => UTL_RAW.CAST_TO_RAW(v_key), which => 0, iv => NULL);
else
   return DBMS_OBFUSCATION_TOOLKIT.DES3Encrypt(input => v_str_raw, key => UTL_RAW.CAST_TO_RAW(v_key), which => 0, iv => UTL_RAW.CAST_TO_RAW(SYSIV));
end if;

END;

FUNCTION decrypt_3des(v_raw IN raw, v_key IN VARCHAR2 DEFAULT SYSKEY) RETURN VARCHAR2 AS

BEGIN

if v_raw is null or MOD(LENGTH(v_raw), 16) <> 0 then 
   RETURN NULL;
end if;

if v_key = SYSKEY then 
return UTL_RAW.CAST_TO_VARCHAR2(DBMS_OBFUSCATION_TOOLKIT.DES3Decrypt(input => v_raw, key => UTL_RAW.CAST_TO_RAW(v_key), which => 0, iv => NULL));
else
return UTL_RAW.CAST_TO_VARCHAR2(DBMS_OBFUSCATION_TOOLKIT.DES3Decrypt(input => v_raw, key => UTL_RAW.CAST_TO_RAW(v_key), which => 0, iv => UTL_RAW.CAST_TO_RAW(SYSIV)));

end if;

END;
END;
/

 

posted on 2017-11-13 17:06  阮減显  阅读(532)  评论(0编辑  收藏  举报

导航