Project 2: Cipher implementation in python
The primary goal of this project is to understand how Caesar cipher works Here, we are using python programming language to implement the Caesar cipher algorithm. It is not mandatory to know python in order to run the program. The main focus is to understand how the input message is converted into ciphertext with the help of encryption logic and then use a similar pattern to perform decryption. You will be provided with a python code that has implemented encryption logic. Your task is to understand the logic and implement decryption by simply changing the logic. Python environment setup: Students can install python working environment on their local computer or they can use the online python IDE in order to run the program.
i. Installing Python in Windows: https://www.youtube.com/watch?v=yiCUmJon-5g ii. Installing Python in MacOS: https://www.youtube.com/watch?v=0hGzGdRQeak iii. Alternatively, you can simply go to https://repl.it/languages/python3 and copy-paste the
code given in the file and then run the code online. Caesar cipher: The Caesar Cipher technique is one of the earliest and simplest methods of encryption technique. It’s simply a type of substitution cipher, i.e., each letter of a given text is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 1, A would be replaced by B, B would become C, and so on. The method is apparently named after Julius Caesar, who apparently used it to communicate with his officials. Thus to cipher a given text we need an integer value, known as a shift which indicates the number of positions each letter of the text has been moved down. The encryption can be represented using modular arithmetic by first transforming the letters into numbers, according to the scheme, A = 0, B = 1,…, Z = 25. Encryption of a letter by a shift n can be described mathematically as.
https://www.youtube.com/watch?v=yiCUmJon-5g
https://www.youtube.com/watch?v=0hGzGdRQeak
https://repl.it/languages/python3
Note: In order to perform decryption, the key must be same as the one used for encryption. In our case, we are using the 3 as a key for encryption.
Encryption: You will be provided with a python file in this assignment. Open it and look for the below line as highlighted. Let’s compare the code with the logic explained below. The most important part of the code is logic which is as below:
result += chr((ord(char) + s-65) % 26 + 65) Let’s compare the highlighted part with the logic of encryption.
E(x) = (x + n) mod 26,
which can also be re-written as (x + n) % 26
In Python file: ord((char) + s - 65) % 26 In the code, the value x is represented as ord(char), s is represented as n and 65 is
subtracted in order to convert ASCII value of upper-case character to base 1. (i.e. A
has ASCII value of 065. In order to perform addition, the base should be 0 so we
subtract 65 from ASCII value and then add it again which is shown in green color.
The same goes with any alphabetic character).
Logic: (x + n) mod 26 Code: ord(char) + s - 65) % 26
x ord(char)
n: Encryption key s: Encryption key
Decryption: The decryption is very simple to implement. Given the logic, you have to change the logic of the code. The formula for the decryption is as below: D(x) = (x - n) mod 26
//Code
In python code: result += chr((ord(char) - s-65) % 26 + 65)
//Replace this part in the decryption file line 11 and run the code. Check whether decryption works properly or not. //Replace the line in the given python code as the above highlighted line and it will perform decryption Based on the logic, you have to make changes in the above part in the code in order to perform decryption. Check whether the decryption is working as expected (i.e., encrypted message converts to the original message if the decryption is correctly performed). Note: The function accepts CAPITAL letters as sinput.
Submit the decryption code as a part of the assignment in the word file as stated below.
Answer the following questions based on the activities you performed and submit the word file on canvas:
i. What are the drawbacks of Caesar cipher? ii. What can be done to improve the Caesar cipher technique? iii. Write down the code for decryption in the word file. (Copy paste the modified code)
Credits:
1. https://www.geeksforgeeks.org/caesar-cipher-in-cryptography/