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