【发布时间】:2019-04-02 17:59:08
【问题描述】:
我曾尝试使用带有 PKCS#7 填充的 CBC 模式的 AES,但结果似乎与我从在线 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 一起使用
【问题讨论】: