Part II: How to make Ruby AES-256-CBC and PHP MCRYPT_RIJNDAEL_128 play well together -
This question is the continuation of my previous one, in this regard I have to work now, but I still go to the other direction I'm struggling for. PHP Generate cryptograph contains all the information that was provided, but I could get the ruby code decrypt without any errors.
Here's the PHP code I'm using to generate cryptogs:
$ cleartext = "Who's a smart boy?"; $ Key = base64_decode ("6sEwMG / aKdBk5Fa2rR6vVw == \ n"); $ Iv = base64_decode ("vCkaypm5tPmtP3TF7aWrug =="); $ Cryptogram = encrypt_encript (MCRYPT_RIJNDAEL_128, $ key, $ clarext, acrypttmddcc, $ iv); $ Result = base64_encode ($ cryptogram); Print "\ n '$ Result' \ n"; RESULT 'JM0OxMINPTnF1vwXdI3XdKI0KlVx210CvpJllFja + GM ='
Then there is an attempt to decrypt the Ruby:
& gt; & Gt; Cipher = openSSL :: cipher :: cipher Enve ('AES-128-CBC') & gt; & Gt; Cipher.key = base 64decode 64 ("6sEwMG / aKdBk5Fa2rR6vVw == \ n") & gt; & Gt; Cipher.iiv = base64decload 64 ("vCkaypm5tPmtP3TF7aWrug ==") & gt; & Gt; Cryptogram = Base 64Decode 64 ('JM0xMINPTNF1vwXdI3XdKI0KlVx210CvpJllFja + GM =') & gt; & Gt; Clarectx = cipher.update (cryptogram) = & gt; "Who is clever" & gt; & Gt; Clairetext & Lt; & Lt; Cipher: FINAL OpenSSL :: Cipher :: Cipher ERE: Bad Decrypt (IRB): 100: In 'Final' (IRB): 100
What really disappointment about this It is possible to repeat the above to get the complete clayetext of that encrypted string, but adding the crap pad to the cryptograph:
& gt; & Gt; Cleatotech = cipher.update (cryptograph + 'pad') = & gt; "Who is a clever boy? 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000" Clairetext; & Lt; Cipher Final OpenSSL :: Cipher :: Cipher Error: Wrong Decrypt From IRB: 119: From 'Last' (IRB): 119
In the case of my actual use, the quartet is structured (one JSON string, because you ask), so I feel comfortable that I can use this plan and can detect bad encrypted input without having to do cipher.final
. However, I can not tolerate this type of coup in my code, so I want to understand how to handle the Ruby code with the last block brilliantly.
The problem is that mcrypt
is not doing the last block padding, while Ruby OpenSSL binding uses the default OpenSSL padding method, which is PKCS padding. I can not really improve the details from the OpenSSL documentation:
PKCS padding works by adding padding bytes of value N to make the total length of more than one data of the block size. Padding is always added, if the data is already more than one of the block size then the block size will be equal. For example, if block size 8 and 11 bytes are encrypted, then 5 padding bytes of value 5 will be added.
You will need to manually add the appropriate padding at the end before clearing the code in PHP before encrypting it. To do so, before encrypting it with your > Pass the function. If you go to the other side (decrypt with encrypt and encryption in Ruby), you will need to strip the padding bytes after decrypting. Side note: You have to add padding, even if the cleartext is already more than one of the blockage (a complete block of padding), so when you decrypt it , Then know that the last bite last block always has added the amount of padding. Otherwise, you can not specify the difference between claytext with a single padding byte and no padding bytes, which has expired in value $ cleartext
( 16
is known as blocky) on
function pkcs5_pad ($ text, $ blocksize) {$ pad = $ blocksize - (strollon ($ text) $ blockage); $ Text return str_repeat (chr ($ pad), $ pad); }
0x01
.
Comments
Post a Comment