/*************************************************************************
File:		encrypt.cpp
Modules:	main()
Function:	Implement One Time Pad from the textbook
Programmer:	Josif Kurunczi
Date:		September 10, 2007
*************************************************************************/
/*************************************************************************
Function:	main()
Purpose:	Take a string of PLAINTEXT and a KEY
Input:		Two strings defined and initialized
Ouptut:		Encrypted CIPHERTEXT based on the PLAINTEXT and the KEY
*************************************************************************/
#include 
#include 

using namespace std;

#define EXIT "Use the Enter key to exit the program.";

// Declares the encrypt file

ofstream encrypt("D:\\encrypt.txt");

void main()
{
	// declare ALL variables and initialize them for easier use	

	int		i, j;		// counting variables used in for loop
	
	char	alphabet[]			= {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
	int		values_alphabet[]	= {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,0};

	char	plaintext[]	= {'E','L','V','I','S','I','S','A','L','I','V','E','\0'};
	int		values_plaintext[13];

	char	key[]		= {'D','N','O','Y','Z','W','L','F','E','D','S','L','\0'};
	int		values_key[13];
	
	char	ciphertext[13];	
	int		values_ciphertext[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
	

	// calculate the values of the PLAINTEXT
	for (i=0;i<12;i++)
		for (j=0;j<26;j++)
			if (plaintext[i] != alphabet[j])
				;
			else
				values_plaintext[i] = values_alphabet[j];

	// Print to screen and to file the PLAINTEXT
	cout	<< "\n" << "PLAINTEXT given: ";
	encrypt << "\n" << "PLAINTEXT given: ";
	
	for (i=0;i<12;i++)
	{
		cout	<< " " << plaintext[i];
		encrypt << " " << plaintext[i];
	}

	// Print to screen and to file the values of the PLAINTEXT
	cout	<< "\n\n" << "Values of PLAINTEXT: ";
	encrypt << "\n\n" << "Values of PLAINTEXT: ";	

	for (i=0;i<12;i++)
	{
		cout	<< " " << values_plaintext[i];
		encrypt << " " << values_plaintext[i];
	}

	// calculate the values of the KEY
	for (i=0;i<12;i++)
		for (j=0;j<26;j++)
			if (key[i] != alphabet[j])
				;
			else
				values_key[i] = values_alphabet[j];
	
	// Print to screen and to file the KEY given
	cout	<< "\n\n" << "KEY given: ";
	encrypt << "\n\n" << "KEY given: ";
	
	for (i=0;i<12;i++)
	{
		cout	<< " " << key[i];
		encrypt << " " << key[i];
	}

	// Print to screen and to file the values of the KEY
	cout	<< "\n\n" << "Values of KEY: ";
	encrypt << "\n\n" << "Values of KEY: ";

	for (i=0;i<12;i++)
	{
		cout	<< " " << values_key[i];
		encrypt << " " << values_key[i];

	}

	// calculate the ciphertext values
	for (i=0;i<12;i++)
	{
		values_ciphertext[i] = ( values_plaintext[i] + values_key[i] ) % 26;
		if (values_ciphertext[i] == 0)
			values_ciphertext[i] = 26;
	}

	// Print to screen and to file the values of the CIPHERTEXT
	cout	<< "\n\n" << "Values of CIPHERTEXT: ";
	encrypt << "\n\n" << "Values of CIPHERTEXT: ";

	for (i=0;i<12;i++)
	{
		cout << " " << values_ciphertext[i];
		encrypt << " " << values_ciphertext[i];
	}

	// convert the RESULTS to the encrypted alphabet letters of the CIPHERTEXT
	for (i=0;i<12;i++)
		for (j=0;j<26;j++)
			if (values_ciphertext[i] != values_alphabet[j])
				;
			else
				ciphertext[i] = alphabet[j];

	// Print to screen and to file the encrypted CIPHERTEXT
	cout	<< "\n\n" << "CIPHERTEXT encrypted: ";
	encrypt << "\n\n" << "CIPHERTEXT encrypted: ";

	for (i=0;i<12;i++)
	{
		cout	<< " " << ciphertext[i];
		encrypt << " " << ciphertext[i];
	}

	cout << endl;
	cin.ignore();			// this allows the window to stay open 
	cout << EXIT;			// must press Enter to exit the program
	cout << endl;

}


    Source: geocities.com/jkurunczi