Data encryption is the one of the key feature for security. It can be used for several purposes. I used DBMS_CRYPTO package for this. DBMS_CRYPTO.ENCRYPT function requires the input to be RAW datatype. Also, it requires a specific character set which is AL32UTF8. UTL_I18N.STRING_TO_RAW is used instead of UTL_RAW.CAST_TO_RAW. Because, you can also format the character set while casting.
DECLARE
V_SECRETDATA VARCHAR2(100) := 'This is secret message';
V_ENCRYPTED VARCHAR2(500);
V_DECRYPTED VARCHAR2(500);
FUNCTION ENCRYPT_DATA(P_DATA VARCHAR2, P_KEY VARCHAR2 DEFAULT '1234567890123456')
RETURN VARCHAR2
IS
V_ENCRYPTED_RAW RAW(2048);
V_ENCRYPTION_TYPE PLS_INTEGER := DBMS_CRYPTO.ENCRYPT_AES128 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5;
BEGIN
V_ENCRYPTED_RAW := DBMS_CRYPTO.ENCRYPT(SRC => UTL_I18N.STRING_TO_RAW(P_DATA, 'AL32UTF8'),
KEY => UTL_I18N.STRING_TO_RAW(P_KEY, 'AL32UTF8'),
TYP => V_ENCRYPTION_TYPE);
RETURN RAWTOHEX(V_ENCRYPTED_RAW);
END;
FUNCTION DECRYPT_DATA(P_DATA RAW, P_KEY VARCHAR2 DEFAULT '1234567890123456')
RETURN VARCHAR2
IS
V_DECRYPTED_RAW RAW(2048);
V_ENCRYPTION_TYPE PLS_INTEGER := DBMS_CRYPTO.ENCRYPT_AES128 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5;
BEGIN
V_DECRYPTED_RAW := DBMS_CRYPTO.DECRYPT(SRC => HEXTORAW(P_DATA),
KEY => UTL_I18N.STRING_TO_RAW(P_KEY, 'AL32UTF8'),
TYP => V_ENCRYPTION_TYPE
);
RETURN UTL_I18N.RAW_TO_CHAR(V_DECRYPTED_RAW);
END;
BEGIN
DBMS_OUTPUT.PUT_LINE('TEXT: '||V_SECRETDATA);
V_ENCRYPTED := ENCRYPT_DATA(P_DATA => V_SECRETDATA);
DBMS_OUTPUT.PUT_LINE('ENCRYPTED DATA: '||V_ENCRYPTED);
V_DECRYPTED :=DECRYPT_DATA(P_DATA => V_ENCRYPTED);
DBMS_OUTPUT.PUT_LINE('DECREYPTED DATA: '||V_DECRYPTED);
END;
Output is showing below
TEXT: This is secret message
ENCRYPTED DATA: 0074D6E08B0E80F030C33D2A707032B2494929F674AE0BF054BCAB939E7330E8
DECREYPTED DATA: This is secret message
can you please provide a full sample of encrypting plsql procedure code using this sample anonymous block…regards