【问题标题】:Does Crypto++ support PKCS#7 padding?Crypto++ 是否支持 PKCS#7 填充?
【发布时间】: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 一起使用

【问题讨论】:

标签: c++ padding crypto++


【解决方案1】:

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

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


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

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 2013-06-11
    • 2011-12-14
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    相关资源
    最近更新 更多