Presentation is loading. Please wait.

Presentation is loading. Please wait.

Array Application Programming. COMP102 Prog. Fundamentals I: Arrays Application / Slide 2 Character Array: Ex. 1 l Note: a palindrome is a word (or words)

Similar presentations


Presentation on theme: "Array Application Programming. COMP102 Prog. Fundamentals I: Arrays Application / Slide 2 Character Array: Ex. 1 l Note: a palindrome is a word (or words)"— Presentation transcript:

1 Array Application Programming

2 COMP102 Prog. Fundamentals I: Arrays Application / Slide 2 Character Array: Ex. 1 l Note: a palindrome is a word (or words) which read the same forward and backward n kayak n solos n radar n racecar n deified n evil olive n Malayalam n lonely Tylenol n Re-divider n Madam I'm Adam n step on no pets n won ton? not now Run Example 1 to find out which words the computer considers a palindrome

3 COMP102 Prog. Fundamentals I: Arrays Application / Slide 3 An Encoded Message l What does the following message represent? YMJ%HTRU657%HQFXX%NX%YTT%JFX^& Answer: THE COMP102 CLASS IS EASY! l Problem statement: Write a function that encodes a message (in English) using some encryption method, and another function that decodes the encoded message.

4 COMP102 Prog. Fundamentals I: Arrays Application / Slide 4 Offset Cipher l We use the simplest method, the “offset cipher”. l Define offset value, which will be used in both the encoding and decoding processes. l The encoding function shifts each character in the message higher by the offset amount. Example: if offset is 1, then a becomes b. l 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.

5 COMP102 Prog. Fundamentals I: Arrays Application / Slide 5 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");

6 COMP102 Prog. Fundamentals I: Arrays Application / Slide 6 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; }

7 COMP102 Prog. Fundamentals I: Arrays Application / Slide 7 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);

8 COMP102 Prog. Fundamentals I: Arrays Application / Slide 8 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(); }

9 COMP102 Prog. Fundamentals I: Arrays Application / Slide 9 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);

10 COMP102 Prog. Fundamentals I: Arrays Application / Slide 10 Ex. 2: decode1 Function // read encoded message character-by-character ins.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(); }

11 COMP102 Prog. Fundamentals I: Arrays Application / Slide 11 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(); }

12 COMP102 Prog. Fundamentals I: Arrays Application / Slide 12 Cipher Improvement l The simple offset cipher is very easy to break (for example, by using frequency analysis to determine the constant offset value). l Better security is provided by a cipher where a key sequence is used for offset values. n 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. n The longer the key sequence, the harder it is to break the encryption. n Both the sender and receiver must share the same secret key sequence.

13 COMP102 Prog. Fundamentals I: Arrays Application / Slide 13 A New Message l What does the following message represent? Z¡¬ L¥]X6¼}un¢┤^╨ÑIXƒ[osåæ Answer: THE COMP102 CLASS IS FUN!

14 COMP102 Prog. Fundamentals I: Arrays Application / Slide 14 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;

15 COMP102 Prog. Fundamentals I: Arrays Application / Slide 15 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] << " "; }

16 COMP102 Prog. Fundamentals I: Arrays Application / Slide 16 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;

17 COMP102 Prog. Fundamentals I: Arrays Application / Slide 17 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; }

18 COMP102 Prog. Fundamentals I: Arrays Application / Slide 18 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;

19 COMP102 Prog. Fundamentals I: Arrays Application / Slide 19 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);

20 COMP102 Prog. Fundamentals I: Arrays Application / Slide 20 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(); }

21 COMP102 Prog. Fundamentals I: Arrays Application / Slide 21 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;

22 COMP102 Prog. Fundamentals I: Arrays Application / Slide 22 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);

23 COMP102 Prog. Fundamentals I: Arrays Application / Slide 23 Ex. 3: decode Function // read encoded message character-by-character ins.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(); }

24 COMP102 Prog. Fundamentals I: Arrays Application / Slide 24 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(); }

25 COMP102 Prog. Fundamentals I: Arrays Application / Slide 25 Cipher Improvement l 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.)


Download ppt "Array Application Programming. COMP102 Prog. Fundamentals I: Arrays Application / Slide 2 Character Array: Ex. 1 l Note: a palindrome is a word (or words)"

Similar presentations


Ads by Google