QUOTE(anya_ritika @ 15 Oct, 2008 - 10:43 PM)

thanx for the help...but there is a problem..
when i run the program : say for the string being...
"Omnia mutantur"
i get an encrypted output is not:
124094093090082019094070071082093071070065
which is the expected output...
how can i solve this problem..
???help me soon..please..need to submit tonight...
I wrote, compiled, and use this in Dev-C++.
- main.cppCODE
/*
Name: Encryptor
Copyright: 2008, September 17th -- Wednesday
Author: Danielle
Date: 18/09/08 02:55
Description: Program encrypts/decrypts a message stored within a file
*/
#include <iostream>
#include <fstream>
#include <stdio.h> // included specifically for _flushall()
using namespace std;
#include "main.h"
int main()
{
char *Filename = new char[20];
int ans;
while (ans != 4) {
cout << "1 = Encrypt message to file" << endl
<< "2 = Encrypt file" << endl
<< "3 = Decrypt file" << endl
<< "4 = Quit" << endl << endl
<< "Encrypt or decrypt? ";
cin >> ans;
switch (ans) {
case 1 :
cout << "Please type a filename to encrypt a MESSAGE to: ";
cin >> Filename;
_flushall();
EncryptMessage(Filename);
system("PAUSE");
system("CLS");
break;
case 2 :
cout << "Please type a FILENAME to encrypt: ";
cin >> Filename;
_flushall();
EncryptFile(Filename);
system("PAUSE");
system("CLS");
break;
case 3 :
cout << "Please type a FILENAME to decrypt the message: ";
cin >> Filename;
_flushall();
Decrypt(Filename);
system("PAUSE");
system("CLS");
break;
case 4 :
delete(Filename);
exit(0);
break;
default : system("CLS"); cout << "Incorrect input entered!" << endl << endl;
}
}
cout << endl;
system("PAUSE");
system("CLS");
delete(Filename);
return EXIT_SUCCESS;
}
- main.hCODE
#ifndef MAIN_H
#define MAIN_H
struct Vars {
char EncryptFile[20001]; // Encrypt fi)le
char message[20001]; // Encrypt message
char Decrypt[20001]; // Decrypt message
long int Encrypt; // Encryption-key
int n; // Counter
} Key;
void EncryptMessage(char *Filename) {
Key.n = 0;
ofstream out;
// Open the file to write to it
out.open(Filename);
if (out.fail()) {
cout << "Couldn't create the file\n\n";
return;
}
cout << "Enter message to encrypt: ";
cin.getline(Key.message, 20001), '\n';
cout << "Enter key to encrypt with: ";
cin >> Key.Encrypt;
cout << "<<---------------------------->>" << endl << endl;
// Loop to encrypt message -- XOR
while (Key.message[Key.n] != '\0') {
Key.message[Key.n] ^= Key.Encrypt;
Key.n++;
}
out << Key.message;
out.close();
// Open the file to write to it
out.open("Key.txt");
if (out.fail()) {
cout << "Couldn't create the file\n\n";
return;
}
out << "Filename: " << Filename << endl;
out << "Encryption key: " << Key.Encrypt << endl;
out.close();
return;
}
void Decrypt(char *Filename) {
Key.n = 0;
char FileCharacter;
fstream FileMod;
FileMod.open(Filename, ios_base::in);
if (!FileMod.is_open()) {
cout << "\nFile not found.\n\n";
return;
}
cout << "Enter key to decrypt message with: ";
cin >> Key.Encrypt;
cout << "<<---------------------------->>" << endl << endl;
while ( (FileCharacter = FileMod.peek()) != EOF) {
FileMod.get(Key.Decrypt[Key.n]);
Key.n++;
}
FileMod.close();
Key.n = 0;
// Loop to dencrypt message -- XOR
while (Key.Decrypt[Key.n] != '\0') {
Key.Decrypt[Key.n] ^= Key.Encrypt;
Key.n++;
}
cout << Key.Decrypt;
cout << endl << endl << "<<---------------------------->>" << endl << endl;
return;
}
void EncryptFile(char *Filename) {
Key.n = 0;
char FileCharacter;
ifstream inFile;
ofstream outFile;
inFile.open(Filename, ios_base::in);
if (!inFile.is_open()) {
cout << "\nFile not found.\n\n";
return;
}
cout << "Enter key to encrypt FILE with: ";
cin >> Key.Encrypt;
cout << "<<---------------------------->>" << endl << endl;
// Read file into Key.EncryptFile
while ( (FileCharacter = inFile.peek()) != EOF) {
inFile.get(Key.EncryptFile[Key.n]);
Key.n++;
}
inFile.close();
Key.n = 0;
// Loop to dencrypt message -- XOR
while (Key.EncryptFile[Key.n] != '\0') {
Key.EncryptFile[Key.n] ^= Key.Encrypt;
Key.n++;
}
outFile.open(Filename, ios_base::in);
if (!outFile.is_open()) {
cout << "\nFile not created.\n\n";
return;
}
outFile << Key.EncryptFile;
outFile.close();
/*
// Open the file to write to it
outFile.open("Key.txt");
if (outFile.fail()) {
cout << "Couldn't create the file\n\n";
return;
}
outFile << "Filename: " << Filename << endl;
outFile << "Encryption key: " << Key.Encrypt << endl;
outFile.close();
*/
return;
}
#endif
I hope it helps, perhaps you can extract your answer from it.
I'm not the best of programmers I don't think, but at least it works and isn't 'all over the place.'
I'm not a fan of OOP, I'm unsure if you were required/suggested to do it OOP-style.
Good luck with your homeowrk!