The encryption technique called Caesar Cipher replaces each letter in a text message by a letter that appears a fixed distance in the alphabet.

computer science

Description

The encryption technique called Caesar Cipher replaces each letter in a text message by a letter that appears a fixed distance in the alphabet. As an example, suppose that you wanted to encode a message by shifting every letter ahead three places. In this cipher, each A becomes a D, B becomes E, and so on. If you reach the end of the alphabet, the process cycles around to the beginning, so that X becomes A, Y becomes B, and Z becomes C.


Note that the transformation applies only to letters; any other characters are copied unchanged to the output, where the case of letters is unaffected: lowercase letters come out as lowercase, and uppercase letters come out as uppercase. Your python program should also accept negative shift values that means that letters are shifted toward the beginning of the alphabet instead of toward the end.


In addition to regular shifting, a second shifting is applied to each letter in the input stream, where a key of letters is used to figure out the shifting value of a letter in a text file. The key is entered from stdin and it can contain any number of letters. As an example, the key could be key = “QWERTYUIOPASDFGHJKLZXCVBNM”. For the letter A, the corresponding letter in the key is Q, so the second shift value for A is –16 that is the difference between the ASCII values of A and Q; for the letter B, the corresponding letter in the key is W that yields the second shift value of –21; for the letter C, the corresponding letter in the key is E that yields the second shift value of –2; etc. For a lowercase letter, you need to convert the letter to uppercase before you use. If the length of the key is less than 26, then the position of a letter in the key can be determined by the expression: k % len ( key ), where k is the position of the letter in the 26-letter alphabetic sequence. If the length of the key is greater than 26, simply discard the letter beyond the 26 letters.


There are two data files for this program: prog3.d1 and prog3.d2. Both are in directory: /home/cs503/progs/20s/p3. The first file contains several test values for shift and key, and the second one contains a text message to encode.


• def main ( ): It reads a pair of values shift and key from the stdin, which are directed from the file: “/home/cs503/progs/20s/p3/prog3.d1”, and for each pair, it prints those values on stdout and calls the function process_infile ( ) (described below) to encode the input text in the data file. It continues of reading the input pairs and encodes the input text until no more values are entered from the stdin.


• def process_infile ( shift, key ): It calls the function open_infile ( ) (described below) to open the data file. It prints out the shift and key values on stdout passed as arguments and gets the text input from the data file. To process each input line in the data file, it calls the encodeCaesarCipher ( ) function (described below) and prints out the encrypted text returned by this function on stdout. Finally, it closes the data file.


Related Questions in computer science category