0% found this document useful (0 votes)
3 views

Code Review

The document contains a C++ program that implements a simple encryption and decryption algorithm. Users can choose to encrypt or decrypt a file by providing a filename and a key, with the results saved to new files. The program handles file input/output and character manipulation based on the provided key for both encryption and decryption processes.

Uploaded by

coconutcweampie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Code Review

The document contains a C++ program that implements a simple encryption and decryption algorithm. Users can choose to encrypt or decrypt a file by providing a filename and a key, with the results saved to new files. The program handles file input/output and character manipulation based on the provided key for both encryption and decryption processes.

Uploaded by

coconutcweampie
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream>

#include <fstream>
#include <string>
#include <map>
using namespace std;

class algorithm {
fstream fileIn;
string fileName;
string newFileName;
string message;
bool restart;
char chars;
int key;

public:
void encrypt();
void decrypt();
};

void algorithm::encrypt(){
restart = false;
cout <<"-Encryption Chosen-" <<endl;
cout <<"Enter name of the file you want to encrypt:";

do {
restart = false;
getline(cin, fileName);
fileIn.open(fileName);

if(!fileIn){
cout <<"File could not be found" <<endl;
cout <<"Re-enter file name:";
restart = true;
}
}while(restart);

cout <<"File " <<"\"" <<fileName <<"\"" <<" SUCCESSFULLY FOUND"


<<endl;
cout <<"Enter key:";
cin >>key;
cin.ignore();

while(fileIn.get(chars)){
message += chars;
}

for(int i =0; i <message.length(); i++){


chars = message[i];
chars += key;
message[i] = chars;

if(message[i] >=127){
message[i] -=94;
}
else if(message[i] ==19){
message[i] = '\n';
}

//write to file
newFileName = "encrypted-file.txt";
ofstream encryptedFile(newFileName);
encryptedFile <<message;
encryptedFile.close();
cout <<"-ENCRYPTION SUCCESSFUL-"<<endl;
cout <<"Look for " << newFileName<< " in the directory" <<endl;
}

void algorithm::decrypt(){
cout <<"-Decryption Chosen-" <<endl;
cout <<"Enter the name of the file you want to decrypt:";

do {
restart = false;
getline(cin, fileName);
fileIn.open(fileName);

if(!fileIn){
cout <<"File could not be found" <<endl;
cout <<"Re-enter file name:";
restart = true;
}

}while(restart);

cout <<"Enter key:";


cin >>key;
cin.ignore();

while(fileIn.get(chars)){
message += chars;
}

for(int i =0; i <message.length(); i++){


chars = message[i];
chars -= key;
message[i] = chars;

if(message[i] <32){
message[i] +=94;
}
}

//write to file
newFileName = "decrypted-file.txt";
ofstream decryptedFile(newFileName);
decryptedFile <<message;
decryptedFile.close();
cout <<"-DECRYPTION SUCCESSFUL-"<<endl;
cout <<"Look for " << newFileName<< " in the directory" <<endl;
}

int main()
{
algorithm alg;
string choice;
bool reset =false;

cout <<"===============Encryption
Algorithm===============" <<endl;
cout <<"Press E to encrypt:\n";
cout <<"Press D to decrypt:" <<endl;
getline(cin, choice);

do{
reset = false;
switch (choice[0])
{
case 'E':
alg.encrypt();
break;

case 'e':
alg.encrypt();
break;

case 'D':
alg.decrypt();
break;

case 'd':
alg.decrypt();
break;

default:
cout <<"Invalid input press E or D:" <<endl;
getline(cin, choice);
reset = true;
break;
}
}while(reset);
return 0;
}

You might also like