我曾尝试将CBC模式的AES与PKCS#7填充一起用于CBC模式,但结果似乎与从在线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一起使用

最佳答案



是。 Crypto ++支持与64位分组密码一起使用的PKCS#5。 PKCS#7与128位分组密码一起使用,并且受支持。最后,Crypto ++还支持大块密码(例如256位块Kalyna和Threefish)的PKCS填充。我不确定大型分组密码采用哪种标准。



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

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

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



您可能也对Crypto ++ Wiki上的CBC Mode感兴趣。

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

10-15 12:32