1. 加密算法
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 ;
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. 解密算法
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;
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;
本文转自 randy_shandong 51CTO博客,原文链接:http://blog.51cto.com/dba10g/270733,如需转载请自行联系原作者