0

i am using the AES ALgo with 128 bit with CBC cipher mode encryption, below is the code:

$cc = 'my secret text';
$key = '3aa22e01c04c7059778c54d122b0273689fba00f4a166a66d15f7ba6a8ba8743';
$iv =  '1234567890123456';
$length = strlen($cc);

$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc','');

mcrypt_generic_init($cipher, $key, $iv);
$encrypted = base64_encode(mcrypt_generic($cipher,$cc));
mcrypt_generic_deinit($cipher);

mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,base64_decode($encrypted));
mcrypt_generic_deinit($cipher);

echo "encrypted: " . $encrypted;
echo "\n";
echo "decrypted: " . substr($decrypted, 0, $length) . "\n";

But this gives me a warning with output as :

Warning: mcrypt_generic_init(): Key size too large; supplied length: 64, max: 32 in /var/www/cipher.php on line 10 Warning: mcrypt_generic_init(): Key size too large; supplied length: 64, max: 32 in /var/www/cipher.php on line 14 encrypted: vM/XVYSjs/QApdCUEQ8bdQ== decrypted: my secret text

Now can someone guide me on why I am facing the size issue and how I can eradicate the issue of size without changing the key...

Padyster
  • 993
  • 3
  • 11
  • 21

2 Answers2

0

You are doing 128-bit encryption but your key is 64 hex characters long, which is 265 bits.

Either you need to be doing 256 bit encryption or your key needs to half the size.

James Grant
  • 217
  • 1
  • 11
0

I modify your code and it runs fine, Please, check my changes, ok

$data_to_encrypt = "2~1~000024~0910~20130723092446~T~00002000~USD~F~375019001012120~0~0~00000000000~";
$key128 = "abcdef0123456789abcdef0123456789";
$iv = "0000000000000000";

$cc = $data_to_encrypt;
$key = $key128;
$iv =  $iv;
$length = strlen($cc);

$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc','');

mcrypt_generic_init($cipher, $key, $iv);
$encrypted = base64_encode(mcrypt_generic($cipher,$cc));
mcrypt_generic_deinit($cipher);

mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,base64_decode($encrypted));
mcrypt_generic_deinit($cipher);

echo "encrypted: " . $encrypted;
echo "<br/>";
echo "length:".strlen($encrypted);
echo "<br/>";
echo "decrypted: " . substr($decrypted, 0, $length);
Aron
  • 1,142
  • 1
  • 14
  • 26