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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/* ============================================================================
Name: homework4.cpp
Author: Robert Ehrlish
Version:
Copyright: 2017
Description: This program encrypts and decrypts
============================================================================ */
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <fstream>
#include <iostream>
#include <string>
/* ============================================================================
Function: write my info
Parameters: none
Return: none void
Description: writes my info
============================================================================
*/
void outPut();
/* ============================================================================
Function: encrypt
Parameters: input and output
Return: none void
Description: encrypts plaintext
============================================================================
*/
void doEncrypt(std::ifstream& in_stream, std::ofstream& out_stream);
/* ============================================================================
Function: decrypt
Parameters: none
Return: none void
Description: decrypts ciphertext
============================================================================
*/
void doDecrypt(std::ifstream& in_stream, std::ofstream& out_stream);
void askForFileNames(char encdec, std::ifstream& in_stream,
std::ofstream& out_stream);
int main()
{
outPut();
std::cout << "Would you like to ENCRYPT or DECRYPT (E or D)? ";
std::string response;
std::cin >> response;
while (response.at(0) != 'e' && response.at(0) != 'E'
&& response.at(0) != 'd' && response.at(0) != 'D')
{
std::cout << "Would you like to ENCRYPT or DECRYPT (E or D)? ";
std::cin >> response;
}
std::ifstream in_stream;
std::ofstream out_stream;
askForFileNames(response.at(0), in_stream, out_stream);
if(response.at(0) == 'e' || response.at(0) == 'E') //if they want to encrypt
{
doEncrypt(in_stream, out_stream);
}
if(response.at(0) == 'd' || response.at(0) == 'D') //if they want to decrypt
{
doDecrypt(in_stream, out_stream);
}
out_stream.close();
in_stream.close();
return 0;
}
void doEncrypt(std::ifstream& in_stream, std::ofstream& out_stream)
{
std::cout << "YOU WANT TO ENCRYPT" << std::endl;
std::cout << "Enter the numerical key (i.e., an integer) used to encrypt: ";
int shiftvalue {0};
std::cin >> shiftvalue;
char orig_symbol {}, new_symbol {};
while(in_stream >> std::noskipws >> orig_symbol)
{
new_symbol = orig_symbol;
if(std::isupper(orig_symbol))
{
//65 is the offset of ASCII
new_symbol=((orig_symbol+shiftvalue - 65) % 26) + 65;
}
if(std::islower(orig_symbol))
{
//97 is the offset of ASCII
new_symbol=((orig_symbol+shiftvalue - 97) % 26) + 97;
}
if(std::isdigit(orig_symbol))
{
//48 is the offset of ASCII
new_symbol=((orig_symbol+shiftvalue - 48) % 10) + 48;
}
out_stream << new_symbol;
}
}
void doDecrypt(std::ifstream& in_stream, std::ofstream& out_stream)
{
std::cout << "YOU WANT TO DECRYPT" << std::endl;
std::cout << "Enter the numerical key (i.e., an integer) used to decrypt: ";
int shiftvalue = 0;
std::cin >> shiftvalue;
char orig_symbol {}, new_symbol {};
while(in_stream >> std::noskipws >> orig_symbol)
{
int tempvalue {};
new_symbol = orig_symbol;
if(isupper(orig_symbol))
{
if(shiftvalue >= 26)
{
tempvalue = shiftvalue % 26;
} else {
tempvalue = shiftvalue;
}
//65 is the offset of ASCII
new_symbol=((26 + orig_symbol-tempvalue - 65) % 26) + 65;
}
if(islower(orig_symbol))
{
if(shiftvalue >= 26)
{
tempvalue = shiftvalue % 26;
}else
{
tempvalue = shiftvalue;
}
//97 is the offset of ASCII
new_symbol=((26 +orig_symbol-tempvalue - 97) % 26) + 97;
}
if(isdigit(orig_symbol))
{
if(shiftvalue >= 10)
{
tempvalue = shiftvalue % 10;
}else
{
tempvalue = shiftvalue;
}
//48 is the offset of ASCII
new_symbol=((10 +orig_symbol-tempvalue - 48) % 10) + 48;
}
out_stream << new_symbol;
}
}
void outPut()
{
std::cout << "+---------------------------------------------------------+\n";
std::cout << "| Computer Science and Engineering |\n";
std::cout << "| CSCE 1030 - Computer Science I |\n";
std::cout << "| Robert Ehrlish rte0023 robertehrlish@my.unt.edu |\n";
std::cout << "+---------------------------------------------------------+\n";
}
void askForFileNames(char encdec, std::ifstream& in_stream,
std::ofstream& out_stream)
{
std::string input, output;
if(encdec == 'e') {
std::cout << "Enter the name of your input file you want to encrypt: ";
std::cin >> input;
std::cout << "Enter the name of the output file to write the ciphertext: ";
std::cin >> output;
} else {
std::cout << "Enter the name of your input file you want to decrypt: ";
std::cin >> input;
std::cout << "Enter the name of the output file to write the plaintext: ";
std::cin >> output;
}
in_stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
in_stream.open(input, std::ios_base::in);
} catch(...) {
std::cerr << "Exception opening file: \"" << input << "\""
<< std::strerror(errno) << '\n';
}
out_stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try {
out_stream.open(output, std::ios_base::out);
} catch(...) {
std::cerr << "Exception opening file: \"" << output << "\""
<< std::strerror(errno) << '\n';
}
}
|