Page 1 of 1

madCrypt and PHP - simple example needed

PostPosted: Mon Jul 27, 2009 1:22 pm
by bartmichael
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
IV: Int64;
S, Pass, Enc, Dec: string;
S := 'Some text';
IV := 456789; //a number
Pass := 'password';

Enc := S;
Encrypt(Enc, Pass, IV);
Enc := Encode(Enc); // SgNc9xZtzEFH3Gt74JHPJQ==

Dec := Decode(Enc);
Decrypt(Dec, Pass, IV);

//OK: now Dec = S

***** PHP
$S = 'Some text';
$IV = 'abcdefgh'; //?????
$Pass = 'password';

$Enc = $S;
$Enc = mcrypt_cfb(MCRYPT_BLOWFISH, $Pass, $Enc, MCRYPT_ENCRYPT, $IV); //mcrypt_cbc ???
$Enc = base64_encode($Enc); // L6IKpL6JdFJe
echo $Enc .'<br>';

$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.

PostPosted: Mon Jul 27, 2009 6:37 pm
by madshi
I'm sorry, but I've no clue about PHP, so I can't really help. Maybe someone else will chime in? Generally madCrypt should be compatible to any other blowfish encryption library, if you use the same parameters...

same problem

PostPosted: Wed Nov 25, 2009 8:17 pm
by rheine
i am having same problem , anyone can post a working example please


PostPosted: Thu Mar 25, 2010 4:54 pm
by zkrige
I have a PHP app encrypting data. I can succesfully decrypt it in JAVA using blowfish CBC.

I presume that the reason madCrypt cannot decrypt PHP blowfish data is that madCrypt has no CBC decrypt option.

If you could add a decrypt type as a paremeter, I'm sure we will be able to decrypt PHP encrypted data

PostPosted: Thu Mar 25, 2010 5:46 pm
by madshi
Oh well, I'm not really an encryption expert. It's been quite a while that I wrote madCrypt and I don't remember the exact details of the algorithm. I don't even know what CBC means exactly right now.

But anyway, madCrypt is open source. Maybe if you know a bit more about decryption than I do, you could write a patch that adds CBC support? If you find a way to make it work, please let me know.

PostPosted: Thu Mar 25, 2010 6:06 pm
by zkrige
unfortunately i'm even less of an encryption expert.

the only reason i know PHP uses CBC (instead of ECB) is that I struggled to get java decrypting my info. I only got it working when I told java to create a cipher like this

Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");

after that it worked fine...

I will have a look at it and see what I can do, but my knowledge is very limited

PostPosted: Thu Mar 25, 2010 6:13 pm
by zkrige

ECB is seen as insecure compared to CBC

PostPosted: Thu Mar 25, 2010 6:32 pm
by zkrige
Ok.. done a bit of digging

it seems that Turbo Power (Open Source on

it has a blowfish CBC cipher