我正在使用库Crypto++加密/解密数据。官方页面为https://www.cryptopp.com。我正在关注this tutorial。它显示了如何在Crypto++中使用分组密码。您可以使用find关键字“ using block cipher”来查看此部分。

我可以顺利进行演示。他们使用密钥加密数据,然后使用相同的密钥解密数据。我想将代码拆分为encrypt()decrypt()函数。
您可以在下面看到我的encrypt()函数。

包括部分:

#include "E:\Working\Improve\CPP\cryptopp565\osrng.h"
using CryptoPP::AutoSeededRandomPool;

#include <iostream>
using std::cout;
using std::cerr;
using std::endl;

#include <string>
using std::string;

#include <cstdlib>
using std::exit;

#include "E:\Working\Improve\CPP\cryptopp565\cryptlib.h"
using CryptoPP::Exception;

#include "E:\Working\Improve\CPP\cryptopp565\hex.h"
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;

#include "E:\Working\Improve\CPP\cryptopp565\filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::StreamTransformationFilter;

#include "E:\Working\Improve\CPP\cryptopp565\aes.h"
using CryptoPP::AES;

#include "E:\Working\Improve\CPP\cryptopp565\ccm.h"
#include "E:\Working\Improve\CPP\cryptopp565\modes.h"
using CryptoPP::ECB_Mode;
#include <fstream>

#include "assert.h"



程式码主体:

// My encrypt function
void encrypt(byte cbCipherText[AES::BLOCKSIZE], byte *plainText,
             byte key[AES::DEFAULT_KEYLENGTH], int sizeKey) {
  int size = sizeof(key);
  ECB_Mode<AES>::Encryption Encryptor(key, sizeKey);

  Encryptor.ProcessData(cbCipherText, plainText, sizeof(plainText));
}

void main() {
  byte PlainText[] = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o',
                      'r', 'l', 'd', 0x0, 0x0, 0x0, 0x0, 0x0};

  byte key[AES::DEFAULT_KEYLENGTH];
  ::memset(key, 0x01, AES::DEFAULT_KEYLENGTH);

  // Encrypt data
  int size = sizeof(key);
  int default = AES::DEFAULT_KEYLENGTH;
  ECB_Mode<AES>::Encryption Encryptor(key, size);

  // Next three lines are tutorial's code for encrypt
  byte cbCipherText[AES::BLOCKSIZE];
  Encryptor.ProcessData(cbCipherText, PlainText, sizeof(PlainText));
  ECB_Mode<AES>::Decryption Decryptor(key, sizeof(key));

  // Next two lines are my code to call the encrypt() function, I "cloned" the
  // code
  // from above three line!. Comment out them we will have the code like the
  // demo.

  byte myCipherText[AES::BLOCKSIZE];
  encrypt(myCipherText, PlainText, key, size);

  // Decrypt
  byte cbRecoveredText[AES::BLOCKSIZE];

  Decryptor.ProcessData(cbRecoveredText, cbCipherText, sizeof(cbCipherText));

  //    std::string PlainText ="Voltaire said, Prejudices are what fools use for
  //reason";

  cout << endl << "Recovered text: " << cbRecoveredText << endl;
  getchar();
}


密钥是用值\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1创建的。在演示代码中,密钥的值从未更改,其大小始终为16。
当我调用encrypt()函数并将其传递给key时,其创建时的密钥大小(sizeof(key))为16,但是传递给函数后,其长度始终为4(!)。键值是x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1ĂŒĂŒĂŒĂŒĂŒĂŒĂŒĂŒHello World(!!!)。
因此,如果我跳入函数,我的代码总是会得到错误"AES: 4 is not valid key length"
我不明白为什么会这样以及如何解决。任何帮助,将不胜感激!

最佳答案

函数原型中的顶级数组不过是给程序员的提示(如果有的话)。

以下原型完全相同

void foo(int x[20]);
void foo(int x[]);
void foo(int* x);


换句话说,使用sizeof(x),您正在测量指针的大小。

您可以改用std::array来避免这种情况(但您可能要避免按值传递它)。

如果您绝对需要使用类似C的API,则需要将数组中的元素数量作为单独的参数传递。没有从指针获取它的标准方法。

09-10 01:02
查看更多