/************************************************************************* File: decrypt.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 the CIPHERTEXT Input: Two strings defined and initialized Ouptut: Decrypted KEY *************************************************************************/ #include#include using namespace std; #define EXIT "Use the Enter key to exit the program."; // Declares the decrypt file ofstream decrypt("D:\\decrypt.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[] = {'A','N','D','I','N','M','E','M','P','H','I','S','\0'}; int values_plaintext[13]; char ciphertext[] = {'D','N','O','Y','Z','W','L','F','E','D','S','L','\0'}; int values_ciphertext[13]; char key[13]; int values_key[13]; // 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: "; decrypt << "\n" << "PLAINTEXT given: "; for (i=0;i<12;i++) { cout << " " << plaintext[i]; decrypt << " " << plaintext[i]; } // Print to screen and to file the values of the PLAINTEXT cout << "\n\n" << "Values of PLAINTEXT: "; decrypt << "\n\n" << "Values of PLAINTEXT: "; for (i=0;i<12;i++) { cout << " " << values_plaintext[i]; decrypt << " " << values_plaintext[i]; } // calculate the values of the CIPHERTEXT for (i=0;i<12;i++) for (j=0;j<26;j++) if (ciphertext[i] != alphabet[j]) ; else values_ciphertext[i] = values_alphabet[j]; // Print to screen and to file the CIPHERTEXT given cout << "\n\n" << "CIPHERTEXT given: "; decrypt << "\n\n" << "CIPHERTEXT given: "; for (i=0;i<12;i++) { cout << " " << ciphertext[i]; decrypt << " " << ciphertext[i]; } // Print to screen and to file the values of the CIPHERTEXT cout << "\n\n" << "Values of CIPHERTEXT: "; decrypt << "\n\n" << "Values of CIPHERTEXT: "; for (i=0;i<12;i++) { cout << " " << values_ciphertext[i]; decrypt << " " << values_ciphertext[i]; } // calculate the KEY values for (i=0;i<12;i++) { values_key[i] = ( values_ciphertext[i] - values_plaintext[i] ) % 26; if (values_key[i] == 0) values_key[i] = 26; else if (values_key[i] < 0) values_key[i] = 26 - abs(values_key[i]); } // Print to screen and to file the values of the KEY cout << "\n\n" << "Values of KEY: "; decrypt << "\n\n" << "Values of KEY: "; for (i=0;i<12;i++) { cout << " " << values_key[i]; decrypt << " " << values_key[i]; } // convert the RESULTS to the decrypted alphabet letters of the KEY for (i=0;i<12;i++) for (j=0;j<26;j++) if (values_key[i] != values_alphabet[j]) ; else key[i] = alphabet[j]; // Print to screen and to file the decrypted KEY cout << "\n\n" << "KEY decrypted: "; decrypt << "\n\n" << "KEY decrypted: "; for (i=0;i<12;i++) { cout << " " << key[i]; decrypt << " " << key[i]; } cout << endl; cin.ignore(); // this allows the window to stay open cout << EXIT; // must press Enter to exit the program cout << endl; }