472,789 Members | 1,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 software developers and data experts.

Simple XOR encryption code issues (segmentation fault)

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.
Mar 11 '07 #1
11 4932
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;
}

Mar 11 '07 #2
On 11 mar, 20:23, John Williams <druidjai...@gmail.comwrote:
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;

}
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

Mar 12 '07 #3
On 11 mar, 20:23, John Williams <druidjai...@gmail.comwrote:
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;

}
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

Mar 12 '07 #4
On 11 mar, 20:23, John Williams <druidjai...@gmail.comwrote:
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;

}
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

Mar 12 '07 #5
ge***********@gmail.com wrote:
On 11 mar, 20:23, John Williams <druidjai...@gmail.comwrote:
>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;

}

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
That worked perfectly to at least allow it to not segmentation fault.
It produces encrypted text however the decrypting isn't accurate so I
presume that one of my counters isn't correct logic so I'm going to
review them and probably have it working in a few minutes. Thank you
very much for helping get those pointers correct.

In regard to your other posts the "if (j >= (7))" is just silly as the
extra parenthesis are not needed. I think they are a remanent of when I
originally intended the program to be able to accept any key length.

Mar 12 '07 #6
On 3月12日, 上午10时04分, "gethostbyn...@gmail.com"
<gethostbyn...@gmail.comwrote:
On 11 mar, 20:23, John Williams <druidjai...@gmail.comwrote:


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/decryptthe 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;
}

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- 隐藏被引用文字 -

- 显示引用的文字 -
infile.read(buffer[j],1); //Read the current data at the get pointer
I think it's right! that is to say that it shuold not be
"infile.read(&buffer[j],1);".
but I find an another error ,when it runs,if you select 2(2 for no),it
will be wrong.
Mar 12 '07 #7
In article <Wb******************@newsfe13.phx>, dr*********@gmail.com
says...

[ ... ]
/* 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*/
I'd usually use command line arguments instead of interactive prompts
and such -- the latter are difficult to write well, and usually a real
pain to use.

I'd also tend to structure the program as a whole rather differently:

#include <fstream>
#include <algorithm>
#include <iostream>
#include <time.h>
#include <functional>
#include <iterator>
#include <ios>
#include <string>

// This class implements the actual encryption algorithm.
// Its function call operator is called once with each input
// character, so it only works for stream ciphers.
//
class xor : public std::binary_function<char, std::string, char{
std::string key_;
public:
xor(std::string const &key) : key_(key) {}

char operator()(char input) const {
static int key_pos = -1;
if (++key_pos key_.length())
key_pos = 0;

return input ^ key_[key_pos];
}
};

// pretty much copied from the OP
std::string gen_key() {
static const int key_len = 8;

srand(time(NULL));
std::string key;

for (int i=0; i<key_len; i++)
key += static_cast<char>(rand()%87+35);
return key;
}

int main(int argc, char **argv) {

if ( argc < 3 || argc 4) {
std::cerr << "Usage: xor <input_file<output_file[key]\n";
return EXIT_FAILURE;
}

std::ifstream infile(argv[1], std::ios::binary);
std::ofstream outfile(argv[2], std::ios::binary);

infile.unsetf(std::ios_base:: skipws);

std::string key;

// use supplied key or generate a new one.
if (argc == 4)
key = std::string(argv[3]);
else {
key = gen_key();
std::cout << "Key: " << key << "\n";
}

// encrypt the data from input to output:
std::transform(
std::istream_iterator<char>(infile),
std::istream_iterator<char>(),
std::ostream_iterator<char>(outfile),
xor(key));

return 0;
}

This isolates the encryption algorithm from the logic for things like
opening files and such, making it fairly easy to substitute another
stream cipher for the Vernam cipher currently being used.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 12 '07 #8
Jerry Coffin wrote:
In article <Wb******************@newsfe13.phx>, dr*********@gmail.com
says...

[ ... ]
>/* 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*/

I'd usually use command line arguments instead of interactive prompts
and such -- the latter are difficult to write well, and usually a real
pain to use.

I'd also tend to structure the program as a whole rather differently:

#include <fstream>
#include <algorithm>
#include <iostream>
#include <time.h>
#include <functional>
#include <iterator>
#include <ios>
#include <string>

// This class implements the actual encryption algorithm.
// Its function call operator is called once with each input
// character, so it only works for stream ciphers.
//
class xor : public std::binary_function<char, std::string, char{
std::string key_;
public:
xor(std::string const &key) : key_(key) {}

char operator()(char input) const {
static int key_pos = -1;
if (++key_pos key_.length())
key_pos = 0;

return input ^ key_[key_pos];
}
};

// pretty much copied from the OP
std::string gen_key() {
static const int key_len = 8;

srand(time(NULL));
std::string key;

for (int i=0; i<key_len; i++)
key += static_cast<char>(rand()%87+35);
return key;
}

int main(int argc, char **argv) {

if ( argc < 3 || argc 4) {
std::cerr << "Usage: xor <input_file<output_file[key]\n";
return EXIT_FAILURE;
}

std::ifstream infile(argv[1], std::ios::binary);
std::ofstream outfile(argv[2], std::ios::binary);

infile.unsetf(std::ios_base:: skipws);

std::string key;

// use supplied key or generate a new one.
if (argc == 4)
key = std::string(argv[3]);
else {
key = gen_key();
std::cout << "Key: " << key << "\n";
}

// encrypt the data from input to output:
std::transform(
std::istream_iterator<char>(infile),
std::istream_iterator<char>(),
std::ostream_iterator<char>(outfile),
xor(key));

return 0;
}

This isolates the encryption algorithm from the logic for things like
opening files and such, making it fairly easy to substitute another
stream cipher for the Vernam cipher currently being used.
Thank you for posting that. I've never taken an actual class in c++ so
it's really nice being able to see my program and yours that have almost
identical functionality, but yours has greatly improved form,
modularism, and safety as an example of how I can improve my style.
Mar 12 '07 #9
John Williams wrote:
ge***********@gmail.com wrote:
>On 11 mar, 20:23, John Williams <druidjai...@gmail.comwrote:
>>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;

}

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

That worked perfectly to at least allow it to not segmentation fault. It
produces encrypted text however the decrypting isn't accurate so I
presume that one of my counters isn't correct logic so I'm going to
review them and probably have it working in a few minutes. Thank you
very much for helping get those pointers correct.

In regard to your other posts the "if (j >= (7))" is just silly as the
extra parenthesis are not needed. I think they are a remanent of when I
originally intended the program to be able to accept any key length.
Found my issue, the bound j >= 7 should have been j >= 8 so problem is
solved and the software does as it was intended...now to write a program
that can defeat xor encryption.
Mar 12 '07 #10
On Mar 12, 4:58 pm, Jerry Coffin <jcof...@taeus.comwrote:
class xor : public std::binary_function<char, std::string, char{
'xor' is a keyword in C++, so you should choose a different
name for your class.
Mar 13 '07 #11
In article <11*********************@j27g2000cwj.googlegroups. com>,
ol*****@inspire.net.nz says...
On Mar 12, 4:58 pm, Jerry Coffin <jcof...@taeus.comwrote:
class xor : public std::binary_function<char, std::string, char{

'xor' is a keyword in C++, so you should choose a different
name for your class.
Oops -- good point. Thanks for the correction.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Mar 13 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Polar | last post by:
Hi! i'm a newbie in C language and i'm writing my first simple codes. In one of these, my purpose is to append the ascii value of an interger (example 101 --> e) at the end of a string to...
9
by: Narendran Kumaraguru Nathan | last post by:
Hi all, I am fairly experianced in C. I am writing a program in which I'm getting a segmentation fault. The problem is that it is getting the segmentation fault when executing calloc. I tried...
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
5
by: Michael | last post by:
Hi All, I have three very simple files as below. When I try and compile these with g++ -ansi -Wall -pedantic -o crap Base.h Other.h I get an error: Base.h:7: internal compiler error:...
8
by: parkskier | last post by:
Ok, so I got a segmentation fault when I tried to run the program I created. I understand what this error means, but I don't know where it is. If someone can quickly look through my code and point...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.