oracle AES CBC,128位密钥加解密方法
方法脚本
前提:需要dbms_crypto包
-- 加密
CREATE OR REPLACE FUNCTION aes_cbc_encrypt(
p_input VARCHAR2,--原文
p_key VARCHAR2,--密钥
p_iv VARCHAR2--偏移量
) RETURN VARCHAR2 IS
encrypted_raw varchar2(2000);
BEGIN
encrypted_raw := DBMS_CRYPTO.Encrypt(
src => utl_raw.cast_to_raw(CONVERT(p_input,'AL32UTF8')),
typ => DBMS_CRYPTO.ENCRYPT_AES128 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5,
key => utl_raw.cast_to_raw(p_key),
iv => utl_raw.cast_to_raw(p_iv)
);
DBMS_OUTPUT.PUT_LINE(encrypted_raw);
RETURN UTL_RAW.CAST_TO_VARCHAR2(UTL_ENCODE.base64_encode(encrypted_raw));
END;
--解密
CREATE OR REPLACE FUNCTION aes_cbc_decrypt(
p_input VARCHAR2,--密文
p_key VARCHAR2,--密钥
p_iv VARCHAR2--偏移量
) RETURN VARCHAR2 IS
decrypted_raw VARCHAR2(2000);
BEGIN
decrypted_raw := DBMS_CRYPTO.DECRYPT(
src => UTL_ENCODE.BASE64_DECODE(utl_raw.cast_to_raw(p_input)),
typ => DBMS_CRYPTO.ENCRYPT_AES128 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5,
key => utl_raw.cast_to_raw(p_key),
iv => utl_raw.cast_to_raw(p_iv)
);
DBMS_OUTPUT.PUT_LINE(decrypted_raw);
RETURN UTL_I18N.RAW_TO_CHAR (decrypted_raw, 'AL32UTF8');
END;
效果
- 加密
- 解密