/* 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: timh a5 2z ic44eqx35 59smc8. original text: the answer to the question is 42. seed: 42000 encrypted text: pma fjxsjn yk ydj mzaxpnks ex 07.. original text: we are the knights who say "ni!" seed: 283842 encrypted text: ex wyo ofx 6usbfce 3rj qtk "us!" programmed by david bibb */ #include // for cout and cin #include // for file streams #include // for system() #include // for isalnum() // function gets file name from user // input & output: char array fn void get_file(char fn[20]){ cin.ignore(80, '\n'); cout << "Enter filename: "; cin.get(fn, 20); cin.ignore(80, '\n'); } // 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; 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 buffer, fn[20]; int ch, seed, orig; get_file(fn); // 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(); orig = 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; // modify the seed seed = ((++seed * seed) + orig) % 36; 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]; get_file(fn); 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 << endl << "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); cout << "encryption successful. check your results." << endl; break; case 2: cypher(-1); cout << "decryption successful. 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; }