Python code for Caesar Cipher
The Caesar cipher, also known as the Caesar shift cipher or Caesar’s code, is a simple substitution cipher used to encrypt messages. In the Caesar cipher, each letter in the plaintext is shifted a certain number of places down the alphabet.
For example, with a shift of 1, A would be replaced by B, B would become C, and so on. The encryption can be represented using a key, which is the number of positions each letter is shifted. The same key is used for both encryption and decryption.
The Caesar cipher is considered to be one of the simplest and most widely known encryption techniques. However, it is also very easy to crack, since there are only 25 possible keys for a given alphabet. As a result, the Caesar cipher is mainly used for educational or recreational purposes today.
Code for Caesar Cipher
def caesar_cipher(plaintext, shift):
ciphertext = ""
for char in plaintext:
if char.isalpha():
shift_char = chr((ord(char.upper()) - 65 + shift) % 26 + 65)
if char.islower():
shift_char = shift_char.lower()
ciphertext += shift_char
else:
ciphertext += char
return ciphertext
plaintext = "HELLO WORLD"
shift = 3
print("Plaintext: ", plaintext)
print("Shift: ", shift)
print("Ciphertext: ", caesar_cipher(plaintext, shift))
This code takes the plaintext and shift as inputs and returns the ciphertext by shifting each letter by the specified amount. Note that this implementation only supports shifting uppercase and lowercase letters, and leaves other characters unchanged.
Explanation of the code
- The function
caesar_ciphertakes two arguments:plaintextandshift. - The variable
ciphertextis initialized as an empty string, which will be used to store the encrypted message. - The for loop iterates over each character in the
plaintext. - The
ifstatement checks if the current character is an alphabetical character (uppercase or lowercase). - If the current character is an alphabet, the next step is to shift it. The
shift_charvariable holds the shifted character. The character is first converted to uppercase usingchar.upper()and then its ASCII code is obtained using theordfunction. The shift is then applied using the formula(ord(char.upper()) - 65 + shift) % 26 + 65. The% 26operation is used to handle cases where the shift value is greater than 26. Finally, the shifted character is converted back to lowercase if necessary usingshift_char.lower(). - If the current character is not an alphabet, it is simply added to the
ciphertextwithout any changes. - The encrypted message is returned as the output of the function.
Unit Test
def test_caesar_cipher():
assert caesar_cipher("HELLO WORLD", 3) == "KHOOR ZRUOG"
assert caesar_cipher("hello world", 3) == "khoor zruog"
assert caesar_cipher("HELLO WORLD!", 3) == "KHOOR ZRUOG!"
assert caesar_cipher("HELLO WORLD", 27) == "IFMMp XPSME"
assert caesar_cipher("HELLO WORLD", 0) == "HELLO WORLD"
test_caesar_cipher()
This test function test_caesar_cipher contains five test cases to check if the caesar_cipher function is working as expected. The assert statements check if the output of the function is equal to the expected result. If all the tests pass, it means the code is working as expected. If any of the tests fail, it indicates that there is an issue with the code and it needs to be fixed.