如何从文件中读取前256位并将其存储在两个数组中
How to read first 256 bits from a file to store them in two arrays
我正在编码一个密码,密钥文件长度为256位。其中128位进入key, 128位进入IV。我明确地定义了这些数组的大小,然后读取,仍然输出我得到的是像148位设置的键,而IV具有较小的因此。最后给出堆栈粉碎错误。
int main(int argc,char *argv[]) {
if(argc < 4 || !argv[1] || !argv[2]) {
cout << "Usage: Encoding: " << argv[0] << " [plaintext-file] [key-file] [cipher-file]" << endl;
cout << "Usage: Decoding: " << argv[0] << " [cipher-file] [key-file] [plaintext-file]" << endl;
return 0;
}
unsigned char key[16], iv[16];
unsigned long i;
// read input file and size etc
FILE* in_file = fopen(argv[1],"rb");
if(!in_file) {
cout << "Error opening read file" << endl;
return 0;
}
fseek(in_file,0,SEEK_END);
unsigned int msglength = ftell(in_file);
cout << "Msglength:" << msglength << endl;
unsigned char * mem_ptr = (unsigned char*)malloc(msglength);
if(!mem_ptr) {
cout << "Error allocating memory" << endl;
fclose(in_file);
return 0;
}
rewind(in_file);
fread(mem_ptr,1,msglength,in_file);
fclose(in_file);
// read keyfile etc
FILE* key_file = fopen(argv[2],"rb");
if(!key_file) {
cout << "Error opening key file" << endl;
return 0;
}
fseek(key_file,0,SEEK_END);
unsigned int key_size = ftell(key_file);
cout << "Key Size:" << key_size << endl;
unsigned char * key_ptr = (unsigned char*)malloc(key_size);
if(!key_ptr) {
cout << "Error allocating memory" << endl;
fclose(key_file);
return 0;
}
rewind(key_file);
fread(key_ptr,1,key_size,key_file);
cout << "sizeof key"<<sizeof(key)<<" sizeof iv" << sizeof(iv) << endl;
for (i=0; i<256; i++) {
if (i<128) key[i] = key_ptr[i];
else iv[i-128] = key_ptr[i];
}
cout << key_ptr << endl;
cout << "Key:" << key << endl << "IV:" << iv << endl;
}
输出Msglength:15
Key Size:257
sizeof key16 sizeof iv16
abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678
Key:abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678
IV:abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678abcdefgh12345678
*** stack smashing detected ***: ./a.out terminated
Segmentation fault (core dumped)
你知道我哪里做错了吗?
您的key
和iv
的大小都是16。
unsigned char key[16], iv[16];
所以这是不好的
for (i=0; i<256; i++) {
if (i<128) key[i] = key_ptr[i];
else iv[i-128] = key_ptr[i];
}
你在这两个数组的末尾都写错了
您有一个缓冲区溢出:您定义key
和iv
为unsigned char[16]
,但在您的for
循环中,您尝试访问它们,如果它们是unsigned char[128]
。
你是否在某些时候混淆了比特和字节?
根据这篇文章Stack Smashing实际上是gcc用来检测缓冲区溢出攻击的保护机制
and I could see
unsigned char key[16], iv[16];
未初始化,可以考虑将它们初始化为0或NULL
也 for (i=0;i<256;i++) {
if (i<128) key[i] = key_ptr[i];
else iv[i-128] = key_ptr[i];
}
这里没有NULL终止符也许你可以看看这些来了解更多
相关文章:
- 在c++中获取两个大int,并将它们存储在数组中
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- C++ 如何在包含两个变量的结构中存储与变量不同数量的值?
- 将一个阵列中的数据存储到另外两个阵列中不起作用
- 图问题:找出两个节点是否在每个节点的O(1)时间和O(2)存储中共享同一分支
- C ++将文本存储在数组的不同索引中,并带有一两个
- 两个大数字的模型存储为字符串
- 如何乘以两个值并原子存储结果
- 添加两个时间值存储为数据库中的字符串
- C 存储两个类对象之间的差异
- C++将存储在链表中的两个大数字相乘
- 原子功能无锁可以更改两个独立的存储位置
- 为什么双精度不存储两个整数的乘积
- 存储两个3-D向量的数据并计算点产品
- 如何在一个数组(C++)中存储两个整数
- 相同的内存位置存储两个不同的值
- 在向量<string>中存储两个间隔字符串
- 一个vector必须存储两个size吗?