Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delphi encryption/PHP decryption and vice versa

606 views
Skip to first unread message

Jan Doggen

unread,
Apr 11, 2008, 2:33:36 PM4/11/08
to
Has anyone gotten Delphi encryption/PHP decryption working?
I tried using Dave Barton's DCPCrypt and MCrypt with PHP, using Rijndael
encryption, but these implementations seem to differ:

const
KeySize = 32; // 256 bits
BlockSize = 16; // 128 bits

Key := 'simple24characterkey1234';
IV := 'simple16byteiv12';
ToEncrypt := 'j...@test.nl';
ToEncrypt := PadWithZeros(ToEncrypt,BlockSize); // Pads a string with #0
so that it is a multiple of BlockSize
{ Bytes of padded string to encrypt: }
{ 6a 61 6e 40 74 65 73 74 2e 6e 6c 00 00 00 00 00 }
Cipher := TDCP_rijndael.Create(Self);
Cipher.Init(Key[1],8*KeySize,@IV[1]);
Cipher.EncryptCBC(ToEncrypt[1],ToEncrypt[1],Length(ToEncrypt));
Cipher.Free;
FillChar(Key[1],KeySize,0);
{ Bytes of encrypted string: }
{ 18 e6 be 0e aa 36 6f a4 b7 9d 93 c8 08 c6 0f 56 }

If I try to decrypt this with MCrypt in PHP I get gibberish.
So I tried to encrypt with MCrypt to compare the two:

$rkey = 'simple24characterkey1234';
$riv = 'simple16byteiv12';
$data = 'j...@test.nl';
$data = mcrypt_cbc(MCRYPT_RIJNDAEL_128,$rkey,$data,MCRYPT_ENCRYPT,$riv);
Now the bytes of the encrypted string are:
f8 a1 0d 75 58 2a 26 de f5 6b 84 bd 3a 45 b2 5e

So this does not exactly match ;-(

Help, what can I use that works?

Thanks
Jan


========================================================================================
http://www.jandoggen.org http://www.beautyofpeople.com
http://www.puddingclub.nl


Henrick Hellström

unread,
Apr 11, 2008, 7:24:11 PM4/11/08
to
Jan Doggen wrote:
> $rkey = 'simple24characterkey1234';
> $riv = 'simple16byteiv12';
> $data = 'j...@test.nl';
> $data = mcrypt_cbc(MCRYPT_RIJNDAEL_128,$rkey,$data,MCRYPT_ENCRYPT,$riv);
> Now the bytes of the encrypted string are:
> f8 a1 0d 75 58 2a 26 de f5 6b 84 bd 3a 45 b2 5e

Using StreamSec Tools 2.1 (which is equivalent to OpenStrSecII on
sourceforge.net in case you want an open source library), the following
code results yields the same output as the mcrypt PHP library:

uses
SecUtils, SsRijndael;

var
lKey: AnsiString;
lC: TCipher;
lS: AnsiString;
begin
lKey := 'simple24characterkey1234';
lS := 'j...@test.nl'#0#0#0#0#0;
UniqueString(lS);
lC := TRijndael_CBC.Create(Pointer(lKey)^,Length(lKey),0);
try
lC.IVector := 'simple16byteiv12';
lC.Encrypt(Pointer(lS)^,Length(lS));
finally
lC.Free;
end;
ShowMessage(BinToHex(Pointer(lS)^,Length(lS)));

Jan Doggen

unread,
May 10, 2008, 10:38:09 AM5/10/08
to
I got it to work.
The Delphi sample in my original post comes from Dave Bartons website, but I
modified it a little and that was where the error is. His original code
works fine.

Jan

"Henrick Hellström" <hen...@streamsec.se> schreef in bericht
news:47ff...@newsgroups.borland.com...

0 new messages