ruby-aes默认使用填充吗? [英] does ruby-aes use padding by default?
问题描述
我在RoR项目中使用以下内容:
I am using the following in a RoR project:
somepass = Aes.encrypt_buffer(128,'ECB',some_cypher_key,nil, pain_string)
使用此lib和方法ECB是否默认使用填充?
Does using this lib and method ECB use padding by default or not?
我最终要做的是拥有一个RoR应用程序和一个Java应用程序能够使用相同的简单字符串创建相同的加密字符串。
What I am ultimately trying to do is have a RoR app and a Java app be able to create the same encrypted string out of the same simple string.
In我使用的Java代码:
cipher = Cipher.getInstance(AES / ECB / PKCS5Padding,SunJCE);
In Java code I use:
cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");
这两行代码不会创建相同的加密密钥。
These two lines of code do not create the same encrypted key.
推荐答案
Aes.encrypt_buffer
将使用填充,而不是您期望的那种。它将使用n个字节填充块,并添加字节值。也就是说,如果它需要添加15个字节,它将填充 0x0f
,如果需要添加5个字节,它将填充 0x05
。也就是说,PKCS7如 RFC-5652 中所述。
Aes.encrypt_buffer
will use padding, just not that kind that you are expecting. It will pad the block with n bytes with the value of added bytes. That is to say, if it needs to add 15 bytes, it will pad with 0x0f
, if it needs to add 5bytes, it will pad with 0x05
. That is to say, PKCS7 as described in RFC-5652.
您应该使用java切换到openssl或使用 Cipher.getInstance(AES / ECB / PKCS7Padding,BC)
。
You should switch to openssl or use Cipher.getInstance("AES/ECB/PKCS7Padding", "BC")
with java.
这篇关于ruby-aes默认使用填充吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!