|
| 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}") |
0 commit comments