/* program gives user choice of encrypting, decrypting, and printing a file to the screen. encrypting and decrypting are virtually opposites, so only one function was actually needed. my encryption algorithm is a modified rot18 obscurity algorithm. rot18 (rotate) cycles through the lowercase letters and all digits. my algorithm adds 18 to the value of whatever the char is, and then adds a seed that is technically user defined, this ensures the difficulty to manually decrypt, and the safety of the encrypted text. decryption subtracts the seed, which must be given, to get the original text. it is possible to have multiple level encryption, and it will work from any order decrypted. original text: this is my encrypted string. seed: 12345 encrypted text: tikv mx s5 mwm2a27tt 9ba282. original text: the answer to the question is 42. seed: 42000 encrypted text: pec 9ntyhv yu 0pn 05q57x44 0b on. original text: we are the knights who say "ni!" seed: 283842 encrypted text: ex uc0 g53 aea9boo tfn sb0 "qm!" programmed by david bibb */ #include // for cout and cin #include // for file streams #include // for system() #include // for isalnum() // function copies one file to another // input: sfile and tfile character arrays void copy_file(char sfile[20], char tfile[20]){ ifstream ifile; ofstream ofile; char temp; ifile.open(sfile, ios::in); ifile.unsetf(ios::skipws); ofile.open(tfile, ios::out); ifile >> temp; while(!ifile.eof()){ ofile << temp; ifile >> temp; } ifile.close(); ofile.close(); } // function gets seed for cypher() // output: int seed, 0 <= seed < 36 int get_seed(){ unsigned int seed; cout << "enter a number with five or more digits: "; cin >> seed; // semi-random way of getting a seed while(seed >= 36) seed = (seed / 36) % 36; //cout << seed << endl; return int(seed); } // function handles the text from target files // input: int dir (1 for encrypt, -1 for decrypt) void cypher(int dir){ // custom character set char salt[] = "0123456789abcdefghijklmnopqrstuvwxyz"; char fn[20], buffer; int ch, seed; // get filename cin.ignore(80, '\n'); cout << "Enter filename: "; cin.get(fn, 20); cin.ignore(80, '\n'); // open files ifstream ifile; ifile.open(fn, ios::in); ifile.unsetf(ios::skipws); ofstream ofile; ofile.open("temp", ios::out); // call to function get_seed seed = get_seed(); ifile >> buffer; while(!ifile.eof()){ ch = int(buffer); if(isalnum(buffer)){ // get rid of caps if(ch >= 65 && ch <= 90) ch = int(buffer) + 32; // reset numbers to below alphabet in ascii else if(ch >= 48 && ch <= 57) ch = int(buffer) + 39; // subtract 87 to make int('0') = 0 ch = ch - 87; // add 18 (rot18) and the seed, depending on direction ch = ch + 18 + (seed * dir); // make sure ch is within bounds (0 and 35) while(ch > 35) ch = ch - 36; while(ch < 0) ch = ch + 36; seed++; ofile << salt[ch]; } else{ ofile << buffer; } ifile >> buffer; } ifile.close(); ofile.close(); // copy file, clear temp copy_file("temp", fn); ofile.open("temp", ios::out); ofile.close(); } // function prints a given file to the screen // output: file text to console void print_file(){ ifstream ifile; char buffer, fn[20]; cin.ignore(80, '\n'); cout << "enter file name: "; cin.get(fn, 20); cin.ignore(80, '\n'); ifile.open(fn, ios::in); ifile.unsetf(ios::skipws); ifile >> buffer; while(!ifile.eof()){ cout << buffer; ifile >> buffer; } ifile.close(); } // function prints out the menu, gets input // output: int choice int menu(){ int choice; cout << "0. exit" << endl << "1. encrypt a file" << endl << "2. decrypt a file" << endl << "3. print file to screen" << endl; cin >> choice; return choice; } // function handles menu choice, does appropriate action // input: int choice // output: boolean again, for main function to tell when finished bool handle(int choice){ bool again = true; system("cls"); switch(choice){ case 0: again = false; cout << "Thanks" << endl; break; case 1: cypher(1); system("cls"); cout << "encryption sucessful. check your results." << endl; break; case 2: cypher(-1); system("cls"); cout << "encryption sucessful. check your results." << endl; break; case 3: print_file(); break; } return again; } // main function. self explanitory int main(){ int choice; do choice = menu(); while(handle(choice)); return 0; }