Security is a top priority when handling sensitive data in databases. Oracle provides the DBMS_CRYPTO
package to help encrypt and decrypt data securely using industry-standard algorithms like AES.
In this blog, we'll walk through:
-
Encrypting plaintext using AES-128 in CBC mode with PKCS5 padding
-
Decrypting that data back to plaintext
-
A full working example in PL/SQL
🔐 Encrypting Data using
DBMS_CRYPTO
DECLARE l_key RAW(128) := UTL_RAW.cast_to_raw('1234567890999999'); -- 16 bytes key for AES128 l_data RAW(2000); l_encrypted RAW(2000); BEGIN -- Convert plain text to RAW l_data := UTL_RAW.cast_to_raw('Hello World'); -- Encrypt using AES128 + CBC + PKCS5 padding l_encrypted := DBMS_CRYPTO.ENCRYPT( src => l_data, typ => DBMS_CRYPTO.ENCRYPT_AES128 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5, key => l_key); -- Output encrypted value in HEX format DBMS_OUTPUT.put_line('Encrypted (HEX): ' || RAWTOHEX(l_encrypted)); END; /
🔓 Decrypting the Encrypted Data
Suppose the encrypted value from the above output is:
D35264E8079CCAA958116F5F2FCDD33E
Use the following block to decrypt it:
Comments
Post a Comment