Note that there are some explanatory texts on larger screens.

plurals
  1. POAES/CBC/PKCS5Padding implementation in Ruby (for rails)
    primarykey
    data
    text
    <p>I need to decrypt text encrypted using <code>AES/CBC/PKCS5Padding</code> scheme. The encrypted text I got was generated using some Java software.</p> <p>All values below are changed by me to something fictional.</p> <p>What I get is a Key <code>aHjgYFutF672eGIUGGVlgSETyM9VJj0K</code> (256-bit = 32-chars * 8-bit) and IV: <code>rxYoks3c8hRRsL2P</code> (16-bit)</p> <p>and (I supposed) Base64 encoded encrypted result <code>ETlAHS5ZcshKxQUaHVB8==</code></p> <p>What I need is to decrypt in Ruby this <code>ETlAHS5ZcshKxQUaHVB8==</code> to get in the and a simple string, like 'blablablabla'</p> <p>I tried to decrypt what I got using both Ruby and just common linux console openssl command. NOTE: Key and IV below are not the ones used in real code:</p> <pre><code># require 'openssl' # require 'base64' # decryption aes = OpenSSL::Cipher::AES256.new(:CBC) aes.decrypt aes.padding = 1 # actually it's on by default aes.key = "aHjgYFutF672eGIUGGVlgSETyM9VJj0K" aes.iv="rxYoks3c8hRRsL2P" aes.update(Base64::decode64("ETlAHS5ZcshKxQUaHVB8=="))+aes.final =&gt; OpenSSL::Cipher::CipherError: bad decrypt </code></pre> <p>Same as above but in console, key and iv converted to hex with:</p> <pre><code>$ echo -n $key256 | hexdump -e '16/1 "%02x"' $ echo -n $iv | hexdump -e '16/1 "%02x"' $ echo "ETlAHS5ZcshKxQUaHVB8==" | openssl enc -d -aes-256-cbc -a -K 61486a675946757446363732654749554747566c67534554794d39564a6a304b -iv 7278596f6b73336338685252734c3250 bad decrypt 140378046432928:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:539: </code></pre> <p>BTW. to get back original key and iv in the console you an use:</p> <pre><code>$ echo 61486a6... | xxd -r -p #or , but then need to add \x before every character pair $ eval `printf "\x61\x48......" </code></pre> <p>Please give me some clues as I hoped in the beginning that I will be able to use <a href="https://github.com/chicks/aes" rel="nofollow">https://github.com/chicks/aes</a> gem. The gem seems fine, it's just a nice wrapper for <code>OpenSSL::Cipher::Cipher</code>.</p> <p>Is it possible that ruby/openssl use different PKCS, let's say PKCS#7, Java uses PKCS#5 and I need to preprocess my data ? Or there is a vesion mismatch between ruby/openssl and that Java's PKCS #7 and #5? #5 is meant for 8byte data blocks and #7 is for 16byte? Just a wild guess ...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload