Array Application Programming. COMP102 Prog. Fundamentals I: Arrays Application / Slide 2 Character...

25
Array Application Programming
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    225
  • download

    1

Transcript of Array Application Programming. COMP102 Prog. Fundamentals I: Arrays Application / Slide 2 Character...

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

Array Application

Programming

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

COMP102 Prog. Fundamentals I: Arrays Application / Slide 2

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

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

COMP102 Prog. Fundamentals I: Arrays Application / Slide 3

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.

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

COMP102 Prog. Fundamentals I: Arrays Application / Slide 4

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.

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

COMP102 Prog. Fundamentals I: Arrays Application / Slide 5

Ex. 2: Main Functionint 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");

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

COMP102 Prog. Fundamentals I: Arrays Application / Slide 6

Ex. 2: Main Function// Print out encoded (cipher) textdump("cipherfile.txt");cout << endl << endl;

cout << "Please press a key to decode this file:"; cin >> wait;

// Call decode functiondecode1("cipherfile.txt", "output.txt");// Print out decoded textdump("output.txt");

return 0;}

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

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);

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

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(); }

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

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);

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

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(); }

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

COMP102 Prog. Fundamentals I: Arrays Application / Slide 11

Ex. 2: Dump File to Screenvoid 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();

}

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

COMP102 Prog. Fundamentals I: Arrays Application / Slide 12

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.

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

COMP102 Prog. Fundamentals I: Arrays Application / Slide 13

A New Message

What does the following message represent?

Z¡¬ L¥]X6¼}un¢┤^╨ÑIXƒ[osåæ

Answer:

THE COMP102 CLASS IS FUN!

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

COMP102 Prog. Fundamentals I: Arrays Application / Slide 14

Ex. 3: Main Functionint 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;

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

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 128key[index] = rand()% MAX_OFFSET;//Print out the random number//cout << key[index] << " ";

}

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

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;

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

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 functiondecode("cipherfile.txt", output_file_name, key);

// Print out decoded textdump(output_file_name);cout << endl << endl;

return 0;}

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

COMP102 Prog. Fundamentals I: Arrays Application / Slide 18

Ex. 3: encode Functionvoid 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;

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

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);

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

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(); }

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

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;

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

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);

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

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();

}

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

COMP102 Prog. Fundamentals I: Arrays Application / Slide 24

Ex. 3: Dump File to Screenvoid 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();

}

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

COMP102 Prog. Fundamentals I: Arrays Application / Slide 25

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.)