简单的XOR加密代码问题(分段错误) [英] Simple XOR encryption code issues (segmentation fault)

查看:92
本文介绍了简单的XOR加密代码问题(分段错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个简单的程序来进行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.

推荐答案

oops忘记附上我的代码


/ * XOR加密算法的简单实现。不是非常安全,但

*快速且易于使用。作为基本的学习练习写在

*加密的基础知识中,这应该只用于学习或最基本的

*加密,因为XOR很容易被击败* /


//由Jaidan撰写(John Williams)3/11/07


#include< stdio.h>

#include< iostream>

#include< fstream>

#include< time.h>

#include < math.h>


using namespace std;


void Cipher(char key [],char inname [],char outname [ ]);

void keymaker(char key []);

int filesizer(fstream * file);


int main ()

{

/ * main收集基本的起始信息,文件名和要使用的密钥和

*调用相应的函数关于用户输入* /


int booltest;

char key [8],inname [21],outname [21];


cout<< 请输入输入文件名:";

cin> inname;

cout<< 请输入输出文件名:";

cin> outname;

cout<< 你想使用auto keygen(1表示是/ 2表示否)?" ;;

cin> booltest;

cout<< endl;


if(booltest == 1){

keymaker(key);

Cipher(key,inname,outname) );

}

else if(booltest == 2){

cout<< 请输入所需的8位数字键:" ;;

cin> key;

cout<< endl;

Cipher(key,inname,outname);

}

cout<< 处理完成!\ n;

返回0;


}


void keymaker(char key [8])

{

/ *生成随机密钥用于加密,因为允许的字符是

*有点宽因此键很难记住(键入?)* /

int i,随机;

srand(time(NULL));


for(i = 0; i< 8; i ++){


key [i] =((rand()%87)+35); / /生成有限的随机ascii char

}

cout<< 将使用的密钥是: << key<<结束;

返回;

}

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(& infile);


for(i = 0,j = 0; i< filesize; j ++,i ++){

if(j> =(7)){//重置开始的关键

j = 0;

outfile.write(* buffer,8); //清除文件缓冲区

}

infile.seekg(i); //设置指向当前引用的get

infile.read(buffer [j],1); //在get指针处读取当前数据

* buffer [j] = * buffer [j] ^ key [j]; // XOR加密/解密当前数据

}

返回;

}


int filesizer(fstream * file)

{

/ *通过寻找文件末尾来确定文件大小* /

int size;


file-> seekg(0,ios :: end);

size = file-> tellg();

file-> seekg(0);

返回大小;

}

oops forgot to attach my code


/* A simple implementation of a XOR encryption algorithm. Not terribly secure, but
* fast and easy to use. Written as a basic learning exercise in the basics of
* encryption this should only be used for study or for the most basic of
* encryption as XOR is easily defeated*/

//Written by Jaidan (John Williams) 3/11/07

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <math.h>

using namespace std;

void Cipher(char key[], char inname[], char outname[]);
void keymaker(char key[]);
int filesizer(fstream *file);

int main()
{
/* main gathers the basic starting information, the filenames and key to be used and
* calls the appropriate functions based on user input*/

int booltest;
char key[8], inname[21], outname[21];

cout << "Please type input filename: ";
cin >inname;
cout << "Please type output filename: ";
cin >outname;
cout << "Would you like to use the auto keygen (1 for yes / 2 for no)?";
cin >booltest;
cout << endl;

if (booltest == 1) {
keymaker(key);
Cipher(key, inname, outname);
}
else if (booltest == 2) {
cout << "Please type desired 8 digit key: ";
cin >key;
cout << endl;
Cipher(key, inname, outname);
}
cout << "Process completed!\n";
return 0;

}

void keymaker(char key[8])
{
/* Generates a random key for use in the encryption, as the characters allowed are
* kinda wide and therefore the key will be hard to remember (type?)*/
int i, random;
srand(time(NULL));

for (i = 0; i < 8; i++) {

key[i] = ((rand() % 87) +35);// generates a limited random ascii char
}
cout << "The key that will be used is: " << key << endl;
return;
}
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;
}

int filesizer(fstream *file)
{
/*Determines the file size by seeking the end of the file*/
int size;

file->seekg(0, ios::end);
size = file->tellg();
file->seekg(0);
return size;
}


开3月11日,20:23,John Williams< druidjai ... @ gmail.comwrote:
On 11 mar, 20:23, John Williams <druidjai...@gmail.comwrote:

oops忘了附上我的代码


[jxorcrypt.cpp] / * XOR加密算法的简单实现。不是非常安全,但

*快速且易于使用。作为基本的学习练习写在

*加密的基础知识中,这应该只用于学习或最基本的

*加密,因为XOR很容易被击败* /


//由Jaidan撰写(John Williams)3/11/07


#include< stdio.h>

#include< iostream>

#include< fstream>

#include< time.h>

#include < math.h>


using namespace std;


void Cipher(char key [],char inname [],char outname [ ]);

void keymaker(char key []);

int filesizer(fstream * file);


int main ()

{

/ * main收集基本的起始信息,文件名和要使用的密钥和

*调用相应的函数关于用户输入* /


int booltest;

char key [8],inname [21],outname [21];


cout<< 请输入输入文件名:";

cin> inname;

cout<< 请输入输出文件名:";

cin> outname;

cout<< 你想使用auto keygen(1表示是/ 2表示否)?" ;;

cin> booltest;

cout<< endl;


if(booltest == 1){

keymaker(key);

Cipher(key,inname,outname) );

}

else if(booltest == 2){

cout<< 请输入所需的8位数字键:" ;;

cin> key;

cout<< endl;

Cipher(key,inname,outname);

}

cout<< 处理完成!\ n;

返回0;


}


void keymaker(char key [8])

{

/ *生成随机密钥用于加密,因为允许的字符是

*有点宽因此键很难记住(键入?)* /

int i,随机;

srand(time(NULL));


for(i = 0; i< 8; i ++){


key [i] =((rand()%87)+35); / /生成有限的随机ascii char

}

cout<< 将使用的密钥是: << key<<结束;

返回;}


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(& infile);


for(i = 0,j = 0; i< filesize; j ++,i ++){

if(j> =(7)){//重置开始的关键

j = 0;

outfile.write(* buffer,8); //清除文件缓冲区

}

infile.seekg(i); //设置指向当前引用的get

infile.read(buffer [j],1); //在get指针处读取当前数据

* buffer [j] = * buffer [j] ^ key [j]; // XOR加密/解密当前数据

}

返回;


}


int filesizer(fstream * file)

{

/ *通过寻找文件末尾来确定文件大小* /

int size;


file-> seekg(0,ios :: end);

size = file-> tellg();

file-> seekg(0);

返回大小;


}
oops forgot to attach my code

[jxorcrypt.cpp]/* A simple implementation of a XOR encryption algorithm. Not terribly secure, but
* fast and easy to use. Written as a basic learning exercise in the basics of
* encryption this should only be used for study or for the most basic of
* encryption as XOR is easily defeated*/

//Written by Jaidan (John Williams) 3/11/07

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <math.h>

using namespace std;

void Cipher(char key[], char inname[], char outname[]);
void keymaker(char key[]);
int filesizer(fstream *file);

int main()
{
/* main gathers the basic starting information, the filenames and key to be used and
* calls the appropriate functions based on user input*/

int booltest;
char key[8], inname[21], outname[21];

cout << "Please type input filename: ";
cin >inname;
cout << "Please type output filename: ";
cin >outname;
cout << "Would you like to use the auto keygen (1 for yes / 2 for no)?";
cin >booltest;
cout << endl;

if (booltest == 1) {
keymaker(key);
Cipher(key, inname, outname);
}
else if (booltest == 2) {
cout << "Please type desired 8 digit key: ";
cin >key;
cout << endl;
Cipher(key, inname, outname);
}
cout << "Process completed!\n";
return 0;

}

void keymaker(char key[8])
{
/* Generates a random key for use in the encryption, as the characters allowed are
* kinda wide and therefore the key will be hard to remember (type?)*/
int i, random;
srand(time(NULL));

for (i = 0; i < 8; i++) {

key[i] = ((rand() % 87) +35);// generates a limited random ascii char
}
cout << "The key that will be used is: " << key << endl;
return;}

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;

}

int filesizer(fstream *file)
{
/*Determines the file size by seeking the end of the file*/
int size;

file->seekg(0, ios::end);
size = file->tellg();
file->seekg(0);
return size;

}



此行有一个问题:

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


开3月11日,20:23,John Williams< druidjai ... @ gmail.comwrote:
On 11 mar, 20:23, John Williams <druidjai...@gmail.comwrote:

oops忘了附上我的代码


[jxorcrypt.cpp] / * XOR加密算法的简单实现。不是非常安全,但

*快速且易于使用。作为基本的学习练习写在

*加密的基础知识中,这应该只用于学习或最基本的

*加密,因为XOR很容易被击败* /


//由Jaidan撰写(John Williams)3/11/07


#include< stdio.h>

#include< iostream>

#include< fstream>

#include< time.h>

#include < math.h>


using namespace std;


void Cipher(char key [],char inname [],char outname [ ]);

void keymaker(char key []);

int filesizer(fstream * file);


int main ()

{

/ * main收集基本的起始信息,文件名和要使用的密钥和

*调用相应的函数关于用户输入* /


int booltest;

char key [8],inname [21],outname [21];


cout<< 请输入输入文件名:";

cin> inname;

cout<< 请输入输出文件名:";

cin> outname;

cout<< 你想使用auto keygen(1表示是/ 2表示否)?" ;;

cin> booltest;

cout<< endl;


if(booltest == 1){

keymaker(key);

Cipher(key,inname,outname) );

}

else if(booltest == 2){

cout<< 请输入所需的8位数字键:" ;;

cin> key;

cout<< endl;

Cipher(key,inname,outname);

}

cout<< 处理完成!\ n;

返回0;


}


void keymaker(char key [8])

{

/ *生成随机密钥用于加密,因为允许的字符是

*有点宽因此键很难记住(键入?)* /

int i,随机;

srand(time(NULL));


for(i = 0; i< 8; i ++){


key [i] =((rand()%87)+35); / /生成有限的随机ascii char

}

cout<< 将使用的密钥是: << key<<结束;

返回;}


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(& infile);


for(i = 0,j = 0; i< filesize; j ++,i ++){

if(j> =(7)){//重置开始的关键

j = 0;

outfile.write(* buffer,8); //清除文件缓冲区

}

infile.seekg(i); //设置指向当前引用的get

infile.read(buffer [j],1); //在get指针处读取当前数据

* buffer [j] = * buffer [j] ^ key [j]; // XOR加密/解密当前数据

}

返回;


}


int filesizer(fstream * file)

{

/ *通过寻找文件末尾来确定文件大小* /

int size;


file-> seekg(0,ios :: end);

size = file-> tellg();

file-> seekg(0);

返回大小;


}
oops forgot to attach my code

[jxorcrypt.cpp]/* A simple implementation of a XOR encryption algorithm. Not terribly secure, but
* fast and easy to use. Written as a basic learning exercise in the basics of
* encryption this should only be used for study or for the most basic of
* encryption as XOR is easily defeated*/

//Written by Jaidan (John Williams) 3/11/07

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <math.h>

using namespace std;

void Cipher(char key[], char inname[], char outname[]);
void keymaker(char key[]);
int filesizer(fstream *file);

int main()
{
/* main gathers the basic starting information, the filenames and key to be used and
* calls the appropriate functions based on user input*/

int booltest;
char key[8], inname[21], outname[21];

cout << "Please type input filename: ";
cin >inname;
cout << "Please type output filename: ";
cin >outname;
cout << "Would you like to use the auto keygen (1 for yes / 2 for no)?";
cin >booltest;
cout << endl;

if (booltest == 1) {
keymaker(key);
Cipher(key, inname, outname);
}
else if (booltest == 2) {
cout << "Please type desired 8 digit key: ";
cin >key;
cout << endl;
Cipher(key, inname, outname);
}
cout << "Process completed!\n";
return 0;

}

void keymaker(char key[8])
{
/* Generates a random key for use in the encryption, as the characters allowed are
* kinda wide and therefore the key will be hard to remember (type?)*/
int i, random;
srand(time(NULL));

for (i = 0; i < 8; i++) {

key[i] = ((rand() % 87) +35);// generates a limited random ascii char
}
cout << "The key that will be used is: " << key << endl;
return;}

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;

}

int filesizer(fstream *file)
{
/*Determines the file size by seeking the end of the file*/
int size;

file->seekg(0, ios::end);
size = file->tellg();
file->seekg(0);
return size;

}



请记住,您正在使用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加密代码问题(分段错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆