问题描述
我需要解密使用 AES / CBC / PKCS5Padding
方案加密的文本。我得到的加密文本是使用某些Java软件生成的。
I need to decrypt text encrypted using AES/CBC/PKCS5Padding
scheme. The encrypted text I got was generated using some Java software.
以下所有值都被我更改为虚构的东西。
All values below are changed by me to something fictional.
我得到的是密钥 aHjgYFutF672eGIUGGVlgSETyM9VJj0K
(256位= 32个字符* 8位)
和IV: rxYoks3c8hRRsL2P
(16位)
What I get is a Key aHjgYFutF672eGIUGGVlgSETyM9VJj0K
(256-bit = 32-chars * 8-bit)and IV: rxYoks3c8hRRsL2P
(16-bit)
和(我认为)Base64编码的加密结果 ETlAHS5ZcshKxQUaHVB8 ==
and (I supposed) Base64 encoded encrypted result ETlAHS5ZcshKxQUaHVB8==
我需要在Ruby中解密此 ETlAHS5ZcshKxQUaHVB8 ==
并插入一个简单的字符串就像 blablablabla一样
What I need is to decrypt in Ruby this ETlAHS5ZcshKxQUaHVB8==
to get in the and a simple string, like 'blablablabla'
我试图解密使用Ruby和通用Linux控制台openssl命令获得的内容。
注意:下面的键和IV并不是实际代码中使用的键:
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:
# 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
=> OpenSSL::Cipher::CipherError: bad decrypt
与上面相同,但在控制台,密钥和iv中进行了转换十六进制:
Same as above but in console, key and iv converted to hex with:
$ 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:
BTW。返回原始密钥并在控制台中返回iv,您可以使用:
BTW. to get back original key and iv in the console you an use:
$ echo 61486a6... | xxd -r -p
#or , but then need to add \x before every character pair
$ eval `printf "\x61\x48......"
请给我一些线索,就像我一开始希望的那样,我将能够使用 gem。宝石看起来不错,它只是 OpenSSL :: Cipher :: Cipher
的一个不错的包装。
Please give me some clues as I hoped in the beginning that I will be able to use https://github.com/chicks/aes gem. The gem seems fine, it's just a nice wrapper for OpenSSL::Cipher::Cipher
.
是否可能ruby / openssl使用不同的PKCS,假设PKCS#7,Java使用PKCS#5,我需要预处理数据吗?还是ruby / openssl与Java的PKCS#7和#5之间在版本上不匹配? #5表示8字节数据块,#7表示16字节?只是一个疯狂的猜测...
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 ...
推荐答案
我的第一篇文章中的Ruby代码是正确的,问题是这 Java部件使用的AES / CBC / PKCS5Padding
。
The Ruby code in my first post is correct, the problem was this AES/CBC/PKCS5Padding
used by Java part.
Java程序不应该将此方案用于 AES- CBC-256
。 PKCS5
填充为64位(8字节)块大小,但是 AES-256-CBC
使用16字节块。因此,必须使用 PKCS7
。
Java program should not use this scheme for AES-CBC-256
. PKCS5
pads to a 64 bit (8 byte) block size, but AES-256-CBC
uses 16 byte blocks. Therefore, PKCS7
must be used.
这篇关于Ruby中的AES / CBC / PKCS5Padding实现(用于rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!