Dbms_crypto application example

Source: Internet
Author: User

User passwords of many applications are encrypted and stored in the database. Some use MD5 hash strings. However, because the MD5 algorithm is irreversible, only one other password can be set at a time. The following uses the 10g DBMS_CRYPTO package for encryption and decryption to solve the above problems. After testing, it supports Chinese (10.2, and the database character set is AL32UTF8), and the encryption and decryption speed is fast. Complete DBMS_CRYPTO package reference in http://blog.csdn.net/wzy0623/archive/2007/06/14/1652032.aspx

Run the DBMS_CRYPTO package with sys user authorization:

Grant execute on sys. DBMS_CRYPTO TO username;

Encryption function:

Create or replace function fn_getencval (p_key IN VARCHAR2, p_in IN VARCHAR2)
RETURN VARCHAR2
IS
Rochelle enc_val VARCHAR2 (256 );
Rochelle in_raw RAW (128): = UTL_I18N.string_to_raw (p_in );
Rochelle key_raw RAW (128): = UTL_I18N.string_to_raw (RPAD (p_key, 96, 'x '));
Rochelle mod NUMBER
: = DBMS_CRYPTO.hash_sh1 + DBMS_CRYPTO.chain_ecb + DBMS_CRYPTO.pad_zero;
BEGIN
Rochelle enc_val: =
UTL_I18N.raw_to_char (DBMS_CRYPTO.encrypt (l_in_raw, l_mod, l_key_raw ));
RETURN l_enc_val;
END;

Decryption function:

Create or replace function fn_getdecval (p_key IN VARCHAR2, p_in IN VARCHAR2)
RETURN VARCHAR2
IS
Rochelle enc_val VARCHAR2 (256 );
Rochelle in_raw RAW (128): = UTL_I18N.string_to_raw (p_in );
Rochelle key_raw RAW (128): = UTL_I18N.string_to_raw (RPAD (p_key, 96, 'x '));
Rochelle mod NUMBER
: = DBMS_CRYPTO.hash_sh1 + DBMS_CRYPTO.chain_ecb + DBMS_CRYPTO.pad_zero;
BEGIN
Rochelle enc_val: =
UTL_I18N.raw_to_char (DBMS_CRYPTO.decrypt (l_in_raw, l_mod, l_key_raw ));
RETURN l_enc_val;
END;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.