function TransChar(AChar: Char): Integer;
begin
if AChar in ['0'..'9'] then
Result := Ord(AChar) - Ord('0')
else
Result := 10 + Ord(AChar) - Ord('A');
end;
function StrToHex(AStr: string): string;
var
I : Integer;
Tmp: string;
begin
Result := '';
For I := 1 to Length(AStr) do
begin
Result := Result + Format('%2x', [Byte(AStr)]);
end;
I := Pos(' ', Result);
While I <> 0 do
begin
Result := '0';
I := Pos(' ', Result);
end;
end;
function HexToStr(AStr: string): string;
var
I : Integer;
CharValue: Word;
begin
Result := '';
For I := 1 to Trunc(Length(Astr)/2) do
begin
Result := Result + ' ';
CharValue := TransChar(AStr[2*I-1])*16 + TransChar(AStr[2*I]);
Result := Char(CharValue);
end;
end;
function Encrypt(const S: String
Key: Word): String;
var
I : Integer;
begin
Result := S;
for I := 1 to Length(S) do
begin
Result := char(byte(S) xor (Key shr 8));
Key := (byte(Result) + Key) * C1 + C2;
if Result = Chr(0) then
Result := S;
end;
Result := StrToHex(Result);
end;
function Decrypt(const S: String
Key: Word): String;
var
I: Integer;
S1: string;
begin
S1 := HexToStr(S);
Result := S1;
for I := 1 to Length(S1) do
begin
if char(byte(S1) xor (Key shr 8)) = Chr(0) then
begin
Result := S1;
Key := (byte(Chr(0)) + Key) * C1 + C2
//保证Key的正确性
end else
begin
Result := char(byte(S1) xor (Key shr 8));
Key := (byte(S1) + Key) * C1 + C2;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit2.Text:=Encrypt(Edit1.Text,12345);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Edit3.Text:=Decrypt(Edit2.Text,12345);
end;
上面给出的算法稳定性不是很高, 有一个base64加密算法可地考虑.
虽然上面的算法加密后不是数字, 但是你可以做一个封装, 进行一下
转换就行了嘛.
如;
var
str, deststr
string;
p
pchar;
i
integer
begin
str ;='test';
str ;= Encrypt(str,1234);
p ;= pchar(str);
for i;=0 to length(str)-1 do
deststr ;= deststr + inttostr(ord(p));
const
base = 100000;// can modify
fieldlen=10;
var
a,b,c
integer;
str
string;
deststr
string;
i
integer;
begin
str ;= '';// 设str是加密后的长串数字
i;=0;
while i*fieldlen<length(str) do
begin
c ;= inttostr(copy(str, i*fieldlen+1, fieldlen));
a ;= c div base;
b ;= c mod base;
deststr ;= inttostr(a)+ inttostr(b)
end;
end;
可以想一下,是否可行, 这只是一个思路.
Key1,Key2,Key3,Key4:Byte;
begin
n:=Length(Str);
if n=0 then exit;
Key4:=Byte((Key div 1000) mod 10);
Key3:=Byte((Key div 100) mod 10);
Key2:=Byte((Key div 10) mod 10);
Key1:=Byte(Key mod 10);
for c:=Times-1 downto 0 do
begin
Str[1]:=Char((Byte(Str[1])-Byte0+Key3+10) mod 10+Byte0);
for i:=2 to n do
Str:=Char(((Byte(Str[i-1])+Byte(Str)-Byte0*2)+Key1+20) mod 10+Byte0);
Str[n]:=Char((Byte(Str[n])-Byte0+Key4+10) mod 10+Byte0);
for i:=n-1 downto 1 do
Str:=Char(((Byte(Str[i+1])+Byte(Str)-Byte0*2)+Key2+20) mod 10+Byte0);
end;
end;
procedure SeqNumDec(var Str: String
Key: Word
Times: Integer);
var
i,c,n:Integer;
Key1,Key2,Key3,Key4:Byte;
begin
n:=Length(Str);
if n=0 then exit;
Key4:=Byte((Key div 1000) mod 10);
Key3:=Byte((Key div 100) mod 10);
Key2:=Byte((Key div 10) mod 10);
Key1:=Byte(Key mod 10);
for c:=Times-1 downto 0 do
begin
for i:=1 to n-1 do
Str:=Char(((Byte(Str)-Byte0)-Key2-(Byte(Str[i+1])-Byte0)+20) mod 10+Byte0);
Str[n]:=Char((Byte(Str[n])-Byte0-Key4+10) mod 10+Byte0);
for i:=n downto 2 do
Str:=Char(((Byte(Str)-Byte0)-Key1-(Byte(Str[i-1])-Byte0)+20) mod 10+Byte0);
Str[1]:=Char((Byte(Str[1])-Byte0-Key3+10) mod 10+Byte0);
end;
end;