Delphi <> PHP XOR Encryption -
i trying encryption , decryption between php , delphi.
my php code is;
<?php error_reporting(e_all); $key = "5y';syhl0ngl4st1ngdfvt5tt"; function decrypt1($string, $key){ $y = 1; ($x = 1;$i < strlen($string); $i++) { $a = (ord($string[$x]) , 0x0f) ^ (ord($key[$y]) , 0x0f); $string[$x] = chr((ord($string[$x]) , 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; : byte; begin y := 1; x := 1 length(str) begin := (ord(str[x]) , $0f) xor (ord(key[y]) , $0f); str[x] := char((ord(str[x]) , $f0) + a); inc(y); if y > length(key) 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 decode encoded value delphi in php, load of rubbish.
i have feeling might character encoding?
there few problems here:
- string indexing in php zero-based , not one-based code assumes.
- delphi strings (well, in modern delphi) utf-16 encoded. code assumes unspecified 8 bit encoding.
- encryption/decryption operates on binary data rather text , fail recognise this.
you should encrypt this:
- encode text in specific, well-defined encoding. instance, utf-8. gives byte array. in delphi
tbytes
. - encrypt byte array yield byte array.
- encode byte array using base64 text representation.
decryption reverses these steps. key thing absorb encryption/decryption operates on binary data rather text.
Comments
Post a Comment