function XorEncode(Source, Key: string): string;
var
I : Integer;
C : Byte;
begin
Result := '';
for I := 1 to Length(Source) do begin
if Length(Key) > 0 then
C := Byte(Key[1 + ((I - 1) mod Length(Key))]) xor Byte(Source[I])
else
C := Byte(Source[I]);
Result := Result + AnsiLowerCase(IntToHex(C, 2));
end;
end;
function XorDecode(Source, Key: string): string;
var
I: Integer;
C: Char;
begin
Result := '';
for I := 0 to Length(Source) div 2 - 1 do begin
C := Chr(StrToIntDef('$' + Copy(Source, (I * 2) + 1, 2), Ord(' ')));
if Length(Key) > 0 then
C := Chr(Byte(Key[1 + (I mod Length(Key))]) xor Byte(C));
Result := Result + C;
end;
end;
Beispiele :
// Crypt
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Text := XorEncode(Memo1.Text, '123')
end;
// Decrypt
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text := XorDecode(Memo1.Text, '123')
end;
Keine Kommentare:
Kommentar veröffentlichen