Simple Oracle Plsql Package for password encryption and decryption
Introduction:
The script provided demonstrates how to create a simple password encryption and decryption package in Oracle using the DBMS_OBFUSCATION_TOOLKIT
. Below is a step-by-step breakdown of each part of the code:
1. Setting Up the Environment:
Before running the package, it is recommended to connect to the database as SYSDBA and run the script ?/rdbms/admin/catobtk.sql
. This script installs the DBMS_OBFUSCATION_TOOLKIT
package necessary for encryption and decryption functionalities.
2. Creating the Table USERS_INFO
:
The table USERS_INFO
is created to store usernames and encrypted passwords.
3. Creating the Package Specification PASSWORD
:
The package specification defines two functions: encrypt
and decrypt
. These functions will handle the encryption and decryption of passwords.
CREATE OR REPLACE PACKAGE PASSWORD AS
function encrypt(i_password varchar2) return varchar2;
function decrypt(i_password varchar2) return varchar2;
END PASSWORD;
/
show errors
4. Creating the Package Body PASSWORD
:
The package body implements the actual logic for the encrypt
and decrypt
functions using Oracle's DBMS_OBFUSCATION_TOOLKIT
.
Key Points:
- The encryption key (
c_encrypt_key
) must be exactly 8 bytes long. - Input data for encryption must have a length divisible by eight, achieved using
RPAD
to pad the input string.
5. Testing the Encryption and Decryption:
The following SQL statements test the encryption and decryption functions:
select password.decrypt(app_password.encrypt('PASSWORD1')) from dual;
select password.encrypt('PSW2') from dual;
select password.decrypt(app_password.encrypt('PSW2')) from dual;
6. Inserting and Retrieving Encrypted Data:
The script demonstrates inserting an encrypted password into the USERS_INFO
table and then retrieving and decrypting it.
insert into USERS_INFO values ('redddy',( select password.encrypt('REDDY1') from dual) );
select password.decrypt((pass)) from USERS_INFO where USERNAME='redddy';