Oracle uses stored procedures to encrypt a piece of string (3DES algorithm)

Source: Internet
Author: User

Create or Replace package Pg_encrypt_decrypt
Is
Ikey VARCHAR2 (8): = ' oracle9i ';
function Gen_raw_key (Ikey in varchar2) return RAW;
function Decrypt_3key_mode (ivalue in Raw,imode on Pls_integer) return varchar2;
function Encrypt_3key_mode (ivalue in Varchar2,imode on Pls_integer) return raw;
function Formatstr (ivalue in varchar2) return varchar2;
function FormatStr2 (ivalue in varchar2) return varchar2;
End
/////////
Create or replace package body Pg_encrypt_decrypt
Is
function Gen_raw_key (Ikey in VARCHAR2)
Return RAW
As
Rawkey Raw (240): = ';
Begin
For I in 1..length (Ikey) loop
Rawkey: = rawkey| | Hextoraw (To_char (ASCII (substr (Ikey, I, 1)));
End Loop;
return rawkey;
End Gen_raw_key;
/* Creating function decrypt_3key_mode*/
FUNCTION Decrypt_3key_mode (Ivalue in Raw, imode in Pls_integer) return VARCHAR2
As
vdecrypted varchar2 (4000);
Rawkey Raw (240): = ';
Begin
Rawkey: = Gen_raw_key (Ikey);--Decrypt input string
vdecrypted: = Dbms_obfuscation_toolkit.des3decrypt (Utl_raw. CAST_TO_VARCHAR2 (ivalue), key_string => Rawkey, which =>);
Return FormatStr2 (vdecrypted);
End Decrypt_3key_mode;
/*creating function encrypt_3key_mode*/
FUNCTION Encrypt_3key_mode (Ivalue in Varchar2, Imode in Pls_integer) return raw
As
vencrypted varchar2 (4000);
Vencryptedraw Raw (2048);
Rawkey Raw (240): = ';
Begin
Rawkey: = Gen_raw_key (Ikey);--Encrypt input string
vencrypted: = Dbms_obfuscation_toolkit.des3encrypt (Formatstr (ivalue), key_string => Rawkey, which =>);
--Convert to Raw as out
Vencryptedraw: = Utl_raw. Cast_to_raw (vencrypted);
return vencryptedraw;
End Encrypt_3key_mode;

Related Article

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.