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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
char substitution_cipher(string cipher_key, char char_to_encrypt);
char reverse_substitution_cipher(string cipher_key, char char_to_encrypt);
string EncryptString(string &cipher_key, string string_to_be_encrypted);
string DecryptString(string &cipher_key, string string_to_be_decrypted);
void RotateCipherKey(string &cipher_key);
void DisplayFile(string filename);
void EncryptFile(string cipher_key, string filename_from, string filename_to);
void DecryptFile(string cipher_key, string filename_from, string filename_to);
int main()
{
string cipher_key = "qwertyuiopasdfghjklzxcvbnm";
EncryptFile(cipher_key, "test.txt", "test-encrypted.txt");
DecryptFile(cipher_key, "test-encrypted.txt", "test-ed.txt");
DisplayFile("test.txt");
DisplayFile("test-encrypted.txt");
DisplayFile("test-ed.txt");
system("PAUSE");
return 0;
}
// Rotate the cipher key. Example: abcdef becames bcdefa
void RotateCipherKey(string &cipher_key)
{
rotate(cipher_key.begin(), cipher_key.begin() + 1, cipher_key.end());
}
// Perform a substitution cipher on a single character
// using the specified cipher key
char SubstitutionCipher(string cipher_key, char char_to_encrypt)
{
for (size_t iii = 0; iii < cipher_key.length(); iii++)
{
RotateCipherKey(cipher_key);
char_to_encrypt = cipher_key[iii];
}
return char_to_encrypt;
}
// Perform a "reverse" substitution cipher on a single character
// using the specified cipher key
char ReverseSubstitutionCipher(string cipher_key, char char_to_decrypt)
{
for (size_t iii = 0; iii < cipher_key.length(); iii++)
{
RotateCipherKey(cipher_key);
char_to_decrypt = cipher_key[iii];
}
return char_to_decrypt;
}
// Encrypt String and return it
// You will use the SubstitutionCipher() function to encrypt the
// individual characters
//
// Note: We will call RotateCipherKey() after each time we encrypt
// a character.
string EncryptString(string &cipher_key, string string_to_be_encrypted)
{
const char *y = string_to_be_encrypted.c_str();
{
//SubstitutionCipher(cipher_key, string_to_be_encrypted);
}
cout << " " << string_to_be_encrypted;
string encrypted_string = string_to_be_encrypted;
return encrypted_string;
}
// Decrypt String and return it
// You will use the ReverseSubstitutionCipher() function to decrypt the
// individual characters
//
// Note: We will call RotateCipherKey() after each time we encrypt
// a character.
string DecryptString(string &cipher_key, string string_to_be_decrypted)
{
string decrypted_string = string_to_be_decrypted;
return decrypted_string;
}
// Display file specified by the filname parameter
void DisplayFile(string filename)
{
string str;
ifstream infile;
infile.open(filename);
infile >> str;
while (infile)
{
cout << " " << str;
infile >> str;
}
cout << endl;
}
// Encrypt the specified file using the specified cipher key and
// write the output to a different file
// This function is complete
void EncryptFile(string cipher_key, string filename_from, string filename_to)
{
string input;
ifstream infile;
ofstream outfile;
infile.open(filename_from.c_str());
outfile.open(filename_to.c_str());
if (!infile)
{
cout << "Can not open input file " + filename_from << endl;
exit(0);
}
if (!outfile)
{
cout << "Can not open Output file " + filename_to << endl;
exit(0);
}
while (getline(infile, input))
{
outfile << EncryptString(cipher_key, input) << endl;
}
infile.close();
outfile.close();
}
// Decrypt the specified file using the specified cipher key and
// write the output to a different file
// This function is complete
void DecryptFile(string cipher_key, string filename_from, string filename_to)
{
string input;
ifstream infile;
ofstream outfile;
infile.open(filename_from.c_str());
outfile.open(filename_to.c_str());
if (!infile)
{
cout << "Can not open input file " + filename_from << endl;
exit(0);
}
if (!outfile)
{
cout << "Can not open Output file " + filename_to << endl;
exit(0);
}
while (getline(infile, input))
{
outfile << DecryptString(cipher_key, input) << endl;
}
infile.close();
outfile.close();
}
|