在Visual c++中,数组的大小在传递给函数时自动改变
Sizeof array auto changed when passed into function in Visual C++
我使用库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);
...
}
现在它成功了!
- 变量没有改变?通过向量的函数调用
- 为什么提升图库的 read_graphviz() 函数会改变节点的索引
- 为什么我的函数接受"std::string"进行排序不会改变它?
- 通过从构造函数中的'this'复制的指针改变常量对象
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 有没有办法创建一个不改变程序中参数的函数
- 为什么我的函数不改变对象的属性
- 类中的枚举在调用自己的函数时不会改变
- C - 如何将各处的静态函数称为改变其私有变量
- 基于继承的类型参数改变父虚拟函数的行为
- 对列表中的每个对象调用成员函数,以改变对象所在的列表
- 为什么调用 CUDA 内核函数时这个类成员变量没有改变
- 当函数应该改变对象的状态时,是选择成员函数还是朋友函数
- 一个什么都不做的函数可以改变一个对象吗?
- ifstream 函数"get"如何改变其字符参数?
- 为什么一个函数的末尾有常量这个词,即使它确实改变了一个变量
- visual C++未执行的代码会改变函数行为
- 不被类函数改变的私有int值
- 谷歌模拟函数改变参数值
- 被非const成员函数改变的可变变量