madCrypt and PHP - simple example needed
Posted: Mon Jul 27, 2009 1:22 pm
Hello!
Has anyone a simple example how to:
1) encrypt a string in Delphi and decrypt it in PHP
2) encrypt a string in PHP and decrypt it in Delphi.
Below is my code - it is working, but I do not know:
1) how to convert IV between Delphi (Int64) and PHP (string)
2) what function to use in PHP to encrypt/decrypt (mcrypt_cfb, mcrypt_cbc, ...)
to make encryption compatible between Delphi and PHP.
***** DELPHI
var
IV: Int64;
S, Pass, Enc, Dec: string;
...
S := 'Some text';
IV := 456789; //a number
Pass := 'password';
//encode
Enc := S;
Encrypt(Enc, Pass, IV);
Enc := Encode(Enc); // SgNc9xZtzEFH3Gt74JHPJQ==
//decode
Dec := Decode(Enc);
Decrypt(Dec, Pass, IV);
//OK: now Dec = S
***** PHP
$S = 'Some text';
$IV = 'abcdefgh'; //?????
$Pass = 'password';
//encode
$Enc = $S;
$Enc = mcrypt_cfb(MCRYPT_BLOWFISH, $Pass, $Enc, MCRYPT_ENCRYPT, $IV); //mcrypt_cbc ???
$Enc = base64_encode($Enc); // L6IKpL6JdFJe
echo $Enc .'<br>';
//decode
$Dec = base64_decode($Enc);
$Dec = mcrypt_cfb(MCRYPT_BLOWFISH, $Pass, $Dec, MCRYPT_DECRYPT, $IV);
$Dec = trim($Dec, "\0");
echo $Dec .'<br>';
//OK: now $Dec = $S
Sorry for asking such a simple question, but I do not know PHP enough and I'm sitting half a day without a solution...
I would be VERY grateful for any suggestions.
Thanks!
Bart
Has anyone a simple example how to:
1) encrypt a string in Delphi and decrypt it in PHP
2) encrypt a string in PHP and decrypt it in Delphi.
Below is my code - it is working, but I do not know:
1) how to convert IV between Delphi (Int64) and PHP (string)
2) what function to use in PHP to encrypt/decrypt (mcrypt_cfb, mcrypt_cbc, ...)
to make encryption compatible between Delphi and PHP.
***** DELPHI
var
IV: Int64;
S, Pass, Enc, Dec: string;
...
S := 'Some text';
IV := 456789; //a number
Pass := 'password';
//encode
Enc := S;
Encrypt(Enc, Pass, IV);
Enc := Encode(Enc); // SgNc9xZtzEFH3Gt74JHPJQ==
//decode
Dec := Decode(Enc);
Decrypt(Dec, Pass, IV);
//OK: now Dec = S
***** PHP
$S = 'Some text';
$IV = 'abcdefgh'; //?????
$Pass = 'password';
//encode
$Enc = $S;
$Enc = mcrypt_cfb(MCRYPT_BLOWFISH, $Pass, $Enc, MCRYPT_ENCRYPT, $IV); //mcrypt_cbc ???
$Enc = base64_encode($Enc); // L6IKpL6JdFJe
echo $Enc .'<br>';
//decode
$Dec = base64_decode($Enc);
$Dec = mcrypt_cfb(MCRYPT_BLOWFISH, $Pass, $Dec, MCRYPT_DECRYPT, $IV);
$Dec = trim($Dec, "\0");
echo $Dec .'<br>';
//OK: now $Dec = $S
Sorry for asking such a simple question, but I do not know PHP enough and I'm sitting half a day without a solution...
I would be VERY grateful for any suggestions.
Thanks!
Bart