260 likes | 378 Views
Programming. Array Application. kayak. Character Array: Ex. 1. Note: a palindrome is a word (or words) which read the same forward and backward kayak solos radar racecar deified evil olive Malayalam lonely Tylenol Re-divider Madam I'm Adam step on no pets won ton? not now.
E N D
Programming Array Application
kayak Character Array: Ex. 1 • Note: a palindrome is a word (or words) which read the same forward and backward • kayak • solos • radar • racecar • deified • evil olive • Malayalam • lonely Tylenol • Re-divider • Madam I'm Adam • step on no pets • won ton? not now Run Example 1 to find out which words the computer considers a palindrome
An Encoded Message • What does the following message represent? YMJ%HTRU657%HQFXX%NX%YTT%JFX^& Answer: THE COMP102 CLASS IS EASY! • Problem statement: Write a function that encodes a message (in English) using some encryption method, and another function that decodes the encoded message.
Offset Cipher • We use the simplest method, the “offset cipher”. • Define offset value, which will be used in both the encoding and decoding processes. • The encoding function shifts each character in the message higher by the offset amount.Example: if offset is 1, then a becomes b. • The decoding function shifts each character in the encoded message lower by the same offset amount.Example: if offset is 1, then b becomes a.
Ex. 2: Main Function int main() { char wait; //Temporary input control character // Print out original plain text dump("input.txt"); cout << endl << endl; cout << "Please press a key to encode this file:"; cin >> wait; // Call encode function encode1("input.txt", "cipherfile.txt");
Ex. 2: Main Function // Print out encoded (cipher) text dump("cipherfile.txt"); cout << endl << endl; cout << "Please press a key to decode this file:"; cin >> wait; // Call decode function decode1("cipherfile.txt", "output.txt"); // Print out decoded text dump("output.txt"); return 0; }
Ex. 2: encode1 Function void encode1(const char plaintext[], const char ciphertext[]){ // plaintext is filename where the original // message (in English) is stored // ciphertext is filename in which to store// the encoded message const int offset = 5; // secret offset value char tempchar; // declare input and output file streams ifstream ins; ofstream outs; // open input and output data files ins.open(plaintext); outs.open(ciphertext);
Ex. 2: encode1 Function // read original text message character-by-char ins.get(tempchar); while (!ins.eof()) { // encode the character tempchar += offset; // write encoded character to // the output file outs.put(tempchar); // read next character ins.get(tempchar); } // close files ins.close(); outs.close(); }
Ex. 2: decode1 Function void decode1(const char ciphertext[], const char plaintext[]){ // ciphertext is filename where the // encoded message is stored // plaintext is filename in which to store // the decoded message const int offset = 5; // same offset value char tempchar; // declare input and output file streams ifstream ins; ofstream outs; // open input and output files ins.open(ciphertext); outs.open(plaintext);
Ex. 2: decode1 Function // read encoded message character-by-characterins.get(tempchar);while (!ins.eof()) { // decode the character tempchar -= offset; // write decoded character //to the output file outs.put(tempchar); // read next encoded character ins.get(tempchar); } // close files ins.close(); outs.close(); }
Ex. 2: Dump File to Screen void dump(const char filename[]){ // this function outputs the contents of a file // whose name is given in the parameter filename char tempchar; ifstream ins; // declare input file stream ins.open(filename); // open input file// output the file content character by character ins.get(tempchar); while (!ins.eof()) { cout << tempchar; ins.get(tempchar); } // close input file ins.close(); }
Cipher Improvement • The simple offset cipher is very easy to break (for example, by using frequency analysis to determine the constant offset value). • Better security is provided by a cipher where a key sequence is used for offset values. • The first character in the plain text is offset by the first key value in the key sequence. The second is offset by the second key value, and so on. • The longer the key sequence, the harder it is to break the encryption. • Both the sender and receiver must share the same secret key sequence.
A New Message • What does the following message represent? Z¡¬ L¥]X6¼}un¢┤^╨ÑIXƒ[osåæ Answer: THE COMP102 CLASS IS FUN!
Ex. 3: Main Function int main() { char wait; //Temporary input control character int key[KEY_SIZE], index; // declare key array // declare the input and output file names char input_file_name[16], output_file_name[16]; // user supplies the input and output file names cout << "Enter input file name (max 15 chars):\n"; cin >> input_file_name; cout << "Enter output file name (max 15 char):\n"; cin >> output_file_name;
Ex. 3: Main Function // Print out original plain text dump(input_file_name); cout << endl << endl; //create the seed for the random number generator srand(time(NULL)); //Create decode key array; for (index = 0; index < KEY_SIZE; index++) { //For plain ASCII text, //the offset should be less than 128 key[index] = rand()% MAX_OFFSET; //Print out the random number //cout << key[index] << " "; }
Ex. 3: Main Function cout << "Please press a key to encode this file: "; cin >> wait; // Call encode function encode(input_file_name, "cipherfile.txt", key); // Print out encoded (cipher) text dump("cipherfile.txt"); cout << endl << endl;
Ex. 3: Main Function cout << "Please press a key to decode this file: "; cin >> wait; // Call decode function decode("cipherfile.txt", output_file_name, key); // Print out decoded text dump(output_file_name); cout << endl << endl; return 0; }
Ex. 3: encode Function void encode(const char plaintext[], const char ciphertext[], const int keylist[]){ // plaintext is filename where the input // message (in English) is stored // ciphertext is filename in which to // store the encoded message // keylist is the array with the coding key char tempchar; int index = 0;
Ex. 3: encode Function // declare input and output file streams ifstream ins; ofstream outs; // open input and output data files ins.open(plaintext); outs.open(ciphertext);
Ex. 3: encode Function // read original text character-by-character ins.get(tempchar); while (!ins.eof()) { // encode the character tempchar += keylist[index]; //Use the key sequence in a cycle index = (index+1) % KEY_SIZE; // write encoded character to the output file outs.put(tempchar); // read next character ins.get(tempchar); } // close files ins.close(); outs.close(); }
Ex. 3: decode Function void decode(const char ciphertext[], const char plaintext[], const int keylist[]){ // ciphertext is filename where the encoded // message is stored // plaintext is filename in which to store // the decoded message // keylist is the array with the coding key char tempchar; int index = 0;
Ex. 3: decode Function // declare input and output file streams ifstream ins; ofstream outs; // open input and output data files ins.open(ciphertext); outs.open(plaintext);
Ex. 3: decode Function // read encoded message character-by-characterins.get(tempchar);while (!ins.eof()) {tempchar -= keylist[index]; // decode char //Use the key sequence in a cycle index = (index+1) % KEY_SIZE; // write decoded character to output file outs.put(tempchar); // read next encoded character ins.get(tempchar); } // close files ins.close(); outs.close(); }
Ex. 3: Dump File to Screen void dump(const char filename[]){ // this function outputs the contents of a file // whose name is given in the parameter filename char tempchar; ifstream ins; // declare input file stream ins.open(filename); // open input file// output the file content character by character ins.get(tempchar); while (!ins.eof()) { cout << tempchar; ins.get(tempchar); } // close input file ins.close(); }
Cipher Improvement • Much stronger encryption systems exist (such as PGP, DES, etc.) today. ( For more information on data encryption in general, refer to the book by David Kahn, “The Code-Breakers” The Mac Millan New York 1976.)