All pastes #784888 Raw Edit

Something

public cpp v1 · immutable
·published 2007-11-19 22:07 UTC
rendered paste body
/*program gives user choice of encrypting, decrypting,and printing a file to the screen.  encrypting anddecrypting are virtually opposites, so only onefunction was actually needed.  my encryption algorithmis 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 charis, and then adds a seed that is technically userdefined, this ensures the difficulty to manuallydecrypt, and the safety of the encrypted text.decryption subtracts the seed, which must be given,to get the original text.  it is possible to havemultiple level encryption, and it will work from anyorder decrypted.original text: this is my encrypted string.seed: 12345encrypted text: timh a5 2z ic44eqx35 59smc8.original text: the answer to the question is 42.seed: 42000encrypted text: pma fjxsjn yk ydj mzaxpnks ex 07..original text: we are the knights who say "ni!"seed: 283842encrypted text: ex wyo ofx 6usbfce 3rj qtk "us!"programmed by david bibb*/#include <iostream.h>  // for cout and cin#include <fstream.h> // for file streams#include <stdlib.h> // for system()#include <ctype.h> // for isalnum()// function gets file name from user// input & output: char array fnvoid 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 arraysvoid 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 < 36int 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 consolevoid 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 choiceint 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 finishedbool 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 explanitoryint main(){	int choice;	do choice = menu();	while(handle(choice));	return 0;}