I am trying to do encryption and decryption between PHP and Delphi.
My PHP code is;
<?php
error_reporting(E_ALL);
$key = "5y';syhl0ngl4st1ngdfvt5tt";
function decrypt1($string, $key){
$y = 1;
for ($x = 1;$i < strlen($string); $i++) {
$a = (ord($string[$x]) and 0x0f) ^ (ord($key[$y]) and 0x0f);
$string[$x] = chr((ord($string[$x]) and 0xf0) + $a);
$y++;
if ($y > strlen($key)) $y = 1;
}
return $string;
}
echo decrypt1(base64_decode("cWx0fw=="),$key);
?>
My Delphi is;
function Decrypt1(Str : String; Key: string): AnsiString;
var
X, Y : Integer;
A : Byte;
begin
Y := 1;
for X := 1 to Length(Str) do
begin
A := (ord(Str[X]) and $0f) xor (ord(Key[Y]) and $0f);
Str[X] := char((ord(Str[X]) and $f0) + A);
Inc(Y);
If Y > length(Key) then Y := 1;
end;
Result := Str;
end;
function Encrypt(Str : String; Key: string): String;
begin
result:= Decrypt1(str,key);
result:= EncodeBase64(result);
end;
The encryption / decryption doesn't work. When attempting to decode encoded value from Delphi in PHP, I get a load of rubbish.
I have a feeling it might be something to do with the character encoding?
TBytes. This is the single biggest misapprehension that we encounter regarding encryption. Encryption operates on binary data and not text.