Home > Back-end >  Help to Delphi encryption decryption algorithm written in C language
Help to Delphi encryption decryption algorithm written in C language

Time:10-03

//encryption key words: key="123"
//plain code: 1231 adcd1234
//encrypted, 8 faf8fa7ac43e90f25151f0853
The function TMainForm. EncryptString (Source, Key: string) : string;//encryption
Var
KeyLen: integer;
KeyPos: integer;
Offset: integer;
Dest: string;
SrcPos: integer;
SrcAsc: integer;
Range: integer;
The begin
KeyLen:=Length (Key);
If KeyLen=0 then
Key:='Delphi';
KeyPos:=0;
Range:=256;
Randomize.
Offset:=the random (Range);
Dest:=format (' % 1.2 x, [Offset]);
For SrcPos:=1 to Length (Source) do
The begin
SrcAsc:=(word (Source [SrcPos]) + Offset) mod. 255;

If KeyPos & lt; KeyLen then
KeyPos:=KeyPos + 1
The else
KeyPos:=1;

SrcAsc:=SrcAsc xor word (Key [KeyPos]);
The Dest: Dest=+ format (' % 1.2 x '[SrcAsc]);
Offset:=SrcAsc;
end;
Result:=Dest;
end;

The function TMainForm. UnEncryptString (Source, Key: string) : string;//decryption
Var
KeyLen: integer;
KeyPos: integer;
Offset: integer;
Dest: string;
SrcPos: integer;
SrcAsc: integer;
TmpSrcAsc: integer;
The begin
KeyLen:=Length (Key);
If KeyLen=0 then
Key:='Delphi';
KeyPos:=0;
Offset:=strtoint (' $' + copy (Source, 1, 2));
SrcPos:=3;
Repeat
SrcAsc:=strtoint (' $' + copy (Source, SrcPos, 2));
If KeyPos & lt; KeyLen then
KeyPos:=KeyPos + 1
The else
KeyPos:=1;

TmpSrcAsc:=SrcAsc xor word (Key [KeyPos]);
If TmpSrcAsc & lt;=Offset then
TmpSrcAsc:=255 + TmpSrcAsc - Offset
The else
TmpSrcAsc:=TmpSrcAsc - Offset;

The Dest: Dest=+ CRH (TmpSrcAsc);
Offset:=SrcAsc;
SrcPos:=SrcPos + 2;
Until SrcPos & gt;=Length (Source);
Result:=Dest;
end;

//I am wrong decryption algorithm written in C language? Please help to check, thank you...
Void unEncrypt (char * source, int srcLen)
{
Xdata char I=0;
Xdata char keyLen;
Xdata char keyPos;
Xdata char srcPos;
Xdata char srcAsc;
Xdata char tmpSrcAsc;
Xdata char offset.
Xdata char key []="123";
Xdata char dest [64]={0};

KeyPos=0;
KeyLen=strlen (key);
Offset=(a2x (source [0]) & lt; <(4) | a2x source [1]).

For (srcPos=2; SrcPos & lt; SrcLen; )
{
SrcAsc=(a2x (source [srcPos]) & lt; <(4) | a2x source [srcPos + 1]).
TmpSrcAsc=srcAsc ^ key [keyPos];

If (tmpSrcAsc & lt;=offset)
{
TmpSrcAsc=255 + tmpSrcAsc - offset;
}
The else
{
TmpSrcAsc=tmpSrcAsc - offset;
}

Dest [i++]=tmpSrcAsc;

Offset=srcAsc;

SrcPos +=2;
If (keyPos & lt; KeyLen)
{
KeyLen++;
}
The else
{
KeyPos=0;
}
}
}

CodePudding user response:

Sorry, forget the son function
Char a2x (char ch)
{
The switch (ch)
{
Case '1' :
return 1;
Case '2' :
Return 2;
Case '3' :
Return 3;
Case '4' :
The return of 4;
Case '5' :
Return 5;
Case '6' :
The return of 6;
Case '7' :
The return of 7;
Case '8' :
The return of 8;
Case '9' :
The return of 9;
Case 'A' :
Case 'a' :
Return 10;
Case 'B' :
Case 'b' :
The return of 11;
Case 'C' :
Case 'c' :
The return of 12;
Case 'D' :
Case 'd' :
Return 13;
Case 'E' :
Case 'e' :
The return of 14;
Case 'F' :
Case 'f' :
The return of 15;
Default:
Break;;
}
return 0;
}

CodePudding user response:

Xdata char keyPos;
Xdata char srcPos;
Don't type int?
  • Related