All pastes #777201 Raw Edit

Mine

public cpp v1 · immutable
·published 2007-11-16 16:54 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: tikv mx s5 mwm2a27tt 9ba282.original text: the answer to the question is 42.seed: 42000encrypted text: pec 9ntyhv yu 0pn 05q57x44 0b on.original text: we are the knights who say "ni!"seed: 283842encrypted text: ex uc0 g53 aea9boo tfn sb0 "qm!"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 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;	//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 consolevoid 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 choiceint 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 finishedbool 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 explanitoryint main(){	int choice;	do choice = menu();	while(handle(choice));	return 0;}