Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commiteae240e

Browse files
committed
add caesar cipher tutorials
1 parent2fdd0f0 commiteae240e

File tree

5 files changed

+106
-0
lines changed

5 files changed

+106
-0
lines changed

‎README.md‎

Lines changed: 2 additions & 0 deletions
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)
Lines changed: 2 additions & 0 deletions
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)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
importsys# The sys module for system-related operations.
2+
fromcoloramaimportFore,init# Import the colorama for colored text
3+
4+
init()# Initialize the colorama library for colored text.
5+
6+
7+
defimplement_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+
forcharacterinmessage:
12+
# Check if the character is an alphabet letter.
13+
ifcharacter.isalpha():
14+
# Determine the shift amount based. i.e the amount of times to be shifted e.g 2,3,4....
15+
shift=keyifnotdecryptelse-key
16+
# Check if the character is a lowercase letter.
17+
ifcharacter.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+
returnresult# 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+
ifkey>25orkey<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+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Import colorama for colorful text.
2+
fromcoloramaimportFore,init
3+
4+
init()
5+
6+
7+
# Define a function for Caesar cipher encryption.
8+
defimplement_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+
forcharintext:
14+
# Check if the character is alphabetical.
15+
ifchar.isalpha():
16+
# Determine the shift value using the provided key (or its negation for decryption).
17+
shift=keyifnotdecryptelse-key
18+
19+
# Check if the character is lowercase
20+
ifchar.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+
returnresult
32+
33+
34+
# Define a function for cracking the Caesar cipher.
35+
defcrack_caesar_cipher(ciphertext):
36+
# Iterate through all possible keys (0 to 25) as there 26 alphabets.
37+
forkeyinrange(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+
whileTrue:
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+
ifnotencrypted_text:
51+
print(f"{Fore.RED}[-] Please specify the text to decrypt.")
52+
else:
53+
crack_caesar_cipher(encrypted_text)
54+
55+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colorama

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp