gpt4 book ai didi

c++ - Crypto++ 是否支持 PKCS#7 填充?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:03:23 31 4
gpt4 key购买 nike

我尝试过使用 AES 和 CBC 模式以及 PKCS#7 填充,但结果似乎与我从在线 AES 网站程序中获得的结果不同。

答案应该是817b c015 a162 57ff 845b fa0c 4dc2 fcbb,我得到的是 8fed aeca 2fe9 fa8a 9f35 0468 0258 e80c

我已阅读 crypto++ 8.1 手册,PKCS_PADDING 适用于 PKCS#5 而不是 #7 https://www.cryptopp.com/docs/ref/struct_block_padding_scheme_def.html?fbclid=IwAR18UIE4hi9Menmm6Pze9GBn4loScIGqyTMDel8HujIUChefuIax4hO1u6k#abea06c498771e8f0ad0fbbc19416a979a622df395c2f0edc35f722d938faaad1f


#include <iostream>
#include <iomanip>
#include <string>

#include "cryptopp/modes.h"
#include "cryptopp/aes.h"
#include "cryptopp/filters.h"

int main(int argc, char* argv[]){

byte key[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36};
byte iv[CryptoPP::AES::BLOCKSIZE] = {0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30};

std::string plaintext = "Hello World!";
std::string ciphertext;

CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(ciphertext), CryptoPP::StreamTransformationFilter::PKCS_PADDING);

stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length()+1);
stfEncryptor.MessageEnd();

for (int i = 0; i < ciphertext.size(); i++) {
std::cout << "0x" << std::hex << (0xFF & static_cast<byte>(ciphertext[i])) << "\n";
}

std::cout << "\n";


return 0;
}


我想知道有什么方法可以将 AES 与 PKCS#7 一起使用

最佳答案

Does Crypto++ support PKCS#7 padding?

是的。 Crypto++ 支持 PKCS #5,它与 64 位 block 密码一起使用。 PKCS #7 与 128 位 block 密码一起使用,并且受支持。最后,Crypto++ 还支持大型 block 密码的 PKCS 填充,例如 256 位 block Kalyna 和 Threefish。我不确定什么标准管理大块密码。


I have tried using AES with CBC mode with PKCS#7 padding, but the result seems to different to what I get from an online AES website program.

这可能是网络应用程序的编码问题。

您应该使用测试 vector 来交叉验证结果。这是来自 NIST SP800-38A 的 AES/CBC :

key: 2b7e151628aed2a6abf7158809cf4f3c
iv: 000102030405060708090a0b0c0d0e0f
plaintext: 6bc1bee22e409f96e93d7e117393172a ae2d8a571e03ac9c9eb76fac45af8e51
30c81c46a35ce411e5fbc1191a0a52ef f69f2445df4f9b17ad2b417be66c3710
ciphertext: 7649abac8119b246cee98e9b12e9197d 5086cb9b507219ee95db113a917678b2
73bed6b8e3c1743b7116e69e22229516 3ff1caa1681fac09120eca307586e1a7

I wonder is there any way to use AES with PKCS#7

您可能还对 CBC Mode 感兴趣在 Crypto++ wiki 上。

关于c++ - Crypto++ 是否支持 PKCS#7 填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55420257/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com