-- 1. 直接使用一条Sql获取
select utl_raw.cast_to_raw(DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT_STRING => 'Testabc中文1223')) from dual
-- 返回:2BFB3DFD34C6D014503E19430A97A013
-- 2-1 创建方法fn_MD5
CREATE OR REPLACE FUNCTION fn_MD5(
passwd IN VARCHAR2)
RETURN VARCHAR2
IS
retval varchar2(32);
BEGIN
retval := utl_raw.cast_to_raw(DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT_STRING => passwd)) ;
RETURN retval;
END;
-- 2-2 方法的使用实例:
select fn_MD5('Testabc中文1223') from dual;
-- 返回:2BFB3DFD34C6D014503E19430A97A013
-- 1. 加密
select utl_raw.cast_to_varchar2(utl_encode.base64_encode(utl_raw.cast_to_raw('加密的内容'))) from dual
-- 输出: vNPD3LXExNrI3Q==
-- 2. 解密:
select (utl_raw.cast_to_varchar2(utl_encode.base64_decode(utl_raw.cast_to_raw('vNPD3LXExNrI3Q==')))) as 结果 from dual
-- 输出: 加密的内容
-- 1-1 UrlEncode ,创建方法如下:
CREATE OR REPLACE FUNCTION url_encode(urlEncode IN VARCHAR2)
RETURN VARCHAR2 AS
BEGIN
--utl_url.escape()该方法只能在函数中调用
RETURN utl_url.escape(urlEncode, TRUE, 'utf-8');
END;
-- 1-2 UrlEncode使用实例:
SELECT url_encode('http://www.cnblogs.com/Marydon20170307?param=测试URL编码') FROM DUAL;
-- 输出: http%3A%2F%2Fwww.cnblogs.com%2FMarydon20170307%3Fparam%3D%E6%B5%8B%E8%AF%95URL%E7%BC%96%E7%A0%81
-- 2 UrlDecode ,直接使用一条Sql即可:
SELECT utl_url.unescape('待进行Url解码的字符串','utf-8') FROM DUAL;
-- 实例:
SELECT utl_url.unescape('http%3A%2F%2Fwww.cnblogs.com%2FMarydon20170307%3Fparam%3D%E6%B5%8B%E8%AF%95URL%E7%BC%96%E7%A0%81','utf-8') FROM DUAL;
-- 输出:http://www.cnblogs.com/Marydon20170307?param=测试URL编码
-- 1. 登录sys用户,执行授权,如果不想公开public,也可指定要授权的用户,如scott
-- conn / as sysdba
grant execute on sys.dbms_crypto to public;
grant execute on sys.UTL_I18N to public;
-- *** 注意 ***: 授权完成,切换自己的用户
-- 2-1. 创建加密函数 ==> Aes+Base64
CREATE OR REPLACE FUNCTION fn_EnAes64(as_ysj IN VARCHAR2,as_key In Varchar2)
RETURN VARCHAR2
IS
encrypted_raw RAW (2000); -- 储存加密后的字节
decrypted_raw RAW (2000); -- 储存解密后的字节
key_bytes_raw RAW (16); -- 储存秘钥 128比特 (16位)
encryption_type PLS_INTEGER := 4868; -- 加密方式 AES/CBC/PKCS5Padding
iv_raw RAW (16); -- 向量
temp varchar2(1000); -- 临时,AES加密并Base64后的结果
BEGIN
-- 秘钥
key_bytes_raw := UTL_I18N.STRING_TO_RAW(as_key,'AL32UTF8'); -- DBMS_CRYPTO.RANDOMBYTES (num_key_bytes);
-- 向量
iv_raw := UTL_I18N.STRING_TO_RAW('9iuj87y2hbi5wxl1','AL32UTF8'); -- DBMS_CRYPTO.RANDOMBYTES (16);
-- 加密过程
encrypted_raw := DBMS_CRYPTO.ENCRYPT
(
src => UTL_I18N.STRING_TO_RAW (as_ysj, 'AL32UTF8'),
typ => encryption_type,
key => key_bytes_raw,
iv => iv_raw
);
-- Bas64 加密
encrypted_raw := utl_encode.base64_encode(r => encrypted_raw);
-- 密文
temp := utl_raw.cast_to_varchar2(r => encrypted_raw);
-- 密文
decrypted_raw := utl_raw.cast_to_raw(c => temp);
return UTL_I18N.RAW_TO_CHAR (decrypted_raw, 'AL32UTF8');
END;
-- 2-2. 创建解密函数 ==> Aes+Base64
CREATE OR REPLACE FUNCTION fn_DeAes64(as_ysj IN VARCHAR2, as_key IN VARCHAR2)
RETURN VARCHAR2
IS
encrypted_raw RAW (2000); -- 储存加密后的字节
decrypted_raw RAW (2000); -- 储存解密后的字节
key_bytes_raw RAW (16); -- 储存秘钥 128比特 (16位)
encryption_type PLS_INTEGER := 4868; -- 加密方式 AES/CBC/PKCS5Padding
iv_raw RAW (16); -- 向量
temp varchar2(1000); -- 临时,AES加密并Base64后的结果
BEGIN
-- 秘钥
key_bytes_raw := UTL_I18N.STRING_TO_RAW( as_key,'AL32UTF8'); -- DBMS_CRYPTO.RANDOMBYTES (num_key_bytes);
-- 向量
iv_raw := UTL_I18N.STRING_TO_RAW('9iuj87y2hbi5wxl1','AL32UTF8'); -- DBMS_CRYPTO.RANDOMBYTES (16);
-- Base64 解密
decrypted_raw := utl_raw.cast_to_raw(c => as_ysj);
decrypted_raw := utl_encode.base64_decode(r => decrypted_raw);
-- Aes解密
decrypted_raw := DBMS_CRYPTO.DECRYPT
(
src => decrypted_raw,
typ => encryption_type,
key => key_bytes_raw,
iv => iv_raw
);
temp := UTL_I18N.RAW_TO_CHAR (decrypted_raw, 'AL32UTF8');
RETURN temp;
END;
select
-- 加密字符串: 1233211234567 Key: 1234567890123456
fn_EnAes64('123','1234567891234567'), -- Aes + Base64 加密
-- 解密字符串: NjOpyoGM2i3gnx+AD8JLPg== key: 1234567890123456
fn_DeAes64('NjOpyoGM2i3gnx+AD8JLPg==','1234567891234567') -- Aes+base64解密
from dual;
-- 输出结果行如下:
-- 1 NjOpyoGM2i3gnx+AD8JLPg== 123
-- 1. 登录sys用户,执行授权,如果不想公开public,也可指定要授权的用户,如scott
-- conn / as sysdba
grant execute on sys.dbms_crypto to public;
grant execute on sys.UTL_I18N to public;
-- *** 注意 ***: 授权完成,切换自己的用户
-- 2-1 加密函数
CREATE OR REPLACE FUNCTION FUN_ENCRYPTION(
V_STR VARCHAR2 ,
V_KEY VARCHAR2
)
RETURN VARCHAR2
AS
V_KEY_RAW RAW(24) ;
V_STR_RAW RAW(2000) ;
V_RETURN_STR VARCHAR2(2000) ;
V_TYPE PLS_INTEGER ;
BEGIN
V_KEY_RAW := UTL_I18N.STRING_TO_RAW(V_KEY,'UTF8') ;
V_STR_RAW := UTL_I18N.STRING_TO_RAW(V_STR,'UTF8') ;
V_TYPE := DBMS_CRYPTO.ENCRYPT_AES192+DBMS_CRYPTO.CHAIN_CBC+DBMS_CRYPTO.PAD_PKCS5 ;
V_STR_RAW := DBMS_CRYPTO.ENCRYPT(SRC => V_STR_RAW , typ => V_TYPE, key => V_KEY_RAW) ;
V_RETURN_STR := RAWTOHEX(V_STR_RAW);
RETURN V_RETURN_STR ;
END;
-- 2-2 解密函数
CREATE OR REPLACE FUNCTION FUN_DECRYPTION(
V_STR VARCHAR2 ,
V_KEY VARCHAR2
)
RETURN VARCHAR2
AS
V_KEY_RAW RAW(24) ;
V_STR_RAW RAW(2000) ;
V_RETURN_STR VARCHAR2(2000) ;
V_TYPE PLS_INTEGER ;
BEGIN
V_KEY_RAW := UTL_I18N.STRING_TO_RAW(V_KEY,'UTF8') ;
V_STR_RAW := HEXTORAW(V_STR);
V_TYPE := DBMS_CRYPTO.ENCRYPT_AES192+DBMS_CRYPTO.CHAIN_CBC+DBMS_CRYPTO.PAD_PKCS5 ;
V_STR_RAW := DBMS_CRYPTO.DECRYPT(SRC => V_STR_RAW , typ => V_TYPE, key => V_KEY_RAW ) ;
V_RETURN_STR := UTL_I18N.RAW_TO_CHAR(V_STR_RAW,'UTF8');
RETURN V_RETURN_STR ;
END;
select
-- 加密字符串:123 Key: 123456789qwertyuio098765
FUN_ENCRYPTION('123','123456789qwertyuio098765'), -- 加密
-- 解密字符串: 1CBB1B9AB9500134F1DDF030B712AA57 key: 123456789qwertyuio098765
FUN_DECRYPTION('1CBB1B9AB9500134F1DDF030B712AA57','123456789qwertyuio098765') -- 解密
from dual;
DECLARE
v_input VARCHAR2(100) := '12345678';
v_key VARCHAR2(100) := 'oracle9i';
-- ORA-28232: obfuscation 工具箱的输入长度无效(原因是加密字符串必须是8的倍数)
encrypted_str VARCHAR2(4000);
decrypted_str VARCHAR2(4000);
encrypted_raw RAW(4000);
decrypted_raw RAW(4000);
BEGIN
-- string类型加密解密
-- encrypt(string)
dbms_obfuscation_toolkit.desencrypt(input_string => v_input, key_string => v_key, encrypted_string => encrypted_str);
dbms_output.put_line('Encrypted string: ' || encrypted_str);
dbms_output.put_line('Encrypted hex value: ' || utl_raw.cast_to_raw(encrypted_str));
-- decrypt(string)
dbms_obfuscation_toolkit.desdecrypt(input_string => encrypted_str, key_string => v_key, decrypted_string => decrypted_str);
dbms_output.put_line('Decrypted String: ' || decrypted_str);
-- raw类型加密解密
-- encrypt(raw)
dbms_obfuscation_toolkit.desencrypt(input => utl_raw.cast_to_raw(v_input), key => utl_raw.cast_to_raw(v_key), encrypted_data => encrypted_raw);
dbms_output.put_line('Encrypted Raw: ' || encrypted_raw);
dbms_output.put_line('Encrypted hex value: ' || rawtohex(encrypted_raw));
-- decrypt(raw)
dbms_obfuscation_toolkit.desdecrypt(input => encrypted_raw, key => utl_raw.cast_to_raw(v_key), decrypted_data => decrypted_raw);
dbms_output.put_line('Decrypted String: ' || utl_raw.cast_to_varchar2(decrypted_raw));
END;
说明:
dbms_crypto包默认只有sysdba用户才可执行,
其他任何用户都需要sysdba进行授权 conn / as sysdba;
sys> grant execute on dbms_crypto to scott;
-- dbms_crypto包默认只有sysdba用户才可执行,其他任何用户都需要sysdba进行授权
-- sys>grant execute on dbms_crypto to scott;
-- 示例(不考虑BLOB类型的加密)
DECLARE
v_str VARCHAR2(20) := '12345678'; -- 加密的字符串
v_type PLS_INTEGER := dbms_crypto.DES_CBC_PKCS5; -- 加密类型 ,可自由搭配
v_key RAW(256);
encrypted_raw RAW(256);
decrypted_raw RAW(256);
BEGIN
-- 生成随机16位密钥 (1个byte等于两位raw)
v_key := dbms_crypto.RandomBytes(8);
dbms_output.put_line('Encrypted Key: ' || v_key);
-- 加密
encrypted_raw := dbms_crypto.Encrypt(src => utl_raw.cast_to_raw(v_str), typ => v_type, key => v_key);
dbms_output.put_line('Encrypted Raw: ' || encrypted_raw);
dbms_output.put_line('Encrypted hex value: ' || rawtohex(encrypted_raw));
-- 解密
decrypted_raw := dbms_crypto.Decrypt(src => encrypted_raw, typ => v_type, key => v_key);
dbms_output.put_line('Decrypted String: ' || utl_raw.cast_to_varchar2(decrypted_raw));
END;