delphi文本加密解密


本文整理自网络,侵删。

 //加密函数 String:需要加密的字符串.Key密钥
Function EncrypKey (Src:String; Key:String):string;
var
idx :integer;
KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
TmpSrcAsc :Integer;
Range :Integer;

begin
KeyLen:=Length(Key);
if KeyLen = 0 then key:='Think Space';
KeyPos:=0;
SrcPos:=0;
SrcAsc:=0;
Range:=256;
Randomize;
offset:=Random(Range);
dest:=format('%1.2x',[offset]);
for SrcPos := 1 to Length(Src) do
begin
SrcAsc:=(Ord(Src[SrcPos]) + offset) MOD 255;
if KeyPos < KeyLen then KeyPos:= KeyPos + 1 else KeyPos:=1;
SrcAsc:= SrcAsc xor Ord(Key[KeyPos]);
dest:=dest + format('%1.2x',[SrcAsc]);
offset:=SrcAsc;
end;
Result:=Dest;
end;

//解密函数 String:需要解密的字符串.Key密钥
Function UncrypKey (Src:String; Key:String):string;
var
idx :integer;
KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
TmpSrcAsc :Integer;
Range :Integer;
begin
KeyLen:=Length(Key);
if KeyLen = 0 then key:='Think Space';
KeyPos:=0;
SrcPos:=0;
SrcAsc:=0;
Range:=256;
offset:=StrToInt('$'+ copy(src,1,2));
SrcPos:=3;
repeat
SrcAsc:=StrToInt('$'+ copy(src,SrcPos,2));
if KeyPos < KeyLen Then KeyPos := KeyPos + 1 else KeyPos := 1;
TmpSrcAsc := SrcAsc xor Ord(Key[KeyPos]);
if TmpSrcAsc <= offset then
TmpSrcAsc := 255 + TmpSrcAsc - offset
else
TmpSrcAsc := TmpSrcAsc - offset;
dest := dest + chr(TmpSrcAsc);
offset:=srcAsc;
SrcPos:=SrcPos + 2;
until SrcPos >= Length(Src);
Result:=Dest;
end;




测试:
procedure TForm1.Button2Click(Sender: TObject);
begin
showmessage(EncrypKey('3、最大坝高<70m,边坡高度<50m,基础处理深度<20m的水库水电工程;','王));
showmessage(UncrypKey(EncrypKey('3、最大坝高<70m,边坡高度<50m,基础处理深度<20m的水库水电工程;','王),'王));
end;

相关阅读 >>

Delphi 判断图像格式bmp jpg gif pcx png psd ras sgi tiff err

Delphi tcombobox 设置默认值

Delphi 泛型数组 strsplit 字符串分割

检查是否在Delphi xe7中启用或禁用了android蓝牙

Delphi监视进程并结束进程

idhttp获取文件大小的代码

vclzip控件的简单使用

Delphi 怎么将一个文件流转换成字符串?

Delphi 获取本地计算机上的com口列表

Delphi 拦截tab按键消息

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...