Created
January 15, 2013 04:58
-
-
Save CameronNorth/4536224 to your computer and use it in GitHub Desktop.
A simple ROT13 encryption program for you to critique.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <fstream> | |
#include <string> | |
#include <stdlib.h> | |
std::string getfile(int x); | |
std::string outputfile(std::string a); | |
std::string crypt(std::string a); | |
int main(int argc, char const *argv[]) | |
{ | |
std::string input = "", output = "", pause = ""; | |
int x=0; | |
input = getfile(x); //Input = user entered file name | |
if(input == "") //Check if input is empty | |
{ | |
std::cout << "Invalid text, exiting program."; | |
std::cin; | |
return 0; | |
} | |
output = crypt(input); //crypt input information | |
if(output == "") //Check if encrypted text is empty | |
{ | |
std::cout << "Problem encrypting text, closing."; | |
std::cin; | |
return 0; | |
} | |
else | |
outputfile(output); | |
std::cout << "\nEnter any text to close the program.\n"; | |
std::cin >> pause; | |
return 0; | |
} | |
std::string getfile(int x) | |
{ | |
std::string orig="", again=""; //string declarations | |
std::cout << "Please enter an existing filename (Example.txt): "; | |
std::cin >> orig; //input file name | |
std::fstream input; //file stream | |
input.open(orig.c_str()); //open input file name | |
if(input.is_open()) // Check if file is open | |
std::cout << "File opened succesfully.\n"; | |
else | |
{ | |
std::cout << "File cannot be opened, would you like to try again?(Y/N)\n"; | |
std::cin >> again; // Ask to enter another file name if first failed | |
if(again == "y") | |
{ | |
getfile(x); | |
exit; | |
} | |
else | |
exit; | |
} | |
std::getline(input, orig); | |
std::cout << "\nOriginal text reads:\n" << orig << "\n\n"; | |
input.close(); // Close file stream | |
return orig; // Return file name | |
} | |
std::string outputfile(std::string a) | |
{ | |
std::string ret=""; | |
std::fstream output; | |
output.open("Crypted.txt", std::fstream::out); //Open file with title "Crypted.txt" | |
if(output.is_open()) //Check if file was opened | |
std::cout << "Output file opened succesfully with the filename \"Crypted.txt\"\n"; | |
else | |
{ | |
std::cout << "Output file not opened succesfully, exiting the program.\n"; | |
std::cin >> ret; | |
} | |
output << a; //Output encrypted string to "Crypted.txt" | |
output.close(); //Close output file stream | |
return a; | |
} | |
std::string crypt(std::string a) | |
{ | |
int z = a.length(), i=0; | |
for(i=0; i<=(z); i++) //Rot13 Algorithm | |
{ | |
if(a[i] < 91 && a[i] > 64) //uppercase | |
{ | |
if(a[i] < 78) | |
a[i] = a[i] + 13; | |
else | |
a[i] = a[i] - 13; | |
} | |
if(a[i] < 123 && a[i] > 96) //lowercase | |
{ | |
if(a[i] < 110) | |
a[i] = a[i] + 13; | |
else | |
a[i] = a[i] - 13; | |
} | |
} | |
return a; //return encrypted string | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment