Creating a Caesar Cipher with Python
In this guide I will explain what the Caesar cipher is and when it was first used. Then I will show you how to create your own Caesar cipher program with Python.
What is the Caesar Cipher?
The Caesar cipher is one of the simplest and oldest methods of encryption. It is a type of substitution cipher in which each letter in the plaintext is shifted a certain number of places down or up the alphabet. This shift value is often referred to as the “key” or “shift.”
History of the Caesar Cipher
The Caesar cipher is one of the earliest known encryption methods and has its roots in ancient Rome. It is attributed to Julius Caesar, the famous Roman military general and statesman, who lived from 100 BC to 44 BC. While it’s often associated with Julius Caesar, the exact historical origins and usage details are somewhat unclear.
The Caesar cipher is believed to have been used by Julius Caesar for military and diplomatic communication during his campaigns. The primary purpose of using this cipher was to protect sensitive information from falling into the wrong hands, particularly in the event that a message was intercepted by enemy forces or spies.
Here’s a simplified example of how the Caesar cipher might have been used by Julius Caesar:
- Julius Caesar and his trusted officers agree on a key, which in this case is 3.
- When Caesar wanted to send a message to one of his officers, he would encode it by shifting each letter in the plaintext message three positions down the alphabet.
- The officer receiving the message would know the key and shift each letter in the ciphertext back up the alphabet to decrypt the message.
While the Caesar cipher provided basic security for its time, it was not particularly strong by modern standards. The limited number of possible keys (only 25 for a 26-letter alphabet) made it vulnerable to brute-force attacks. However, in ancient times, with less emphasis on secure communication methods and encryption, it served its purpose in protecting sensitive information from casual eavesdroppers.
How the Caesar Cipher works:
Choose a shift value (key): The sender and receiver of the message agree on a specific number by which each letter will be shifted. For example, if the key is 3, each letter in the plaintext will be shifted 3 positions down the alphabet.
Encrypt the message: To encrypt a message, you replace each letter in the plaintext with the letter that is shifted by the key positions. Wrap around the alphabet if necessary. For example, if the key is 3 and you want to encrypt the letter ‘A,’ it becomes ‘D.’ If you want to encrypt ‘Z,’ it wraps around to ‘C.’
Decrypt the message: To decrypt a message, you simply reverse the process. You shift each letter in the ciphertext back by the key positions. For example, if the key is 3 and you want to decrypt ‘D,’ it becomes ‘A.’ Similarly, ‘C’ becomes ‘Z.’
Here’s an example of the Caesar cipher with a key of 3:
Plaintext: HELLO
Encrypted: KHOOR
Ciphertext: KHOOR
Decrypted: HELLO
Creating a Caesar Cipher with Python
The encryption is very simple, you take the first letter and move it 3 places to the right in the alphabet, “A” becomes a “D”, “B” becomes an “E”, “C” becomes an “F”, etc.
Code for Encryption:
Scroll down the code until you find the line text = "CAESAR CIPHER DEMO"
you can replace the string within the " "
quotation marks with any text or message you want to encrypt. Then run the Python script to encrypt the text.
def encrypt(text,s): result = "" # transverse the plain text for i in range(len(text)): char = text[i] # Encrypt uppercase characters in plain text if (char.isupper()): result += chr((ord(char) + s-65) % 26 + 65) # Encrypt lowercase characters in plain text else: result += chr((ord(char) + s - 97) % 26 + 97) return result #check the above function text = "CEASER CIPHER DEMO" s = 4 print("Plain Text : " + text) print("Shift pattern : " + str(s)) print("Cipher: " + encrypt(text,s))
Code for Decryption:
You should have seen you’re encrypted text displayed to the terminal or console. You can now take the encrypted text and input it into the following script in the message = 'GIEWIVrGMTLIVrHIQS'
variable. Again, just replace the text within the quotation marks with your encrypted text. Run the Python script and your encrypted text with be printed and decrypted to the terminal or console.
message = 'GIEWIVrGMTLIVrHIQS' #encrypted message LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' for key in range(len(LETTERS)): translated = '' for symbol in message: if symbol in LETTERS: num = LETTERS.find(symbol) num = num - key if num < 0: num = num + len(LETTERS) translated = translated + LETTERS[num] else: translated = translated + symbol print('Hacking key #%s: %s' % (key, translated))
Conclusion
The Caesar cipher is relatively easy to understand and implement, but it is not secure for modern communication because there are only 25 possible keys (assuming a standard 26-letter English alphabet). This means that an attacker can easily try all possible keys to decrypt the message.
In more secure modern encryption systems, more complex methods are used, such as the use of longer keys and algorithms that are resistant to brute-force attacks. The Caesar cipher is mainly used for educational purposes and as a simple introduction to the concept of encryption.
Find more of our Python guides here: Python Guides
Recommendation:
Big Book of Small Python Programs: 81 Easy Practice Programs: https://amzn.to/3rGZjCR