Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pycrypto aes 256 Initialization vector size

here's the case i have a php script that's using aes256 ,CBC the both key and IV size are 32 bytes long

data= '123456789abcdef' 
from Crypto.Cipher import AES
a = AES.new('oqufXQ(?bc=6_hR2I3sMZChDpb6dDlw4',2,'fOaiIOkD8*9Xeu_s4_bb87Ox_UG+D9GA')
print a.encrypt(data)

and the error i got

<type 'exceptions.ValueError'>: IV must be 16 bytes long
Traceback (most recent call last):
  File "/base/data/home/apps/s~xxxxxxx/1.155074369696961822/main.py", line 4, in <module>

php code that works

 echo base64_encode(encrypt('0123456789abcdef'))  ;


 function encrypt($data)
  {
    return mcrypt_encrypt(MCRYPT_RIJNDAEL_256 ,'oqufXQ(?bc=6_hR2I3sMZChDpb6dDlw4', $data , MCRYPT_MODE_CBC, utf8_encode('fOaiIOkD8*9Xeu_s4_bb87Ox_UG+D9GA') );
  }

I cant change the IV size

Note that I am Not very Familiar With Python, Just Need a way to encrypt data as it will be one appengine .

like image 840
user80287 Avatar asked Dec 22 '22 05:12

user80287


1 Answers

Aha!

There's a difference of opinion what the "256" refers to.

AES has a fixed block size of 128 bits, so "AES 256" means 128 bit blocks, 256 bit key, 14 rounds.

However, Rijndael allows both the key size and the block size to vary. MCRYPT_RIJNDAEL_256 refers to Rijndael with block size set to 256 (and I don't know how many rounds). So it does indeed take a 32 byte IV. Your PHP script is not using AES 256.

This is confirmed in https://bugs.php.net/bug.php?id=47125 -- the reporter considers it a bug in PHP mcrypt, PHP considers it a bug in libmcrypt, but it's not a bug, since libmcrypt does document what MCRYPT_RIJNDAEL_256 means (at least the linux man page for mcrypt does, my Google-fu has failed to find any actual documentation for libmcrypt). That thing happens not to be the same as what AES 256 means.

So, you're encrypting and decrypting with ciphers that although related, might as well be completely different.

The bad news is that there doesn't appear to be a Crypto.Cipher.RIJNDAEL in PyCrypto. If you can pass a 256-bit key to MCRYPT_RIJNDAEL_128 in the PHP script, then that would be AES 256 (thanks Paŭlo).

like image 144
Steve Jessop Avatar answered Dec 31 '22 15:12

Steve Jessop