与带有 AESEngine 和 PKCS7 填充的 bouncycastle 的 PaddedBufferedBlockCipher 等效的密码是什么?
What is the equivalent cipher to bouncycastle's PaddedBufferedBlockCipher with AESEngine and PKCS7 padding?
我想用nodejs解密AES-256加密字符串。我正在为此使用加密模块。
字符串使用 Bouncy castle java 库加密。在 Java 中,密码被初始化使用:
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new AESEngine(), new PKCS7Padding());
crypto
nodejs 模块使用 openssl 的密码列表来初始化它,例如:
var decipher = crypto.createDecipher('aes-256-cbc',key);
我应该使用哪种算法?
以下是可供选择的算法列表:
-bash-4.1$ openssl list-cipher-algorithms|grep AES-256
AES-256-CBC
AES-256-CFB
AES-256-CFB1
AES-256-CFB8
AES-256-CTR
AES-256-ECB
AES-256-OFB
AES-256-XTS
AES256 => AES-256-CBC
aes256 => AES-256-CBC
使用AES-256-ECB
解密数据(我没有看到任何CBC或其他模式。)。
调用 decipher.setAutoPadding(true)
以使用 PKCS 填充。
如果您使用块密码加密某些内容,则需要
- 可以采用单个输入块并将其分解为单个输出块的块密码(对于 AES,块大小为 16 字节),
- 使您能够以结构化方式加密多个块的操作模式
- 使您能够加密长度不等于块大小倍数的内容的填充。
您显示的PaddedBufferedBlockCipher
只有两个。操作模式隐含为 ECB 模式,因为它仅包括将块密码分别应用于每个块。
您将在 node.js 中获得相同的行为:
var decipher = crypto.createDecipheriv('aes-xxx-ecb', key, '');
将 xxx
换成您的密钥大小(以位为单位)。有效大小为 128 位、192 位和 256 位。其他一切都行不通。另外,请确保您的密钥编码正确。
如果您想知道为什么在这里使用 createDecipheriv
而不是 createDecipher
,我建议您仔细比较这两个函数的文档。 createDecipher
需要密码而不是密钥。
其他注意事项:
永远不要使用 ECB mode. It's deterministic and therefore not semantically secure. You should at the very least use a randomized mode like CBC or CTR. It is better to authenticate your ciphertexts so that attacks like a padding oracle attack are not possible. This can be done with authenticated modes like GCM or EAX, or with an encrypt-then-MAC 方案。
我想用nodejs解密AES-256加密字符串。我正在为此使用加密模块。
字符串使用 Bouncy castle java 库加密。在 Java 中,密码被初始化使用:
PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new AESEngine(), new PKCS7Padding());
crypto
nodejs 模块使用 openssl 的密码列表来初始化它,例如:
var decipher = crypto.createDecipher('aes-256-cbc',key);
我应该使用哪种算法?
以下是可供选择的算法列表:
-bash-4.1$ openssl list-cipher-algorithms|grep AES-256
AES-256-CBC
AES-256-CFB
AES-256-CFB1
AES-256-CFB8
AES-256-CTR
AES-256-ECB
AES-256-OFB
AES-256-XTS
AES256 => AES-256-CBC
aes256 => AES-256-CBC
使用AES-256-ECB
解密数据(我没有看到任何CBC或其他模式。)。
调用 decipher.setAutoPadding(true)
以使用 PKCS 填充。
如果您使用块密码加密某些内容,则需要
- 可以采用单个输入块并将其分解为单个输出块的块密码(对于 AES,块大小为 16 字节),
- 使您能够以结构化方式加密多个块的操作模式
- 使您能够加密长度不等于块大小倍数的内容的填充。
您显示的PaddedBufferedBlockCipher
只有两个。操作模式隐含为 ECB 模式,因为它仅包括将块密码分别应用于每个块。
您将在 node.js 中获得相同的行为:
var decipher = crypto.createDecipheriv('aes-xxx-ecb', key, '');
将 xxx
换成您的密钥大小(以位为单位)。有效大小为 128 位、192 位和 256 位。其他一切都行不通。另外,请确保您的密钥编码正确。
如果您想知道为什么在这里使用 createDecipheriv
而不是 createDecipher
,我建议您仔细比较这两个函数的文档。 createDecipher
需要密码而不是密钥。
其他注意事项:
永远不要使用 ECB mode. It's deterministic and therefore not semantically secure. You should at the very least use a randomized mode like CBC or CTR. It is better to authenticate your ciphertexts so that attacks like a padding oracle attack are not possible. This can be done with authenticated modes like GCM or EAX, or with an encrypt-then-MAC 方案。