Delphi 字符串加密解密代码


本文整理自网络,侵删。

 Delphi 字符串加密解密代码
type
TDynByteArray = array of byte;
const
SeedA = 5678; /// 常量 ,你可以修改
SeedB = 5432; /// 常量 ,你可以修改
/// 对数组加密
function Crypt(const s: TDynByteArray; Key: Word; const bEncrypt: boolean = true): TDynByteArray; overload;
var
i : integer;
begin
SetLength(Result, Length(s));
for i := Low(s) to High(s) do
begin
Result[i] := s[i] xor (key shr 8);
if bEncrypt then
Key := (Result[i] + key) * SeedA + SeedB
else
Key := (s[i] + Key) * SeedA + SeedB;
end;
end;
/// 字符串
function Crypt(const s: string; Key: Word; const bEncrypt: boolean = True): string; overload;
var
i : integer;
ps, pr : ^byte;
begin
SetLength(Result, Length(s));
ps := @s[1];
pr := @Result[1];
for i := 1 to length(s) do
begin
pr^ := ps^ xor (Key shr 8);
if bEncrypt then
Key := (pr^ + Key) * SeedA + SeedB
else
Key := (ps^ + Key) * SeedA + SeedB;
pr := pointer(integer(pr) + 1);
ps := pointer(integer(ps) + 1);
end
end;
/// 也可以对记录进行加密 ,只要把 TResultData 改成你的记录类型即可!!!!!!
function Crypt(const s: TResultData; Key: Word; const bEncrypt: boolean = True): TResultData; overload;
var
i : integer;
ps, pr : ^byte;
begin
ps := @s;
pr := @Result;
for i := 1 to SizeOf(s) do
begin
pr^ := ps^ xor (Key shr 8);
if bEncrypt then
Key := (pr^ + Key) * SeedA + SeedB
else
Key := (ps^ + Key) * SeedA + SeedB;
pr := pointer(integer(pr) + 1);
ps := pointer(integer(ps) + 1);
end;
end;
***************************
function cryptstr(const s:string; stype: dword):string;
var
i: integer;
fkey: integer;
begin
result:=’’;
case stype of
0:
begin
randomize;
fkey := random($ff);
for i:=1 to length(s) do
result := result+chr( ord(s[i]) xor i xor fkey);
result := result + char(fkey);
end;
1:
begin
fkey := ord(s[length(s)]);
for i:=1 to length(s) - 1 do
result := result+chr( ord(s[i]) xor i xor fkey);
end;
end;

相关阅读 >>

Delphi 正则表达式起步

Delphi xe 安卓 memo设置字体颜色

Delphi arp攻击代码

Delphi 利用createservice写与桌面交互的win32服务

Delphi异常信息捕捉

Delphi 让程序支持外来文件拖放的单元文件

Delphi 2009 新增单元 character isletter、isupper、islower、isdigit、isnumber

Delphi获取当前计算机所有盘符

Delphi 控制iis,检测、增加、删除虚拟目录

Delphi设置屏幕分辨率的函数

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



打赏

取消

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

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

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

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

评论

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