How to make Ruby AES-256-CBC and PHP MCRYPT_RIJNDAEL_128 play well together -


I am preparing data to send from a Ruby stack to PHP stack. I am using OpenSSL :: Cipher Library on the Ruby side and 'mcrypt' library in PHP. When I encrypt in Ruby using 'aes-256-cbc' (256-bit block size), then using the MCRYPT_RIJNDAEL_128 (128-bit block size) in PHP to decrypt it. I doubt the ruby ​​code which is broken, because cipher IV is 16; I believe it should be 32:

  & gt; & Gt; Cipher = openSSL :: cipher :: cipher Enve ('AES-128-CBC') = & gt; # & Lt; OpenSSL :: Cipher :: Cipher: 0x3067c5c & gt; & Gt; & Gt; Cipher Key_lean = & gt; 16 & gt; & Gt; Cipher.V_Len = & gt; 16 & gt; & Gt; Cipher = openSSL :: cipher :: cipher Enve ('AES-256-CBC') = & gt; # & Lt; OpenSSL :: Cipher :: Cipher: 0x306de18 & gt; & Gt; & Gt; Cipher Key_lean = & gt; 32 & gt; & Gt; Cipher.V_Len = & gt; 16  

So here's my Ruby on trial, first I generate the key and iv:

  & gt; & Gt; Cipher = openSSL :: cipher :: cipher Enve ('AES-256-CBC') & gt; & Gt; Cipher Encrypt & gt; & Gt; Iv = cipher Random_iv & gt; & Gt; Iv64 = [iv]. Pack ("m"). Strip = & gt; "VCkaypm5tPmtP3TF7aWrug ==" & gt; & Gt; Key = cipher.rand_key & gt; & Gt; Key64 = [key] .pack ("m"). Strip = & gt; "RIvFgoi9xZaHS / 0Bp0J9WDRyND6Z7jrd3btiAfcQ8Y0 ="  

Then I use those keys to encrypt:

  & gt; & Gt; Plain_data = "Hello, don, this is a string." & Gt; & Gt; Cipher = openSSL :: cipher :: cipher Enve ('AES-256-CBC') & gt; & Gt; Cipher Encrypt & gt; & Gt; Cipher.ke = base64decode64 (key 64)> & gt; Cipher.iiv = base64decode 64 (iv64)> & gt; Encrypted_data = cipher.update (plain_data) & gt; & Gt; Encrypted_data & lt; & Lt; Cipher.final & gt; & Gt; Crypt64 = [encrypted_data]. Pack ("m"). Strip = & gt; "5gfc / kJcnAV2fJI0haxnLcdraIKWgtu54UoznVxf8K0 ="  

Here is PHP decryption:

  $ ruby_crypt = "5gfC / kJcnAV2fJI0haxnLcdraIKWgtu54UoznVxf8K0 ="; $ Encrypted_data = base64_decode ($ ruby_crypt); $ Key = base64_decode ("RIvFgoi9xZaHS / 0Bp0J9WDRyND6Z7jrd3btiAfcQ8Y0 ="); $ Iv = base64_decode ("vCkaypm5tPmtP3TF7aWrug =="); $ Result = mcrypt_decrypt (MCRYPT_RIJNDAEL_128, $ key, $ encrypted_data, MCRYPT_MODE_CBC, $ iv); $ Unencryption = rrtm ($ result, "\ x00 .. \ x1F"); Print "\ nUn encrypted token: \ n '$ unencrypted' \ n"; Result: Unencrypted token: 'Hi, don, this is a string.'  

I would now like to use the block size, obviously I am misunderstanding the API. help?

I do not know PHP, but reading through the related questions on the sidebar, I see. It contains a reference, indicating that in 128 MCRYPT_RIJNDAEL_128 refers to the block size of the encryption, not the key size you will see that the key size you passed between Ruby and PHP In both cases there are 256 bits. In other words, it seems to be expected behavior, and you are already using the big key.

  #! / Usr / bin / ruby ​​is required 'base64' puts ((Base64.decode64 ("RIvFgoi9xZaHS / 0Bp0J9WDRyND6Z7jrd3btiAfcQ8Y0 =") Length * 8) .to_s)  

hh


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

php - jQuery AJAX Post not working -