#include <iostream>
#include <strstream>
#include <fstream> //header file for file i/o, contains data types
// ifstream, ofstream, open, close
#include <string> //header file for string manipulation operations
using namespace std;
//The CharacterShift Function takes in the Key
//and the Cipher value (both chars)
//and decrypts them as per this projects
//request and it will also return a blank space
//if the cipher is a blank space
char CharacterShift(char Key, char Cipher);
string Decryption(string CipherArray, string KeyArray);
void IOFunction();
int main(){
IOFunction();
return 0;
}
char CharacterShift(char Key, char Cipher)
{
char output;
if(Cipher==' ')
{
return Cipher;
}
Key = Key - 65;
Cipher = Cipher - 65;
if( Key > Cipher)
{
output = 91+Cipher-Key;
}
else
{
output = 65 + (Cipher-Key);
}
return output;
}
string Decryption(string KeyArray, string CipherArray){
int TemporaryCount=0;
int i=0;
string New;
int CipherArrayLength=CipherArray.length();
int KeyArrayLength=KeyArray.length();
for(i;i<CipherArrayLength;i++){
if(CipherArray[i]=='?'){
New+='?';
}
if(CipherArray[i]=='\''){
New+='\'';
}
if(CipherArray[i]=='%'){
New+='\n';
New+='\n';
}
if(CipherArray[i]=='.'){
New+='.';
}
if(TemporaryCount>=KeyArrayLength){
TemporaryCount=0;
}
if(CipherArray[i]==' '){
New+=' ';
}
if(TemporaryCount<KeyArrayLength && CharacterShift(KeyArray[TemporaryCount],CipherArray[i])!=' '&&CipherArray[i]!='.' ){
New+=CharacterShift(KeyArray[TemporaryCount],CipherArray[i]);
TemporaryCount++;
}
}
return New;
}
void IOFunction(){
string junk,Key1,Cipher1,Key2,Cipher2;
string TotalText;
string myFileName; //variable to hold external file name
ifstream inFile; //internal file (stream) name
cout <<"Please enter your input file name:";
cin >> myFileName;
inFile.open(myFileName.c_str()); //convert to c_string and opens file
// if open does not work, inFile is in a “fail” state
while (!inFile) // therefore !inFile will be ”true”
{
inFile.clear(); //puts the file states back to “good” from bad open
cout << endl << "You entered an invalid file name. Please try again:";
cin >> myFileName;
inFile.open(myFileName.c_str());
}
while (inFile) //”true” if the open command executed properly
{
getline(inFile,junk,'@');
getline(inFile,Key1,'#');
getline(inFile,Cipher1,'@');
getline(inFile,Key2,'#');
getline(inFile,Cipher2,'@');
}
if(junk.length()>1){
cout<<"*** Error Code 2 -- No key given ***"<<endl;
}
inFile.close();
cout<< Decryption(Key1, Cipher1);
cout<< Decryption(Key2, Cipher2);
}