Homework help - File encrpytion/decryption problem...

C

Chad

The following program is supposed to encrypt and decrpyt a file. The
professor told use that we have to use XOR (exclusive or) when
encrypting and decrypting the file.

//Homework 8, question 2

#include <iostream>
#include <math.h>
#include <string>
#include <fstream>
#include <cstring>
#define MAX 40
#define HASHSIZE 101
using namespace std;

unsigned hash(char *s)
{
unsigned hashval;
for (hashval = 0; *s; s++)
{
hashval = *s + 31 * hashval;
}

return hashval % HASHSIZE;
}

int main(void)
{
unsigned int value;
char epassword[MAX];
char dpassword[MAX];
char inputFilename[MAX];
char outputFilename[MAX];
char c;

cout << "Enter a password: ";
cin.getline(epassword, MAX);
cout << "Enter an input filename: ";
cin >> inputFilename;
cout << "Enter an output filename: ";
cin >> outputFilename;

srand(hash(epassword));
value = rand();
//value = hash(epassword);

ifstream IS(inputFilename, ios::in);
ofstream OS(outputFilename, ios::eek:ut);

if (!IS) exit(1);
if (!OS) exit(1);

//encrpyt the file
while (IS.read(&c, 1))
{
c = value ^ c;
OS << c;
}

IS.close();
OS.close();

cout << "\nThe encrypted file is\n...";
ifstream IS1(outputFilename, ios::in);
if (!IS1) exit(1);

//display the encrypted file
while (IS1 >> c) cout << c;

cout << endl;
IS1.close();

while (1)
{
cout << "Enter the password to decrpyt the file: ";
cin >> dpassword;
if( strcmp(dpassword, epassword) == 0) break;
else cout << "Invalid password\n";
}
ifstream IS2(outputFilename, ios::in);
if (!IS2) exit(1);

//display the decrypted file
while (IS2 >> c)
{
c = c ^ value;
cout << c;
}
cout << endl;

IS2.close();
//system("pause");
return 0;
}

My input.dat file is..
This is some text

The program appears to works fine for some passwords like "sex" and
"alcohol". However, when I use a password like "underagegirls", the
decrpyted file, output.dat comes out as..

This is se text


Why is the output getting truncated?

Chad
 
L

Luca Risolia

The following program is supposed to encrypt and decrpyt a file. The
professor told use that we have to use XOR (exclusive or) when
encrypting and decrypting the file.

I wrote a simple example showing encryption and decryption using some
manipulators for istream's and ostream's. The encryption algorithm uses
the xor operator on a given passphrase. You may want to look at it to
have an idea:

http://www.linux-projects.org/listing/cpp_solutions/21.22/main.cpp

Because of the reasons Paavo Helde has mentioned in a previous post,
these (simple) manipulators have to keep track of the number of chars
encrypted or to decrypt.

I hope it helps.
 
J

Juha Nieminen

Chad said:
//encrpyt the file
while (IS.read(&c, 1))
{
c = value ^ c;
OS << c;
}

As a side note, I hope you understand that this is approximately the
poorest form of "encryption" possible.
 
O

osmium

Juha Nieminen said:
As a side note, I hope you understand that this is approximately the
poorest form of "encryption" possible.

To the OP:
When you get this working perhaps you could post a brief message and Juha
could break it for us.thus demonstrating just how poor the method is.
 
J

Juha Nieminen

osmium said:
To the OP:
When you get this working perhaps you could post a brief message and Juha
could break it for us.thus demonstrating just how poor the method is.

Well, it's completely trivial because there are only 256 possible
encryption keys. Just "decrypt" the message with all of them and look
which one results in clear text. (If the message were very large, you
wouldn't even need to decrypt it all. It would be enough to decrypt
the first few dozens of bytes, and see if it forms readable text.)

Even if that weren't possible, it would still be quite easy in another
way: Since a space is the most common character in a written piece of text,
just look at a value that repeats in average each 3 to 6 bytes in the
"encrypted" text, calculate its XOR with the character ' ' and then use
that to decrypt the entire message.

This form of "encryption" is incredibly poor.
 
W

woodbrian77

Well, it's completely trivial because there are only 256 possible
encryption keys. Just "decrypt" the message with all of them and look
which one results in clear text. (If the message were very large, you
wouldn't even need to decrypt it all. It would be enough to decrypt
the first few dozens of bytes, and see if it forms readable text.)

Even if that weren't possible, it would still be quite easy in another
way: Since a space is the most common character in a written piece of text,
just look at a value that repeats in average each 3 to 6 bytes in the
"encrypted" text, calculate its XOR with the character ' ' and then use
that to decrypt the entire message.

This form of "encryption" is incredibly poor.

I've been wondering what people think of this:
http://www.cprogramming.com/tutorial/xor.html



Ebenezer Enterprises
http://webEbenezer.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top