|
| 1 | +# Import sys for system operations and colorama for colored output. |
| 2 | +importsys |
| 3 | +fromcoloramaimportinit,Fore |
| 4 | + |
| 5 | +# Initialise colorama |
| 6 | +init() |
| 7 | + |
| 8 | + |
| 9 | +# Function to Encrypt using the Vigenère cipher. |
| 10 | +defvigenere_encrypt(plain_text,key): |
| 11 | +encrypted_text='' |
| 12 | + |
| 13 | +# Repeat the key to match the length of the plaintext. |
| 14 | +key_repeated= (key* (len(plain_text)//len(key)))+key[:len(plain_text)%len(key)] |
| 15 | + |
| 16 | +# Iterate through each character in the plaintext. |
| 17 | +foriinrange(len(plain_text)): |
| 18 | +# Check if the character is an alphabet letter. |
| 19 | +ifplain_text[i].isalpha(): |
| 20 | +# Calculate the shift based on the corresponding key letter. |
| 21 | +shift=ord(key_repeated[i].upper())-ord('A') |
| 22 | + |
| 23 | +# Encrypt uppercase and lowercase letters separately. |
| 24 | +ifplain_text[i].isupper(): |
| 25 | +encrypted_text+=chr((ord(plain_text[i])+shift-ord('A'))%26+ord('A')) |
| 26 | +else: |
| 27 | +encrypted_text+=chr((ord(plain_text[i])+shift-ord('a'))%26+ord('a')) |
| 28 | +else: |
| 29 | +# If the character is not an alphabet letter, keep it unchanged. |
| 30 | +encrypted_text+=plain_text[i] |
| 31 | + |
| 32 | +# Return the final encrypted text |
| 33 | +returnencrypted_text |
| 34 | + |
| 35 | + |
| 36 | +# Decryption function for the Vigenère cipher |
| 37 | +defvigenere_decrypt(cipher_text,key): |
| 38 | +decrypted_text='' |
| 39 | + |
| 40 | +# Repeat the key to match the length of the ciphertext |
| 41 | +key_repeated= (key* (len(cipher_text)//len(key)))+key[:len(cipher_text)%len(key)] |
| 42 | + |
| 43 | +# Iterate through each character in the ciphertext |
| 44 | +foriinrange(len(cipher_text)): |
| 45 | +# Check if the character is an alphabet letter |
| 46 | +ifcipher_text[i].isalpha(): |
| 47 | +# Calculate the shift based on the corresponding key letter |
| 48 | +shift=ord(key_repeated[i].upper())-ord('A') |
| 49 | + |
| 50 | +# Decrypt uppercase and lowercase letters separately |
| 51 | +ifcipher_text[i].isupper(): |
| 52 | +decrypted_text+=chr((ord(cipher_text[i])-shift-ord('A'))%26+ord('A')) |
| 53 | +else: |
| 54 | +decrypted_text+=chr((ord(cipher_text[i])-shift-ord('a'))%26+ord('a')) |
| 55 | +else: |
| 56 | +# If the character is not an alphabet letter, keep it unchanged |
| 57 | +decrypted_text+=cipher_text[i] |
| 58 | + |
| 59 | +# Return the final decrypted text |
| 60 | +returndecrypted_text |
| 61 | + |
| 62 | + |
| 63 | +key="KEY" |
| 64 | +# Get user input (Message to encrypt). |
| 65 | +plaintext=input('[!] Enter your message: ') |
| 66 | + |
| 67 | +# Encrypt the plaintext using the Vigenère cipher |
| 68 | +cipher_text=vigenere_encrypt(plaintext,key) |
| 69 | + |
| 70 | +# Print the results |
| 71 | +print(f"[+] Plaintext:{plaintext}") |
| 72 | +print(f"{Fore.GREEN}[+] Ciphertext:{cipher_text}") |
| 73 | + |
| 74 | +# Ask if user wants to decrypt the message (just to see the functionality.) |
| 75 | +ask_to_decrypt=input('\n\n[?] Do you want to decrypt the message?\n[?] Y or N: ').lower() |
| 76 | + |
| 77 | +# If user wants to. |
| 78 | +ifask_to_decrypt=='y': |
| 79 | +# Decrypt the ciphertext back to the original plaintext. |
| 80 | +decrypted_text=vigenere_decrypt(cipher_text,key) |
| 81 | +print(f"{Fore.GREEN}[+] Decrypted text:{decrypted_text}") |
| 82 | + |
| 83 | +# If user does not want to. |
| 84 | +elifask_to_decrypt=='n': |
| 85 | +sys.exit() |
| 86 | +# When an invalid input is entered. |
| 87 | +else: |
| 88 | +print(f"{Fore.RED}[-] Invalid input.") |