Reputation: 75
I tried to encrypt a data using 16bit key using ecb mode for rinjndael_128 cipher. the encrption was successful and i can also succesfully decrypt the encrypted data. But the problem is, the mcrypt_encrypt function returns the garbled character string. I want to see this result in hex format.
while Im using the online tool for the same data getting this hex value as result bd61ce515890e2e3fb5e404bbe886cc2
code
$key = pack('H*', "07070609070306050601070007000700");
$plaintext = "2dfb0998b2f76f35f5b08972b57cfbfc";
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_ECB);
$ciphertext_base64 = base64_encode($ciphertext);
echo "encrypted - text: ".$ciphertext . "<br>";
echo "encrypted base 64 encoded - text: ".$ciphertext_base64 . "<br>";
$ciphertext_dec = base64_decode($ciphertext_base64);
$plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,$ciphertext_dec, MCRYPT_MODE_ECB);
echo "decrypted data - text: ".$plaintext_dec . "<br>";
Result:
before encryption - text: 2dfb0998b2f76f35f5b08972b57cfbfc
encrypted - text: (MÓx‹ÓåBÖ i½4²5žNUXÃè/Óë£@ö
encrypted base 64 encoded - text: KE3TeIsa0+VC1g1pCL00sjWeTlVYw+iNgS/TEOujQPY=
decrypted data - text: 2dfb0998b2f76f35f5b08972b57cfbfc*
Upvotes: 2
Views: 814
Reputation: 96159
The block size of RIJNDAEL-128/ECB is ...well 128 bit, i.e. 16 bytes.
But your input currently is 32 bytes and therefore the output is also two blocks i.e. 32 bytes long.
Your plaintext looks like it's "hex-encoded" like the key. So, treat it like the key.
Also base64_encode() is not the same as making a "hex-string" from a byte sequence. But you can use unpack() for that.
<?php
$key = pack('H*', '07070609070306050601070007000700');
$plaintext = pack('H*', '2dfb0998b2f76f35f5b08972b57cfbfc');
$ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $plaintext, MCRYPT_MODE_ECB);
list(,$ciphertext_hex) = unpack('H*', $ciphertext);
echo $ciphertext_hex;
prints bd61ce515890e2e3fb5e404bbe886cc2
Upvotes: 1