Re: Homework help - File encrpytion/decryption problem...
On May 9, 5:15 am, "osmium" <r124c4u...@comcast.net> wrote:
"Juha Nieminen" wrote:
Chad <cdal...@gmail.com> wrote:
//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.
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 appears to work. With that, here is what I'm turning in...
//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 >> epassword;
cout << "Enter an input filename: ";
cin >> inputFilename;
cout << "Enter an output filename: ";
cin >> outputFilename;
srand(hash(epassword));
ifstream IS(inputFilename, ios::in | ios::binary);
ofstream OS(outputFilename, ios::out | ios::binary);
if (!IS) exit(1);
if (!OS) exit(1);
//encrpyt the file
while (IS.read(&c, 1))
{
value = (5*rand() + 3) % RAND_MAX;
c = value ^ c;
OS.write(&c, sizeof(c));
}
IS.close();
OS.close();
cout << "\nThe encrypted file is\n...";
ifstream IS1(outputFilename, ios::in | ios::binary);
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 | ios::binary);
if (!IS2) exit(1);
//display the decrypted file
srand(hash(epassword));
while (IS2.read(&c, sizeof(c)))
{
value = (5*rand() + 3) % RAND_MAX;
c = c ^ value;
cout << c;
}
cout << endl;
IS2.close();
system("pause");
return 0;
}
Now amuse me by breaking the encryption.