1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstring>
#include <fstream>
using namespace std;
void codex(const string buffer, const char map[], int length, string& buffer2)
{
char letter;
for (int i = 0; i < buffer.length(); i++)
{
if (isupper(buffer[i]))
{
letter = buffer[i] - 'A';
letter += map[i % length];
letter = letter + 'A';
buffer2.push_back(letter);
}
else if (islower(buffer[i]))
{
letter = buffer[i] - 'a';
letter += map[i % length];
letter = letter + 'a';
buffer2.push_back(letter);
}
else
{
buffer2.push_back(buffer[i]);
}
}
}
void encrypt(string encryptKey, char map[])
{
for ( int i = 0; i < encryptKey.length(); i++)
map[i] = encryptKey[i] - 'a';
}
void decrypt(string decryptKey, char map[])
{
for (int i = 0; i < decryptKey.length(); i++)
map[i] = decryptKey[i] + 'a';
}
void main()
{
string end[] = {"encrypt", "decrypt"},
ekey,
buffer,
buffer2;
char file[100],
enmap[128],
demap[128],
repeat = 'y';
int maplength,
answer;
ifstream infile;
ofstream outfile;
do
{
cout << "Would you like to encrypt or decrypt? (1 or 2) ";
cin >> answer;
while (answer != 1 && answer != 2)
{
cout << "\n\n1 or 2: ";
cin >> answer;
}
cout << "\n\nEnter the name of the file you wish to " << end[answer - 1] << ".";
cout << "\n\n";
cin >> file;
infile.open(file);
while (!infile.is_open())
{
cout << "\n\nThe file could not be opened successfully. Please try again.";
cin >> file;
infile.open(file);
}
cout << "\n\nEnter the name of your output file: ";
cout << endl;
cin >> file;
outfile.open(file);
cout << "\n\nEnter your encrypion key (128 characters max): " << "\n\n";
cin >> ekey;
while (ekey.length() > 128)
{
cout << "\n\nYour key is too long, enter another one: " << "\n\n";
cin >> ekey;
}
maplength = ekey.length();
if (answer == 1)
{
encrypt(ekey, enmap);
}
else
{
decrypt(ekey, demap);
}
while (infile)
{
getline(infile, buffer);
if (answer == 1)
{
codex(buffer, enmap, maplength, buffer2);
}
else
{
codex(buffer, demap, maplength, buffer2);
}
}
outfile.close();
outfile.clear();
infile.close();
infile.clear();
buffer2.erase(0);
cout << "\n\nWould you like to run the application again? (y or n): ";
cin >> repeat;
}
while (repeat == 'y' || repeat == 'Y');
}
|