📄 untcrypt.pas
字号:
unit UntCrypt;
interface
uses StrUtils, SysUtils;
function CryptStr(const s:string; ntype: integer):string;
//第一种加解密方式,ntype为0表示加密,1表示解密
function CryptStr2(const s:string; ntype:integer):string;
//第二种加解密方式,且解密时能判断是否加过密
//本系统里已用该过程,过程内容需根据收发双方都了解的算法定制,不然最终接收者(手机用户)看不懂密文
function CryptStr3(const s:string; ntype:integer):string;
//第三种加解密方式
function StrTosAsc(const s:string; nUnitLen:integer):string;
//字符串转换成ASCII码,每个字符需要转换成nUnitLen位,不够的前面补0
function sAscToStr(const s:string; nUnitLen:integer):string;
//ASCII码串转换成源字符串,每个字符的ASCII码有nUnitLen位,
implementation
function StrTosAsc(const s:string; nUnitLen:integer):string;
var
i,j,n:integer;
sTmp:string;
begin
Result := '';
for i:=1 to Length(s) do
begin
sTmp := IntToStr(Ord(s[i]));
n := Length(sTmp);
if n>nUnitLen then sTmp := RightStr(sTmp,nUnitLen)
else for j:=n+1 to nUnitLen do sTmp := '0' + sTmp;
Result := Result + sTmp;
end;
end;
function sAscToStr(const s:string; nUnitLen:integer):string;
var
sTmp,TmpStr:string;
begin
Result := '';
TmpStr := s;
sTmp := LeftStr(TmpStr, nUnitLen);
TmpStr := RightStr(TmpStr,Length(TmpStr)-Length(sTmp));
While Length(sTmp)>0 do
begin
Result := Result + Chr(byte(StrToInt(sTmp)));
sTmp := LeftStr(TmpStr, nUnitLen);
TmpStr := RightStr(TmpStr,Length(TmpStr)-Length(sTmp));
end;
end;
function CryptStr(const s:string; ntype: integer):string;
var
i: integer;
fkey: integer;
begin
result:='';
if Length(s)=0 then exit;
case ntype of
0: //setpass;
begin
randomize;
fkey := 65 + random(26);
for i:=1 to length(s) do
result := result+chr( ord(s[i]) xor i xor fkey);
result := result + char(fkey);
end;
else //getpass
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;
end;
function CryptStr2(const s:string; ntype:integer):string;
var
i: integer;
fkey: integer;
begin
// Result := s;
result:='';
if Length(s)=0 then exit;
case ntype of
0: //setpass;
begin
randomize;
fkey := 65 + random(26);
Result := char($ff);
for i:=1 to length(s) do
result := result+chr( ord(s[i]) xor i xor fkey);
result := result + char(fkey);
end;
else //getpass
begin
if (s[1]=char($ff))and(s[length(s)]>='A')and(s[length(s)]<='Z') then
begin
fkey := ord(s[length(s)]);
for i:=1 to length(s) - 2 do
result := result+chr( ord(s[i+1]) xor i xor fkey);
end else Result := s;
end;
end;
end;
function CryptStr3(const s:string; ntype:integer):string;
var
TmpStr,sTmp:string;
i,j,n:integer;
begin
Result := '';
case ntype of
0: begin //加密
for i:=1 to Length(s) do
begin
if (i mod 2 = 0) then sTmp := IntToStr(Ord(s[i])-2)
else sTmp := IntToStr(Ord(s[i])-1);
n := Length(sTmp);
if n>3 then sTmp := RightStr(sTmp,3)
else for j:=n+1 to 3 do sTmp := '0' + sTmp;
Result := Result + sTmp;
end;
end;
else begin //解密
TmpStr := s;
sTmp := LeftStr(TmpStr, 3);
TmpStr := RightStr(TmpStr,Length(TmpStr)-Length(sTmp));
i := 1;
While Length(sTmp)>0 do
begin
if (i mod 2 = 0) then Result := Result + Chr(byte(StrToInt(sTmp)+2))
else Result := Result + Chr(byte(StrToInt(sTmp)+1));
sTmp := LeftStr(TmpStr, 3);
TmpStr := RightStr(TmpStr,Length(TmpStr)-Length(sTmp));
Inc(i);
end;
end;
end;//of "case ..."
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -