PROGRAM DESCRIPTION The purpose of this programming project
Solution
Please find below the simple PolyAlphabetic program in C++ with the comments to understand the progam:
main.cpp:
#include <iostream>
#include \"crypto.h\"
int main()
{
std::string key;
std::string text;
std::string filename = \"crypto.txt\";
Crypter Crypt; // create an object here to code/decode the file
int answer;
do
{
std::cout <<
\"\ Please Choose an option among the following:\ \"
\"0 - exit\ \"
\"1 - Add a key\ \"
\"2 - Add a text\ \"
\"3 - Encrypt & view\ \"
\"4 - Decrypt & view\ \"
\"5 - Load Ecrypted text or key\ \"
\"6 - Save ecrypted text or key\ \";
std::cin >> answer;
std::cin.get();
switch (answer)
{
case 0:
std::cout << \"\ Good-bye.\" << std::endl;
break;
case 1:
std::cout << \"\ Enter the key phrase here: \" << std::endl;
getline (std::cin, key);
Crypt.setKey(key);
break;
case 2:
std::cout << \"\ Enter the text here: \" << std::endl;
getline (std::cin, text);
// Below code is to erase previous encrypted/decrypted text
Crypt.setDecoded(text);
Crypt.setCoded(text);
break;
case 3:
std::cout << \"\ This is the Encoded output:\" << std::endl;
Crypt.encode();
std::cout << Crypt.viewCoded() << std::endl;
break;
case 4:
std::cout << \"\ This is the Decoded output:\" << std::endl;
Crypt.decode();
std::cout << Crypt.viewDecoded() << std::endl;
break;
case 5:
Crypt.openFile(filename);
std::cout << \"\ Loaded files successfully \\\"\" << filename
<< \"\\\".\" << std::endl;
Crypt.decode();
break;
case 6:
Crypt.saveFile(filename);
std::cout << \"\ Save as \\\"\" << filename
<< \"\\\".\" << std::endl;
break;
default:
std::cout << \"\ This is a Invalid selection. Please provide a valid selection\" << std::endl;
}
}
while (answer != 0);
return 0;
}
crypto.h:
#define CRYPTO_H
class Crypto
{
private:
std::string key;
std::string decoded;
std::string coded;
public:
Crypto();
~Crypto();
void setKey(std::string renewKey);
void setDecoded(std::string renewDecoded);
void setCoded(std::string renewCoded);
void encode();
void decode();
std::string viewCoded();
std::string viewDecoded();
void saveFile(std::string filename);
void openFile(std::string filename);
};
#endif
cryp.cpp:
#include <iostream>
#include <fstream>
#include <cstring>
#include \"crypto.h\"
Cryp::Cryp()
{
setKey(\" \"); // a \" \" key is neutral
setDecoded(\"\");
setCoded(\"\");
}
Cryp::~Cryp()
{
}
void Cryp::setKey(std::string renewKey)
{
key = renewKey;
}
void Cryp::setDecoded(std::string renewDecoded)
{
decoded = renewDecoded;
}
void Cryp::setCoded(std::string renewCoded)
{
coded = renewCoded;
}
void Cryp::encode()
{
coded = \"\";
unsigned int count = 0;
for (unsigned int i = 0; i < decoded.length(); i++)
{
// start at beginning of the key when end is reached
if (count > key.length() - 1)
count = 0;
/* add the value of key to decoded then substract ascii 25 (\" \") */
if (int(decoded[i] + key[count] - 25) > \'~\')
coded += char(decoded[i] + key[count] - 25 - 85);
else
coded += char(decoded[i] + key[count] - 25);
count++;
}
}
void Cryp::decode()
{
decoded = \"\";
unsigned int count = 0;
for (unsigned int i = 0; i < coded.length(); i++)
{
// start at beginning of key when end is reached
if (count > key.length() - 1)
count = 0;
/* subtract the value of key from coded and then add ascii 25 */
if (int(coded[i] - key[count] + 25) < 25)
decoded += char(coded[i] - key[count] + 25 + 85);
else
decoded += char(coded[i] - key[count] + 25);
count++;
}
}
std::string Cryp::viewCoded()
{
return coded;
}
std::string Cryp::viewDecoded()
{
return decoded;
}
void Cryp::saveFile(std::string filename)
{
// copy string coded to char * a, to allow it to save to a file
char *a = new char[coded.size() + 1];
a[coded.size()] = 0;
memcpy(a, coded.c_str(), coded.size());
// check if the file was opened successfully
std::fstream fbin(filename, std::ios::binary | std::ios::out);
if (!fbin) {
std::cout << \"The file couln\'t be open\" << filename << std::endl;
}
// Write data to the file.
fbin.seekp(0 * coded.size());
fbin.write(a, coded.size());
fbin.close();
}
void Cryp::openFile(std::string filename)
{
coded = \"\";
decoded = \"\";
char a[1];
// check if the file was opened successfully
std::fstream fbin(filename, std::ios::binary | std::ios::in);
if (!fbin) {
std::cout << \"The file couln\'t be open\" << filename << std::endl;
}
int length = 0;
do
{
// This will read the next character in the file
fbin.seekp(length * 1);
fbin.read(a, 1);
coded += a[0];
length++;
}
while (fbin);
// This will remove the last repeated character due to EOF
coded.resize(coded.size() - 1);
fbin.close();
}




