在Visual c++中,数组的大小在传递给函数时自动改变

Sizeof array auto changed when passed into function in Visual C++

本文关键字:函数 改变 c++ Visual 数组      更新时间:2023-10-16

我使用库Crypto++加密/解密数据。官方页面是https://www.cryptopp.com。我正在遵循本教程。介绍了如何在Crypto++下使用分组密码。您可以通过查找关键字"using block cipher"查看此部分。

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

#include "E:WorkingImproveCPPcryptopp565osrng.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:WorkingImproveCPPcryptopp565cryptlib.h"
using CryptoPP::Exception;
#include "E:WorkingImproveCPPcryptopp565hex.h"
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;
#include "E:WorkingImproveCPPcryptopp565filters.h"
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::StreamTransformationFilter;
#include "E:WorkingImproveCPPcryptopp565aes.h"
using CryptoPP::AES;
#include "E:WorkingImproveCPPcryptopp565ccm.h"
#include "E:WorkingImproveCPPcryptopp565modes.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();
}

创建的密钥值为x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1。在演示代码中,键的值永远不会改变,它的大小总是16。当我调用encrypt()函数并将key传递给它时,密钥大小(sizeof(key))在创建时为16,但传递给函数后,长度始终为4(!)。键值是x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1ĂŒĂŒĂŒĂŒĂŒĂŒĂŒĂŒ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,你需要将数组中元素的数量作为单独的参数传递。从指针中获取它没有标准的方法

感谢@krzaq的评论。我解决了我的问题。问题是:key的大小和明文的大小必须作为数字传递给函数。在将指针传递给函数后,不能使用sizeof()来检索size。

我修复了代码:

// My encrypt function
void encrypt(byte cbCipherText[AES::BLOCKSIZE], byte *plainText,
             byte key[AES::DEFAULT_KEYLENGTH], int sizeKey, int sizeKey) {
  int size = sizeof(key);
  ECB_Mode<AES>::Encryption Encryptor(key, sizeKey);
  Encryptor.ProcessData(cbCipherText, plainText, textKey);
}
...
void main() {
 ...
  int sizeText = sizeOf(plainText);
  encrypt(myCipherText, PlainText, key, sizeKey, sizeText);
...
  }

现在它成功了!