Skip to content

Commit b277253

Browse files
committed
add implementing affine cipher tutorial
1 parent 2d39d8c commit b277253

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5151
- [How to Crack the Caesar Cipher in Python](https://thepythoncode.com/article/how-to-crack-caesar-cipher-in-python). ([code](ethical-hacking/caesar-cipher))
5252
- [How to Lock PDFs in Python](https://thepythoncode.com/article/lock-pdfs-in-python). [(code)](ethical-hacking/pdf-locker)
5353
- [How to Create a Custom Wordlist in Python](https://thepythoncode.com/article/make-a-wordlist-generator-in-python). ([code](ethical-hacking/bruteforce-wordlist-generator))
54+
- [How to Implement the Affine Cipher in Python](https://thepythoncode.com/article/how-to-implement-affine-cipher-in-python). ([code](ethical-hacking/implement-affine-cipher))
5455

5556
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
5657
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)

Diff for: ethical-hacking/implement-affine-cipher/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Implement the Affine Cipher in Python](https://thepythoncode.com/article/how-to-implement-affine-cipher-in-python)
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Import necessary libraries.
2+
import string
3+
from colorama import init, Fore
4+
5+
# Initialise colorama.
6+
init()
7+
8+
9+
# Function to perform Affine Cipher encryption.
10+
def affine_encryption(plaintext, a, b):
11+
# Define the uppercase alphabet.
12+
alphabet = string.ascii_uppercase
13+
# Get the length of the alphabet
14+
m = len(alphabet)
15+
# Initialize an empty string to store the ciphertext.
16+
ciphertext = ''
17+
18+
# Iterate through each character in the plaintext.
19+
for char in plaintext:
20+
# Check if the character is in the alphabet.
21+
if char in alphabet:
22+
# If it's an alphabet letter, encrypt it.
23+
# Find the index of the character in the alphabet.
24+
p = alphabet.index(char)
25+
# Apply the encryption formula: (a * p + b) mod m.
26+
c = (a * p + b) % m
27+
# Append the encrypted character to the ciphertext.
28+
ciphertext += alphabet[c]
29+
else:
30+
# If the character is not in the alphabet, keep it unchanged.
31+
ciphertext += char
32+
33+
# Return the encrypted ciphertext.
34+
return ciphertext
35+
36+
37+
# Define the plaintext and key components.
38+
plaintext = input(f"{Fore.GREEN}[?] Enter text to encrypt: ")
39+
a = 3
40+
b = 10
41+
42+
# Call the affine_encrypt function with the specified parameters.
43+
encrypted_text = affine_encryption(plaintext, a, b)
44+
45+
# Print the original plaintext, the key components, and the encrypted text.
46+
print(f"{Fore.MAGENTA}[+] Plaintext: {plaintext}")
47+
print(f"{Fore.GREEN}[+] Encrypted Text: {encrypted_text}")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
colorama

0 commit comments

Comments
 (0)