Skip to content

Commit eae240e

Browse files
committedNov 9, 2023
add caesar cipher tutorials
1 parent 2fdd0f0 commit eae240e

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed
 

‎README.md

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
4747
- [How to List Wi-Fi Networks in Python](https://thepythoncode.com/article/list-nearby-wifi-networks-with-python). ([code](ethical-hacking/listing-wifi-networks))
4848
- [How to Verify File Integrity in Python](https://thepythoncode.com/article/verify-downloaded-files-with-checksum-in-python). ([code](ethical-hacking/verify-file-integrity))
4949
- [How to Create a Zip File Locker in Python](https://thepythoncode.com/article/build-a-zip-file-locker-in-python). ([code](ethical-hacking/zip-file-locker))
50+
- [How to Implement the Caesar Cipher in Python](https://thepythoncode.com/article/implement-caesar-cipher-in-python). ([code](ethical-hacking/caesar-cipher))
51+
- [How to Crack the Caesar Cipher in Python](https://thepythoncode.com/article/how-to-crack-caesar-cipher-in-python). ([code](ethical-hacking/caesar-cipher))
5052

5153
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
5254
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [How to Implement the Caesar Cipher in Python](https://thepythoncode.com/article/implement-caesar-cipher-in-python)
2+
# [How to Crack the Caesar Cipher in Python](https://thepythoncode.com/article/how-to-crack-caesar-cipher-in-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys # The sys module for system-related operations.
2+
from colorama import Fore, init # Import the colorama for colored text
3+
4+
init() # Initialize the colorama library for colored text.
5+
6+
7+
def implement_caesar_cipher(message, key, decrypt=False):
8+
# Initialize an empty string to store the result.
9+
result = ""
10+
# Iterate through each character in the user's input message.
11+
for character in message:
12+
# Check if the character is an alphabet letter.
13+
if character.isalpha():
14+
# Determine the shift amount based. i.e the amount of times to be shifted e.g 2,3,4....
15+
shift = key if not decrypt else -key
16+
# Check if the character is a lowercase letter.
17+
if character.islower():
18+
# Apply Caesar cipher transformation for lowercase letters.
19+
result += chr(((ord(character) - ord('a') + shift) % 26) + ord('a'))
20+
else:
21+
# Apply Caesar cipher transformation for uppercase letters.
22+
result += chr(((ord(character) - ord('A') + shift) % 26) + ord('A'))
23+
else:
24+
# Preserve non-alphabet characters as they are.
25+
result += character
26+
return result # Return the encrypted or decrypted result.
27+
28+
29+
# Prompt the user to enter the text to be encrypted
30+
text_to_encrypt = input(f"{Fore.GREEN}[?] Please Enter your text/message: ")
31+
# Prompt the user to specify the shift length (the key).
32+
key = int(input(f"{Fore.GREEN}[?] Please specify the shift length: "))
33+
34+
35+
# Check if the specified key is within a valid range (0 to 25).
36+
if key > 25 or key < 0:
37+
# Display an error message if the key is out of range.
38+
print(f"{Fore.RED}[!] Your shift length should be between 0 and 25 ")
39+
sys.exit() # Exit the program if the key is invalid.
40+
41+
# Encrypt the user's input using the specified key.
42+
encrypted_text = implement_caesar_cipher(text_to_encrypt, key)
43+
44+
# Display the encrypted text.
45+
print(f"{Fore.GREEN}[+] {text_to_encrypt} {Fore.MAGENTA}has been encrypted as {Fore.RED}{encrypted_text}")
46+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Import colorama for colorful text.
2+
from colorama import Fore, init
3+
4+
init()
5+
6+
7+
# Define a function for Caesar cipher encryption.
8+
def implement_caesar_cipher(text, key, decrypt=False):
9+
# Initialize an empty string to store the result.
10+
result = ""
11+
12+
# Iterate through each character in the input text.
13+
for char in text:
14+
# Check if the character is alphabetical.
15+
if char.isalpha():
16+
# Determine the shift value using the provided key (or its negation for decryption).
17+
shift = key if not decrypt else -key
18+
19+
# Check if the character is lowercase
20+
if char.islower():
21+
# Apply the Caesar cipher encryption/decryption formula for lowercase letters.
22+
result += chr(((ord(char) - ord('a') + shift) % 26) + ord('a'))
23+
else:
24+
# Apply the Caesar cipher encryption/decryption formula for uppercase letters.
25+
result += chr(((ord(char) - ord('A') + shift) % 26) + ord('A'))
26+
else:
27+
# If the character is not alphabetical, keep it as is e.g. numbers, punctuation
28+
result += char
29+
30+
# Return the result, which is the encrypted or decrypted text
31+
return result
32+
33+
34+
# Define a function for cracking the Caesar cipher.
35+
def crack_caesar_cipher(ciphertext):
36+
# Iterate through all possible keys (0 to 25) as there 26 alphabets.
37+
for key in range(26):
38+
# Call the caesar_cipher function with the current key to decrypt the text.
39+
decrypted_text = implement_caesar_cipher(ciphertext, key, decrypt=True)
40+
41+
# Print the result, showing the decrypted text for each key
42+
print(f"{Fore.RED}Key {key}: {decrypted_text}")
43+
44+
45+
# Initiate a continuous loop so the program keeps running.
46+
while True:
47+
# Accept user input.
48+
encrypted_text = input(f"{Fore.GREEN}[?] Please Enter the text/message to decrypt: ")
49+
# Check if user does not specify anything.
50+
if not encrypted_text:
51+
print(f"{Fore.RED}[-] Please specify the text to decrypt.")
52+
else:
53+
crack_caesar_cipher(encrypted_text)
54+
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colorama

0 commit comments

Comments
 (0)
Please sign in to comment.