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:
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
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:
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