Oracle functions implement simple encryption and decryption

Source: Internet
Author: User

1. Encryption Algorithm create or replace function encrypt (in_string varchar2, shift number) return varchar2 is
Lv_out_string varchar2 (100 );
Lv_in_string varchar2 (100 );
Lv_length number (5 );
Lv_count number (5 );
Lv_temp varchar2 (1 );
Lv_shift number (5 );
Begin
Lv_shift: = mod (shift, 26 );
Lv_in_string: = lower (in_string );
Select LENGTH (lv_in_string), 1, ''into lv_length, lv_count, lv_out_string from dual;

For lv_count in 1 .. lv_length loop
Lv_temp: = substr (lv_in_string, lv_count, 1 );
If ascii (lv_temp) <97 or ascii (lv_temp)> 122 then
Lv_out_string: = lv_out_string | lv_temp;
Else
If ascii (lv_temp) + lv_shift> 122 then
Lv_temp: = chr (ascii (lv_temp) + lv_shift-26 );
Else
Lv_temp: = chr (ascii (lv_temp) + lv_shift );
End if;
Lv_out_string: = lv_out_string | lv_temp;
End if;
End loop;
Return lv_out_string;
End encrypt; 2. decryption algorithm create or replace function decrypt (in_string varchar2, shift number) return varchar2
Is
Lv_out_string varchar2 (10 );
Lv_length number (3 );
Lv_count number (3 );
Lv_temp varchar2 (100 );
Lv_shift number (3 );

Begin
Lv_shift: = mod (shift, 26 );
Select length (in_string), 1, ''into lv_length, lv_count, lv_out_string from dual;
For lv_count in 1 .. lv_length loop
Lv_temp: = substr (in_string, lv_count, 1 );
If ascii (lv_temp) <97 or ascii (lv_temp)> 122 then
Lv_out_string: = lv_out_string | lv_temp;
Else
If ascii (lv_temp)-lv_shift <97 then
Lv_temp: = chr (ascii (lv_temp)-lv_shift + 26 );

Else
Lv_temp: = CHR (ASCII (lv_temp)-lv_shift );
End if;
Lv_out_string: = lv_out_string | lv_temp;
End if;
End loop;
Return lv_out_string;
End decrypt;

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.