问题描述
我已经写了一个简单的程序来进行XOR加密,这是我第一次尝试了解加密的工作方式。代码编译得很好,但是每次运行都会出现分段错误。使用gdb调试它让我
将问题缩小到Cipher函数我认为它出错了行
84或85.该程序使它首次读取/ cipher / write pass没有
问题,但是第二次传球杀了它。使用gdb打印变量
左边显示在seg故障之前的以下内容。
i == 1
j == 1
* buffer [0]包含来自上一遍的加密文本
* buffer [1]无法访问地址处的内存...
*缓冲[2]随机值(未初始化......但内存可以访问
)
我无法弄清楚为什么*缓冲区[1]无法访问我的代码和
会感谢一些帮助搞清楚我的错误。
I''ve written a simple program to do XOR encryption as my first foray
into understanding how encryption works. The code compiles fine, however
it segmentation faults on every run. using gdb to debug it let me
narrow the problem down to the Cipher function I think it faults at line
84 or 85. The program makes it''s first read/cipher/write pass without
issue but the second pass kills it. Using gdb to print the variables
left showed me the following right before the seg fault.
i == 1
j == 1
*buffer[0] contains ciphered text from the previous pass
*buffer[1] "Cannot access memory at address ..."
*buffer[2] random values (uninitialized...however the memory is able to
be accessed)
I can''t figure out why *buffer[1] would be inaccessible with my code and
would appreciate some help figuring out my error.
推荐答案
此行有一个问题:
infile.read(buffer [j],1); //在获取时读取当前数据
这是我提议的修改:
void Cipher(char key [],char inname [ ],char outname [])
{
/ * XOR加密/解密算法。从指定的
输入文件中获取输入,并将
*输出到指定的输出文件。由于XOR
加密的性质,这个
int i,j,block,ArraySize,filesize;
char buffer [8];
fstream infile,outfile;
infile .open(inname,ios :: in);
outfile.open(outname,ios :: out | ios :: trunc);
filesize = filesizer(& (b =(i = 0,j = 0; i< filesize; j ++,i ++){
if(j> =(inf));
7)){//将密钥重置为开始
j = 0;
outfile.write(buffer,8); //清除缓冲区
文件
}
infile.seekg(i); //设置get指向
当前参考
infile.read(& buffer [j],1); //读取当前数据
获取指针
buffer [j] = buffer [j] ^ key [j]; // XOR加密/解密
当前数据
}
返回;
}
当然,您可以通过其他方式更改代码。现在正确生成预期文件
吗?
gethostbyname
There is a problem in this line:
infile.read(buffer[j],1); //Read the current data at the get
This is my proposed modifications:
void Cipher(char key[], char inname[], char outname[])
{
/*XOR encryption/decipher algorithm. takes input from a specified
input file and
* outputs it to a specified output file. Due to the nature of XOR
encryption this
* algorithm handles both the encryption and deciphering of the data*/
int i, j, block, ArraySize, filesize;
char buffer[8];
fstream infile, outfile;
infile.open(inname, ios::in);
outfile.open(outname, ios::out | ios::trunc);
filesize = filesizer(&infile);
for (i = 0, j = 0; i < filesize; j++, i++) {
if (j >= (7)) { // Reset the key to the begining
j = 0;
outfile.write(buffer, 8); // Purge buffer to
file
}
infile.seekg(i); //sets the get pointed to the
current reference
infile.read(&buffer[j],1); //Read the current data at
the get pointer
buffer[j] = buffer[j]^key[j]; //XOR encrypt/decrypt
the current data
}
return;
}
Of course, you can change your code in others ways. Is it generating
the expected file correctly now?
gethostbyname
请记住,您正在使用C ++ iostream与C设计代码的混合。
这不好。
嘿,为什么括号涉及数字?
" if(j> =(7)){"
PS。原谅我的英语
gethostbyname
Remember you are using a mixture of C++ iostreams with C design code.
It isn''t good.
Hey, why the parenthesis involving the digit?
" if (j >= (7)) {"
PS. forgive my english
gethostbyname
这篇关于简单的XOR加密代码问题(分段错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!