python - 为什么 PyCrypto 不使用默认 IV?

标签 python aes pycrypto

我想弄清楚为什么我的 Python 客户端和 Ruby 服务器在如何加密数据方面存在分歧。我在 Ruby 代码和我的代码中看到的唯一区别是它们没有指定初始化向量,因此它回落到所有\x0 的默认值

当我尝试在没有 iv 的情况下实例化 PyCrypto 时,它给我一个错误。这是一个例子:

from Crypto.Cipher import AES
test = "Very, very confidential data"
key = b'Thirty Two Byte key, made Beef y' 

gryp = AES.new(key, AES.MODE_CBC)

(这个例子本质上是 PyCrypto 文档中的示例代码,没有指定 IV)文档说 w/r/t IV “它是可选的,如果不存在,它将被赋予全零的默认值。”但是我收到错误“ValueError:IV 必须是 16 个字节长”。

所以我可以指定 IV,这不是问题本身,但我试图弄清楚它是否认为它不能使用默认值,如果我使用库的方式有其他问题。

最佳答案

这似乎是 Pycrypto 的类文档中的错误 AES ,因为 AES 实现已更改,因此 IV 对于那些需要一个的模式不是是可选的(即,如果您想这样做的话,您必须自己传递 16 个字节的零).

查看此 bug report for the same issue有人没有指定 IV 并查找了在线文档。有一个明确要求 IV 的更改,基本上,没有人更新在线文档来反射(reflect)这一点。 Pycrypto 源中的类文档已更新,但需要重新生成在线文档以反射(reflect)这一点。

来源的新文档指出:

For all other modes, it must be block_size bytes longs.

代替旧版本的

For all other modes, it must be block_size bytes longs. It is optional and when not present it will be given a default value of all zeroes.

源代码中指定 iv 的更新示例是:

from Crypto.Cipher import AES
from Crypto import Random

key = b'Sixteen byte key'
iv = Random.new().read(AES.block_size)
cipher = AES.new(key, AES.MODE_CFB, iv)
msg = iv + cipher.encrypt(b'Attack at dawn')

关于python - 为什么 PyCrypto 不使用默认 IV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14389336/

相关文章:

python装饰器提取参数

Java:RSA 算法的 key 不适合对称加密。

java - Java中使用AES算法加密

python - 在 Mac OS X 上,easy_install fabric 和/或 easy_install pycrypto 由于链接器错误 "illegal text-relocation"而失败

python - 如何使用 warnings.filterwarnings 抑制第三方警告

python - PyCrypto:仅使用文件中的公钥解密(无私钥+公钥)

python - 使用外键映射从使用 Python 和 SQLAlchemy 的其他表中获取数据

python - 在python中如何添加到windows路径变量以查找可执行文件

python - While 循环范围(0,5)与 ran = ran[ :-1]; ran ! = [] 始终为真

java - PBEWITHSHA256AND128BITAES-CBC-BC 在 RedHat 6.4 上创建 java.security.NoSuchAlgorithmException