madCrypt and PHP - simple example needed

delphi package - madRes, madTools, madStrings, ...
Post Reply
bartmichael
Posts: 1
Joined: Fri Sep 26, 2008 11:24 pm

madCrypt and PHP - simple example needed

Post by bartmichael »

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
madshi
Site Admin
Posts: 10749
Joined: Sun Mar 21, 2004 5:25 pm

Post 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...
rheine
Posts: 1
Joined: Wed Nov 25, 2009 7:54 pm

same problem

Post by rheine »

i am having same problem , anyone can post a working example please
zkrige
Posts: 4
Joined: Thu Mar 25, 2010 4:51 pm

madcrypt/php

Post 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
madshi
Site Admin
Posts: 10749
Joined: Sun Mar 21, 2004 5:25 pm

Post 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.
zkrige
Posts: 4
Joined: Thu Mar 25, 2010 4:51 pm

Post 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
zkrige
Posts: 4
Joined: Thu Mar 25, 2010 4:51 pm

Post by zkrige »

incidentally,

ECB is seen as insecure compared to CBC
zkrige
Posts: 4
Joined: Thu Mar 25, 2010 4:51 pm

Post by zkrige »

Ok.. done a bit of digging

it seems that Turbo Power (Open Source on www.sourceforge.net)

it has a blowfish CBC cipher
Post Reply