Oracle加密解密函数

首先以 sys 用户授权执行 DBMS_CRYPTO 包:
GRANT EXECUTE ON SYS.DBMS_CRYPTO TO YPHOA;
创建一张表,有两个字段,一个字段是要加密码数据的ID,别一个字段是加密key

加密方法:
CREATE OR REPLACE FUNCTION initbudgetEncrypt(p_key IN VARCHAR2, p_in IN VARCHAR2)
RETURN RAW IS
ENCRYPTED_RAW RAW(4000);
ENCRYPTION_TYPE PLS_INTEGER := DBMS_CRYPTO.ENCRYPT_AES128 +
DBMS_CRYPTO.CHAIN_CBC +
DBMS_CRYPTO.PAD_PKCS5;
BEGIN
ENCRYPTED_RAW := DBMS_CRYPTO.ENCRYPT(SRC => UTL_I18N.STRING_TO_RAW(p_in,'AL32UTF8'),
TYP => ENCRYPTION_TYPE,
KEY => p_key);
RETURN ENCRYPTED_RAW;
END;

解密方法:
CREATE OR REPLACE FUNCTION initbudgetTest(p_key IN VARCHAR2,
p_in IN RAW)
RETURN VARCHAR2 IS
OUTPUT_STRING VARCHAR2(4000);
DECRYPTED_RAW RAW(4000);
ENCRYPTION_TYPE PLS_INTEGER := DBMS_CRYPTO.ENCRYPT_AES128 +
DBMS_CRYPTO.CHAIN_CBC +
DBMS_CRYPTO.PAD_PKCS5;
BEGIN
DECRYPTED_RAW := DBMS_CRYPTO.DECRYPT(SRC => p_in,
TYP => ENCRYPTION_TYPE,
KEY => p_key);
OUTPUT_STRING := UTL_I18N.RAW_TO_CHAR(DECRYPTED_RAW, 'AL32UTF8');
RETURN OUTPUT_STRING;
END;

你可能感兴趣的:(Oracle)