Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

we are going to be creating a basic encryption/decryption function. The principl

ID: 3676170 • Letter: W

Question

we are going to be creating a basic encryption/decryption function. The principle function we are writing will call an encrypt function and/or a decrypt function. Normally, I wouldn't spec the additional functions for you to write but for this lab I'm listing at least 3 functions you need to write. Our encryption is going to be basic. If a letter is simply going to be replaced with a different letter from the alphabet. Use this scheme when encrypting or decrypting:

a == z
b == y
c == x
d == w
e == v
f == u
g == t
h == s
i == r
j == q
k == p
l == o
m == n

So an 'a' will be come 'z' and a 'z' will become an 'a', a 'b' will become an 'y' and an 'y' will be come a 'b', and so on. We'll only be dealing with lower case letters. Any thing that isn't a lower case letter just gets "added" to the results string. So spaces stay spaces, punctuation stays punctuation, etc.

Here's a snippet of code you can use to start that process:

Requirements

You must write your method signatures in a file called: encrypt.h

You must write the principle function with signature: void encryptDecrypt( string inputfile, string outputfile );

You must write the "helper" function with signature: string encrypt( string input );

You must write the "helper" function with signature: string decrypt( string input );

Since my code needs to call your helper functions too, they must be declared in the header file too.

This is in c++, and I just need a basic code following these exact requirements.

Explanation / Answer

// encrypt.h

#ifndef ENCRYPT_H
#define ENCRYPT_H

#include <iostream>
using namespace std;

void encryptDecrypt(string inputfile, string outputfile);
string encrypt(string input);
string decrypt(string input);

#endif

#include <iostream>
#include "encrypt.h"
#include <fstream>
using namespace std;

void encryptDecrypt(string inputfile, string outputfile){
   ifstream inFile(inputfile.c_str());
   ofstream outFile(outputfile.c_str());
   string temp;
   while(inFile >> temp){
       outFile << encrypt(temp);
   }
}
string encrypt(string input){
   string x = input;
   for(int i = 0; i < input.size(); i++){
       if(input[i] >= 'a' && input[i] <= 'z'){
           int t = 25 - (input[i] - 'a');
           x[i] = 'a' + t;
       }
   }
   return x;
}
string decrypt(string input){
   string x = input;
   for(int i = 0; i < input.size(); i++){
       if(input[i] >= 'a' && input[i] <= 'z'){
           int t = 25 - (input[i] - 'a');
           x[i] = 'a' + t;
       }
       else{
           x = input[i];
       }
   }
   return x;
}