Help with Encryption and Decryption

Hi all. Was trying to put together a code in response to this project:

Write a CPP program that will print a menu of 3 choices:

1 for ENCRYPTION
2 for DECRYPTION
3 Exit

If ENC your program will ask for a password, an input file name and an output file name. Your program will read the file and mesh it with the password resulting in an encrypted file. Write this file out.

If DEC your program will ask for the password and the encrypted file name. Read the file and perform a decryption, printing the decryption on the screen

If EXIT program will terminate.

Our course has shown you how to write a file ( Back in the beginning when a lot of output needs to be printed) and how to read a file (project 3).

Clue: Create the program base first ( menu, reading a file, writing a file) and once this works proceed to insert the instructions that are necessary for the Enc /Dec...

Please document your program and use functions. Send me the documented source code, the original file, the encrypted file, and a copy of the decrypted results.

I have written what I believe to be pretty far besides a few mistakes. The program can run, but I am just in need of a few pointers of how to save the file correctly and how to open the file and decrypt it correctly. Thanks!

#include <iostream>
#include <fstream>

using namespace std;

void ans1(), ans2(), ans3();
int main()
{
int choice;
cout << "\tPassword Encryption Program\n\n";
cout << "1 for ENCRYPTION\n";
cout << "2 for DECRYPTION\n";
cout << "3 to EXIT PROGRAM\n";
cin >> choice;

switch (choice)
{
case 1:
ans1();
break;
case 2:
ans2();
break;
case 3:
ans3();
break;
default:
cout << "This choice is not available\n";
break;
}
return 0;
}

void ans1()
{
char pass[20], filein[30], encrypted[30];
// e below indicates a text file on the micro (can be changed)
const char* filename=(filein);
ifstream infile(filename);
if (!infile) cout << "no input file" << endl;

int i,j,x,y;
cout << "Please enter a password\n";
cin >> pass;
cout << "Please enter a desired input filename to encrypt, in the form driveletter:\subfolder\file.txt\n";
cin >> filein;
cout << "Please enter a desired output filename for the final ecrypted file in\n";
cout << "the form driveletter:\subfolder\file.txt\n";

cin >> encrypted;

infile>>filein;

x=strlen(filein);
y=strlen(pass);

//save as new file
ofstream outfile;
outfile<<encrypted;
outfile.open(encrypted);
j=0;
for (i=0; i<x; i++,j++)
{ if (j>=y) j=0; // Why this instruction ?
filein[i]=filein[i]+pass[j];
cout <<endl << filein;
}


j=0;
for (i=0; i<x; i++)
{ if (j>=y) j=0;
filein[i]=filein[i]-pass[j];
}
cout << endl << filein;

cout << endl <<"Enc is done";
}
void ans2()
{
char pass[20];
char encrypted[30];
cout << "Please enter the file you wish to decrypt in the form 'driveletter:\sublevel\file.txt'\n";
cin >> encrypted;
// e below indicates a text file on the micro (can be changed)
const char* filename=(encrypted);
ifstream infile(filename);
if (!infile) cout<<"no input file"<<endl;
if (infile) cout << "please enter password to decrypt file"
{
cin >> pass;
}
void ans3()
{
}
This may be better to see after a few changes

#include <iostream>
#include <fstream>

using namespace std;

void ans1(), ans2(), ans3();
int main()
{
int choice;
cout << "\tPassword Encryption Program\n\n";
cout << "1 for ENCRYPTION\n";
cout << "2 for DECRYPTION\n";
cout << "3 to EXIT PROGRAM\n";
cin >> choice;

switch (choice)
{
case 1:
ans1();
break;
case 2:
ans2();
break;
case 3:
ans3();
break;
default:
cout << "This choice is not available\n";
break;
}
return 0;
}

void ans1()
{
char pass[20], filein[30], encrypted[30];
// e below indicates a text file on the micro (can be changed)
const char* filename=(filein);
ifstream infile(filename);
if (!infile) cout << "no input file" << endl;

int i,j,x,y;
cout << "Please enter a password\n";
cin >> pass;
cout << "Please enter a desired input filename to encrypt, in the form driveletter:\subfolder\file.txt\n";
cin >> filein;
cout << "Please enter a desired output filename for the final ecrypted file in\n";
cout << "the form driveletter:\subfolder\file.txt\n";

cin >> encrypted;

infile>>filein;

x=strlen(filein);
y=strlen(pass);


j=0;
for (i=0; i<x; i++,j++)
{ if (j>=y) j=0; // Why this instruction ?
filein[i]=filein[i]+pass[j];
cout <<endl << filein;
j=0;
for (i=0; i<x; i++)
{ if (j>=y) j=0;
filein[i]=filein[i]-pass[j];
//save as new file
ofstream outfile;
outfile<<encrypted;
outfile.open(encrypted);
}
cout << endl << filein;

cout << endl <<"Enc is done";
}
void ans2()
{
int i,j,x,y;
char pass[20];
char encrypted[30];
cout << "Please enter the file you wish to decrypt in the form 'driveletter:\sublevel\file.txt'\n";
cin >> encrypted;
cout << "Please enter the password to the file\n";
cin >> pass;
// e below indicates a text file on the micro (can be changed)
const char* filename=(encrypted);
ifstream infile(filename);
if (!infile) cout<<"no input file"<<endl;

x=strlen(encrypted);
y=strlen(pass);


j=0;
for (i=0; i<x; i++,j++)
{ if (j>=y) j=0; // Why this instruction ?
filein[i]=filein[i]-pass[j];
cout <<endl << filein;
j=0;
for (i=0; i<x; i++)
{ if (j>=y) j=0;
filein[i]=filein[i]+pass[j];
}
cout<<endl<<a;

cout<<endl<<"Decryption is done";
}
void ans3()
{
}
Topic archived. No new replies allowed.