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. 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: "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. Caesar Cipher 2
  • def open_infile ( ): It opens the data file for reading (whose full pathname is given ) and returns the value of the input stream to the calling function. If opening the data file is unsuccessful, it prints an error message on stderr and stops the execution of the program with the exit value 1
  • def encodeCaesarCipher ( line, shift, key ): It returns a new string formed by shifting every letter in line forward the number of letters indicated by shift and key, cycling back to the beginning of the alphabet if necessary. To implement shifting, this function calls the following auxiliary function.
  • def new_position ( c, shift, key ): For the character c, the shift value, and the second shift value obtained from the key, it returns a replacement character for c.

Note: If you want to test your program, simply enter arbitrary key, shift pairs from the stdin and use the full pathname of your data file in your python program instead of the test data file prog3.d2

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.