madCrypt and PHP - simple example needed

delphi package - madRes, madTools, madStrings, ...

madCrypt and PHP - simple example needed

Postby bartmichael » 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
bartmichael
 
Posts: 1
Joined: Fri Sep 26, 2008 11:24 pm

Postby madshi » Mon Jul 27, 2009 6:37 pm

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

same problem

Postby rheine » Wed Nov 25, 2009 8:17 pm

i am having same problem , anyone can post a working example please
rheine
 
Posts: 1
Joined: Wed Nov 25, 2009 7:54 pm

madcrypt/php

Postby zkrige » Thu Mar 25, 2010 4:54 pm

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

Postby madshi » Thu Mar 25, 2010 5:46 pm

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

Postby zkrige » Thu Mar 25, 2010 6:06 pm

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

Postby zkrige » Thu Mar 25, 2010 6:13 pm

incidentally,

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

Postby zkrige » Thu Mar 25, 2010 6:32 pm

Ok.. done a bit of digging

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

it has a blowfish CBC cipher
zkrige
 
Posts: 4
Joined: Thu Mar 25, 2010 4:51 pm


Return to madBasic

Who is online

Users browsing this forum: No registered users and 2 guests