PyCrypto 在 CFB 模式下与 CommonCrypto 兼容吗?
我正在尝试获取一些 python 代码来解密使用 OS X CommonCrypto API 加密的数据。关于 CommonCrypto 使用的确切选项的文档很少甚至没有,因此我需要一些帮助来确定在 PyCrypto 中设置哪些选项。
具体来说,我的 CommonCrypto 解密设置调用是:
CCCryptorCreateWithMode(kCCDecrypt, kCCModeCFB, kCCAlgorithmAES128, ccDefaultPadding, NULL, key, keyLength, NULL, 0, 0, 0, &mAESKey);
我的主要问题是:
- 由于同时存在 kCCModeCFB 和 kCCModeCFB8,CommonCrypto 对 CFB 模式的定义是什么 - 段大小是多少等?
- CommonCrypto AES128 使用的块大小是多少? 16还是128?
- 默认填充是多少?在 CFB 模式下它是否重要?
目前,只要我将segment_size设置为16*,前4个字节的数据就可以使用PyCrypto成功解密。
有想法吗?
I'm trying to get somepython code to decrypt data that was encrypted using the OS X CommonCrypto APIs. There is little to no documentation on the exact options that CommonCrypto uses, so I'm needing some help figuring out what options to set in PyCrypto.
Specifically, my CommonCrypto decryption setup call is:
CCCryptorCreateWithMode(kCCDecrypt, kCCModeCFB, kCCAlgorithmAES128, ccDefaultPadding, NULL, key, keyLength, NULL, 0, 0, 0, &mAESKey);
My primary questions are:
- Since there is both a kCCModeCFB and kCCModeCFB8, what is CommonCrypto's definition of CFB mode - what segment size, etc?
- What block size is the CommonCrypto AES128 using? 16 or 128?
- What is the default padding, and does it even matter in CFB mode?
Currently, the first 4 bytes of data is decrypting successfully with PyCrypto *as long as I set the segment_size to 16*.
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不了解 CommonCrypto 或 PyCrypto 的情况下,一些部分答案:
AES(在所有三个变体中)的块大小为 128 位,即 16 字节。
CFB(密码反馈模式)实际上也可以在没有填充的情况下工作(即使用部分最后一个块),因为对于每个
块密文被创建为明文与某个密钥流块的异或,这仅取决于先前的块。
(您仍然可以使用任何您想要的填充。)
如果您可以尝试一些已知数据,请首先查看密文大小。如果不是a的倍数
完整块(与明文 + IV 相同),那么很可能没有填充。
否则,使用 noPadding 模式解密,查看结果,并与不同的已知填充模式进行比较。
从浏览源代码,可能是 PKCS#5-padding。
CFB8 是 CFB 的一种变体,它仅使用每个分组密码调用输出的前 8 位(= 一个字节)(它采用
前 128 位(= 16 字节)密文(或 IV)作为输入)。这需要 16 倍的分组密码调用,但是
允许部分发送流,而不必担心块边界。
CFB 还有另一个定义,其中包括段大小 - 这里段大小是
每个密码输出要使用的位(或字节)。在此定义中,“普通”CFB 的段大小为 128 位(= 16 字节),CFB8 的段大小为 8 位(一个字节)。
Without knowing CommonCrypto or PyCrypto, some partial answers:
AES (in all three variants) has a block size of 128 bits, which are 16 bytes.
CFB (cipher feedback mode) would actually also work without padding (i.e. with a partial last block), since for each
block the ciphertext is created as the XOR of plaintext with some keystream block, which only depends on previous blocks.
(You still can use any padding you want.)
If you can experiment with some known data, first have a look at the ciphertext size. If it is not a multiple of a
full block (and the same as the plaintext + IV), then it is quite likely no padding.
Otherwise, decrypt it with noPadding mode, have a look at the result, and compare with the different known padding modes.
From a glance at the source code, it might be PKCS#5-padding.
CFB8 is a variant of CFB which uses only the top 8 bits (= one byte) of each block cipher call output (which takes the
previous 128 bits (= 16 bytes) of ciphertext (or IV) as input). This needs 16 times as many block cipher calls, but
allows partial sending of a stream without having to worry about block boundaries.
There is another definition of CFB which includes a segment size - here the segment size is the number of
bits (or bytes) to be used from each cipher output. In this definition, the "plain" CFB would have a segment size of 128 bits (= 16 bytes), CFB8 would have a segment size of 8 bits (one byte).