Homework 5 CMP12a Winter 2002 (Ira Pohl and Scott Brandt)
Due 2/18/2002, before midnight.
Cryptography HW5
We will use both arrays and methods to
develop an encryption scheme for character messages. We will read in messages
using the tio function readChar(). This reads a character at a
time from the console and returns its integer value. We will read a message
up to the guard character #. This message will be no more than
255 character long. This is how long our array will be to store both the clear
text and to produce encrypted test.
We will use the Random() function
to produce encrypted text. The following methods will help you structure your
code:
//main should prompt for clear
text
System.out.println("enter message until #");
use readUntil('#');
to assign to a char[] the clear text
use encrypt() with a seed of your choice such as 7 to get an encryption
use printChars( ) to
print first the clear text and then the encrypted text
public static void encrypt(char[]
clearText, char[] codeText, int seed)
//takes clear text and produces encrypted text
//r = new Random(seed) produces a random number sequence
public static char encodeChar(char
c, int d)
//use encodeChar to produce a coding
//d should be a random integer that together with
//the char value c gives as return value the coded text char.
public static void printChars(char[]
text) //print out char array as characters
public static char[] readUntil(char
guard)
//produce an array of char from terminal input.
The character values for alphabetic characters are from 65 for 'A' upto 122 for 'z'
Assume you generate a value of between
0 and 25 at random to add to these values. Now you have c + d as between 65
and 147. Assume you mod this value by 26 then you are back to values between
0 and 25. Now add to this 65 and you get the values for characters that are
the capital letters. So if the clear text is an 'H' or value 72 and you generate
at random the value 10 then you get 82 % 26 + 65 as the encodeChar()
value which produces 69 and this is the character 'E'.
Try writing each method separately
and test each method independently. Hand simulate the encrytion technique to
make sure you understand how this all works. remember that Random()
produces doubles betwenn0 and 1. You need to multiply this by 26 and cast to
an integer to get an appropriate random integer. HINT: Read pages 358-360 about
encryption.
In Homework 6 which will be the graded problem, we will extend this homework to decryption.
use submit cmps012a-ip.w02 hw5 filename.java
(Ira Pohl's class)
or submit cmps012a-sb.w02 hw5 filename.java (Scott Brandt's
class)