求字符串加密解密函数(100分)

  • 主题发起人 主题发起人 xuxiaohan
  • 开始时间 开始时间
X

xuxiaohan

Unregistered / Unconfirmed
GUEST, unregistred user!
最简单的将字符串中的每个字符提出来,在分别加上或乘以。。。同一个数值,比如a的asc码是97,用97+10就成了k,这样如字符串‘abc’就成了‘klm’,当然具体需要多复杂就看你怎么作了,也可以用一些加密算法来加密。
 
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 := s xor (key shr 8);
if bEncrypt then
Key := (Result + key) * SeedA + SeedB
else
Key := (s + 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) 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) xor i xor fkey);
end;
end;

 
加密/解密字符串

unit uEncrypt;

interface

function Decrypt(const S: AnsiString
Key: Word): AnsiString;
function Encrypt(const S: AnsiString
Key: Word): AnsiString;

implementation

const
C1 = 52845;
C2 = 22719;

function Decode(const S: AnsiString): AnsiString;
const
Map: array[Char] of Byte = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0);
var
I: LongInt;
begin
case Length(S) of
2:
begin
I := Map[S[1]] + (Map[S[2]] shl 6);
SetLength(Result, 1);
Move(I, Result[1], Length(Result))
end;
3:
begin
I := Map[S[1]] + (Map[S[2]] shl 6) + (Map[S[3]] shl 12);
SetLength(Result, 2);
Move(I, Result[1], Length(Result))
end;
4:
begin
I := Map[S[1]] + (Map[S[2]] shl 6) + (Map[S[3]] shl 12) +
(Map[S[4]] shl 18);
SetLength(Result, 3);
Move(I, Result[1], Length(Result))
end
end
end;

function PreProcess(const S: AnsiString): AnsiString;
var
SS: AnsiString;
begin
SS := S;
Result := '';
while SS <> '' do
begin
Result := Result + Decode(Copy(SS, 1, 4));
Delete(SS, 1, 4)
end
end;

function InternalDecrypt(const S: AnsiString
Key: Word): AnsiString;
var
I: Word;
Seed: Word;
begin
Result := S;
Seed := Key;
for I := 1 to Length(Result) do
begin
Result := Char(Byte(Result) xor (Seed shr 8));
Seed := (Byte(S) + Seed) * Word(C1) + Word(C2)
end
end;

function Decrypt(const S: AnsiString
Key: Word): AnsiString;
begin
Result := InternalDecrypt(PreProcess(S), Key)
end;

function Encode(const S: AnsiString): AnsiString;
const
Map: array[0..63] of Char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz0123456789+/';
var
I: LongInt;
begin
I := 0;
Move(S[1], I, Length(S));
case Length(S) of
1:
Result := Map[I mod 64] + Map[(I shr 6) mod 64];
2:
Result := Map[I mod 64] + Map[(I shr 6) mod 64] +
Map[(I shr 12) mod 64];
3:
Result := Map[I mod 64] + Map[(I shr 6) mod 64] +
Map[(I shr 12) mod 64] + Map[(I shr 18) mod 64]
end
end;

function PostProcess(const S: AnsiString): AnsiString;
var
SS: AnsiString;
begin
SS := S;
Result := '';
while SS <> '' do
begin
Result := Result + Encode(Copy(SS, 1, 3));
Delete(SS, 1, 3)
end
end;

function InternalEncrypt(const S: AnsiString
Key: Word): AnsiString;
var
I: Word;
Seed: Word;
begin
Result := S;
Seed := Key;
for I := 1 to Length(Result) do
begin
Result := Char(Byte(Result) xor (Seed shr 8));
Seed := (Byte(Result) + Seed) * Word(C1) + Word(C2)
end
end;

function Encrypt(const S: AnsiString
Key: Word): AnsiString;
begin
Result := PostProcess(InternalEncrypt(S, Key))
end;

end.

{**************************************************************}
//例子
{**************************************************************}

procedure TForm1.Button1Click(Sender: TObject);
const
my_key = 33189;
var
sEncrypted, sDecrypted :AnsiString;
begin
// 加密
sEncrypted := Encrypt('this is a sample text to encrypt...abcd 123 {}[]?=)=(',my_key);
//显示加密后字符串
ShowMessage(sEncrypted);
//解密
sDecrypted := Decrypt(sEncrypted,my_key);
// 显示解密后字符串
ShowMessage(sDecrypted);
end;
 
这类例子网上众多呀,这个你看一下吧
Function EncrypKey1 (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:='iloveqq_pp@hotmail.com';
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;
Function UncrypKey1(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:='iloveqq_pp@hotmail.com';
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;
 
kouchun 的不行,
App2001 的可以。
其他的没有验证
 
后退
顶部