Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 数组的大小在传递到Visual C++;_C++_Arrays_Pointers_Sizeof - Fatal编程技术网

C++ 数组的大小在传递到Visual C++;

C++ 数组的大小在传递到Visual C++;,c++,arrays,pointers,sizeof,C++,Arrays,Pointers,Sizeof,我正在使用库Crypto++对数据进行加密/解密。官方网页是。我跟在后面。它展示了如何将分组密码与Crypto++结合使用。您可以使用find关键字“usingblockcipher”查看此部分 我可以顺利地运行演示。它们使用密钥加密数据,然后使用相同的密钥解密数据。我想将代码拆分为一个encrypt()和一个decrypt()函数。 您可以在下面看到我的encrypt()函数。 该部分包括: #include "E:\Working\Improve\CPP\cryptopp565\osrng

我正在使用库
Crypto++
对数据进行加密/解密。官方网页是。我跟在后面。它展示了如何将分组密码与
Crypto++
结合使用。您可以使用find关键字“usingblockcipher”查看此部分

我可以顺利地运行演示。它们使用密钥加密数据,然后使用相同的密钥解密数据。我想将代码拆分为一个
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"
#包括“E:\Working\Improve\CPP\cryptopp565\osrng.h”
使用CryptoPP::AutoSeedRandomPool;
#包括
使用std::cout;
使用std::cerr;
使用std::endl;
#包括
使用std::string;
#包括
使用std::exit;
#包括“E:\Working\Improve\CPP\cryptopp565\cryptolib.h”
使用CryptoPP::Exception;
#包括“E:\Working\Improve\CPP\cryptopp565\hex.h”
使用CryptoPP::HexEncoder;
使用CryptoPP::HexDecoder;
#包括“E:\Working\Improve\CPP\cryptopp565\filters.h”
使用CryptoPP::StringSink;
使用CryptoPP::StringSource;
使用CryptoPP::StreamTransformationFilter;
#包括“E:\Working\Improve\CPP\cryptopp565\aes.h”
使用CryptoPP::AES;
#包括“E:\Working\Improve\CPP\cryptopp565\ccm.h”
#包括“E:\Working\Improve\CPP\cryptopp565\modes.h”
使用CryptoPP::ECB_模式;
#包括
#包括“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();
}
//我的加密函数
无效加密(字节cbCipherText[AES::BLOCKSIZE],字节*明文,
字节键[AES::DEFAULT_KEYLENGTH],int sizeKey){
int size=sizeof(键);
ECB_模式::加密机(密钥,sizeKey);
ProcessData(cbCipherText,明文,sizeof(明文));
}
void main(){
字节明文[]={'H','e','l','l','o','W','o',',
“r”、“l”、“d”、0x0、0x0、0x0、0x0、0x0、0x0};
字节键[AES::DEFAULT_KEYLENGTH];
::memset(key,0x01,AES::DEFAULT_KEYLENGTH);
//加密数据
int size=sizeof(键);
int default=AES::default\U KEYLENGTH;
ECB_模式::加密机(密钥、大小);
//接下来的三行是教程的加密代码
字节cbCipherText[AES::BLOCKSIZE];
ProcessData(cbCipherText,明文,sizeof(明文));
ECB_模式::解密解密器(密钥,sizeof(密钥));
//接下来的两行是调用encrypt()函数的代码,我“克隆”了
//代码
//从上面的三行!注释掉它们,我们将得到如下代码
//演示。
字节myCipherText[AES::BLOCKSIZE];
加密(密文、明文、密钥、大小);
//解密
字节cbRecoveredText[AES::BLOCKSIZE];
ProcessData(cbRecoveredText、cbCipherText、sizeof(cbCipherText));
//std::string PlainText=“伏尔泰说过,偏见是愚人利用的东西
//理性”;

cout函数原型中的顶级数组只不过是对程序员的提示,如果这样的话

以下原型完全相同

void foo(int x[20]);
void foo(int x[]);
void foo(int* x);
换句话说,使用
sizeof(x)
,您正在测量指针的大小

您可以使用
std::array
来避免这种情况(但您可能希望避免按值传递它)


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

函数原型中的顶级数组只不过是对程序员的提示,如果有的话

以下原型完全相同

void foo(int x[20]);
void foo(int x[]);
void foo(int* x);
换句话说,使用
sizeof(x)
,您正在测量指针的大小

您可以使用
std::array
来避免这种情况(但您可能希望避免按值传递它)


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

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

我修正了密码:

// 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);

...
  }
//我的加密函数
无效加密(字节cbCipherText[AES::BLOCKSIZE],字节*明文,
字节键[AES::DEFAULT_KEYLENGTH],int-sizeKey,int-sizeKey){
int size=sizeof(键);
ECB_模式::加密机(密钥,sizeKey);
Encryptor.ProcessData(密文、明文、文本密钥);
}
...
void main(){
...
int-sizeText=sizeOf(纯文本);
加密(myCipherText、明文、密钥、sizeKey、sizeText);
...
}

现在它成功了!

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

我修正了密码:

// 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);

...
  }
//我的加密函数
无效加密(字节cbCipherText[AES::BLOCKSIZE],字节*明文,
字节键[AES::DEFAULT_KEYLENGTH],int-sizeKey,int-sizeKey){
int size=sizeof(键);
ECB_模式::加密机(密钥,sizeKey);
Encryptor.ProcessData(密文、明文、文本密钥);
}
...
void main(){
...
int-sizeText=sizeOf(纯文本);
加密(myCipherText、明文、密钥、sizeKey、sizeText);
...
}

现在它工作了!

因为我使用的是API中的ProcessData()函数,所以我必须将字节*或字节[]传递给它Encryptor.ProcessData(cbCipherText、plainText、sizeof(plainText))。可能是我不能在这种情况下使用std::array:\@Andiana为什么您认为
ProcessData
要求您将大小作为另一个参数传递?为什么他们不能调用
sizeof(data)
?@Andiana然后您需要向函数中添加另一个参数。@Andiana
sizeKey
似乎是
键的大小,而不是
明文的大小。您需要传递所需的每个数组的大小passing@krzaq:OMG!你救了我的命!我将明文的大小添加为参数