Skip to content

Commit

Permalink
Removed double implementation of vigenere_cipher.py. Fixes TheAlgorit…
Browse files Browse the repository at this point in the history
  • Loading branch information
aryan1165 committed Oct 8, 2023
1 parent 937ce83 commit eee0ae7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 131 deletions.
82 changes: 0 additions & 82 deletions ciphers/beaufort_cipher.py

This file was deleted.

115 changes: 66 additions & 49 deletions ciphers/vigenere_cipher.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,82 @@
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"""
Author: Mohit Radadiya
"""

from string import ascii_uppercase

def main() -> None:
message = input("Enter message: ")
key = input("Enter key [alphanumeric]: ")
mode = input("Encrypt/Decrypt [e/d]: ")

if mode.lower().startswith("e"):
mode = "encrypt"
translated = encrypt_message(key, message)
elif mode.lower().startswith("d"):
mode = "decrypt"
translated = decrypt_message(key, message)
dict1 = {char: i for i, char in enumerate(ascii_uppercase)}
dict2 = dict(enumerate(ascii_uppercase))

print(f"\n{mode.title()}ed message:")
print(translated)


def encrypt_message(key: str, message: str) -> str:
# This function generates the key in
# a cyclic manner until it's length isn't
# equal to the length of original text
def generate_key(message: str, key: str) -> str:
"""
>>> encrypt_message('HDarji', 'This is Harshil Darji from Dharmaj.')
'Akij ra Odrjqqs Gaisq muod Mphumrs.'
>>> generate_key("THE GERMAN ATTACK","SECRET")
'SECRETSECRETSECRE'
"""
return translate_message(key, message, "encrypt")


def decrypt_message(key: str, message: str) -> str:
x = len(message)
i = 0
while True:
if x == i:
i = 0
if len(key) == len(message):
break
key += key[i]
i += 1
return key


# This function returns the encrypted text
# generated with the help of the key
def cipher_text(message: str, key_new: str) -> str:
"""
>>> decrypt_message('HDarji', 'Akij ra Odrjqqs Gaisq muod Mphumrs.')
'This is Harshil Darji from Dharmaj.'
>>> cipher_text("THE GERMAN ATTACK","SECRETSECRETSECRE")
'BDC PAYUWL JPAIYI'
"""
return translate_message(key, message, "decrypt")


def translate_message(key: str, message: str, mode: str) -> str:
translated = []
key_index = 0
key = key.upper()
cipher_text = ""
i = 0
for letter in message:
if letter == " ":
cipher_text += " "
else:
x = (dict1[letter] - dict1[key_new[i]]) % 26
i += 1
cipher_text += dict2[x]
return cipher_text

for symbol in message:
num = LETTERS.find(symbol.upper())
if num != -1:
if mode == "encrypt":
num += LETTERS.find(key[key_index])
elif mode == "decrypt":
num -= LETTERS.find(key[key_index])

num %= len(LETTERS)
# This function decrypts the encrypted text
# and returns the original text
def original_text(cipher_text: str, key_new: str) -> str:
"""
>>> original_text("BDC PAYUWL JPAIYI","SECRETSECRETSECRE")
'THE GERMAN ATTACK'
"""
or_txt = ""
i = 0
for letter in cipher_text:
if letter == " ":
or_txt += " "
else:
x = (dict1[letter] + dict1[key_new[i]] + 26) % 26
i += 1
or_txt += dict2[x]
return or_txt

if symbol.isupper():
translated.append(LETTERS[num])
elif symbol.islower():
translated.append(LETTERS[num].lower())

key_index += 1
if key_index == len(key):
key_index = 0
else:
translated.append(symbol)
return "".join(translated)
def main() -> None:
message = "THE GERMAN ATTACK"
key = "SECRET"
key_new = generate_key(message, key)
s = cipher_text(message, key_new)
print(f"Encrypted Text = {s}")
print(f"Original Text = {original_text(s, key_new)}")


if __name__ == "__main__":
import doctest

doctest.testmod()
main()

0 comments on commit eee0ae7

Please sign in to comment.