

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


For this lab, you’ll be introduced to working with the char data type, all while practice your use of arrays. As an excuse to work with arrays of characters, we’ll do some basic cryptography.
Cryptography is, loosely speaking, the study of methods for taking “human readable” data (clear- text or plaintext) and then scrambling (encrypting) it into an obfuscated form (ciphertext) that cannot easily be read. Of course, such obfuscation is useless unless there is some means by which the ciphertext can be restored (decrypted) to its original cleartext form. A cipher defines a one-to-one correspondence between plaintexts to ciphertexts. That is, it de- fines a pair of algorithms, one for encryption and one for decryption, that respectively scramble and unscramble data. Each cipher uses a single parameter called the key that is used as part of the encryption and decryption calculations. For a given ciphertext, the same key must be used to decrypt it as was used when creating it during encryption.^1 The ciphers that we are going to use are monoalphabetic substitution ciphers. Specifically, for these ciphers, encryption is accomplished by replacing each character in the plaintext with a dif- ferent letter in the ciphertext. Therefore, the lengths of the plaintext and the ciphertext are equal, and both encryption and decryption can each be performed one character at a time.
For this first part of this project, we will use the Caesar cipher (also known as a shift cipher). To encrypt a message using this cipher, each character of the cleartext is shifted forward by k charac- ters in the alphabet, thus producing a ciphertext. Likewise, decrypting a message requires shifting each character in the ciphertext backwards by the same k characters in the alphabet. Moreover, since the alphabet of characters is a fixed-sized list, it is possible to shift “off the end” of the list. Whenever this situation arises, the shift operation “wraps around” to the beginning of the alphabet. To see how this cipher works, consider only the 26 uppercase letters of the English alphabet. Fur- thermore, consider using the key value k = 5. The last 5 characters in this alphabet, when shifted, are wrapped-around to the first five characters. So, the correspondence between the cleartext and ciphertext characters would be:
Cleartext char A B C D E F G... S T U V W X Y Z Ciphertext char F G H I J K L... X Y Z A B C D E
Thus, the following message, if encrypted using the same key of k = 5 would be: (^1) The use of the same key value for encryption and decryption makes a cipher symmetric. There are asymmetric ciphers, most notably public-key cryptography, where different keys are used for encryption and decryption, but those are beyond the scope of this course. Consider the Networks and Cryptography course.
Cleartext message THEQUICKBROWNFOX Ciphertext message YMJVZNHPGWTBSKTC
Implementation hints: In order to write a program that performs this type of encryption and decryption, there are two valuable observations of which you need to be aware:
Begin like so:
The file CaesarCipher.java already contains a significant portion of the needed source code. The one missing piece is the body of the method cipher(). This method is passed an array of characters (via the parameter text), as well as the desired shift to be performed on each character (via the parameter key). This method must...