View previous topic :: View next topic |
Author |
Message |
JoeCoder
Joined: 29 Oct 2005 Posts: 294
|
Posted: Wed Sep 24, 2008 9:48 pm Post subject: Comparing results with PHP's mcrypt. |
|
|
First, I'm very glad to see that D is getting a powerful encryption library such as this--much thanks.
I'm working on encrypting some data in D (Blowfish/CBC/null padding) and decrypting it in PHP. My code works about 9 times out of 10, but I get garbage every now and then. I broke my test down to the following basic cases. D1.0 and PHP 5.2x with the mcrypt extension:
Code: | import dcrypt.crypto.ciphers.Blowfish;
import dcrypt.crypto.modes.CBC;
import dcrypt.crypto.padding.NullByte;
import dcrypt.crypto.ManagedBlockCipher;
void main() {
ubyte[] key = cast(ubyte[])"somethin";
ubyte[] iv = cast(ubyte[])"\0\0\0\0\0\0\0\0";
ubyte[] plaintext = cast(ubyte[])"12345678";
ManagedBlockCipher cipher = new ManagedBlockCipher(new CBC(new Blowfish), new NullByte);
ParametersWithIV params = new ParametersWithIV(new SymmetricKey(key), iv);
cipher.init(true, params);
ubyte[] ciphertext = new ubyte[cipher.finishOutputSize(plaintext.length)];
uint amount;
// Let's do some encrypting
amount = cipher.update(plaintext, ciphertext);
cipher.finish(ciphertext[amount..ciphertext.length]);
// Show you the goods
foreach (ubyte i; ciphertext)
printf("%02x", i);
} |
Outputs: e3555bf67269c266b7dfdd542653c432
Code: | <?php
$key = 'somethin';
$iv = "\0\0\0\0\0\0\0\0";
$plaintext = '12345678';
$encrypted = mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $plaintext, MCRYPT_MODE_CBC, $iv);
for ($i=0; $i < strlen($encrypted); $i++)
print dechex(ord($encrypted[$i]));
?> |
Outputs: e3555bf67269c266
The D output is very similar to the PHP output but is twice as many bytes. I can of course trim it, but I'd rather know if anyone actually knows what's going on.
Am I missing anything obvious? Thank in advance. |
|
Back to top |
|
|
reikon
Joined: 31 Jul 2008 Posts: 7
|
Posted: Tue Jan 20, 2009 6:31 pm Post subject: |
|
|
Wow! I didn't realize dcrypt had a forum. Much apologies for the very slow response!
The additional block is added as a result of using padding. An extra block is always added when padding is used and data falls on a block boundary (that is, the data is a multiple of the block size of the cipher).
To quote Wikipedia:
Quote: |
If the input happens to fill up an entire block, another block is added to accommodate the padding; otherwise, the end of the input plaintext might be misinterpreted as padding.
|
Thanks for using dcrypt, and again, I apologize for the slow response! |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|