vigenere cipher Algorithm

The Vigenère cipher is a classical encryption algorithm that was invented by Giovan Battista Bellaso in the mid-16th century and later misattributed to Blaise de Vigenère in the 19th century. This symmetric key cipher is a form of polyalphabetic substitution, meaning it utilizes multiple substitution alphabets to encrypt plaintext messages. The Vigenère cipher is an extension of the Caesar cipher, which uses a simple shift of letters within the alphabet, while the Vigenère cipher uses a keyword to determine the shift. This added complexity provides greater security than monoalphabetic substitution ciphers like the Caesar cipher, as it makes frequency analysis more difficult for would-be attackers. To implement the Vigenère cipher, a tabula recta, also known as the Vigenère square or Vigenère table, is used. This table consists of 26 rows of shifted alphabets, one for each letter of the alphabet. To encrypt a plaintext message, a keyword is chosen, which is then repeated as many times as necessary to match the length of the plaintext. Each letter of the plaintext is then encrypted by finding the intersection of the row corresponding to the keyword letter and the column corresponding to the plaintext letter in the tabula recta. The resulting ciphertext is formed by replacing each plaintext letter with the letter found at the intersection. Decryption is performed by reversing the process, using the keyword to locate the correct row in the tabula recta and finding the corresponding plaintext letter in the column. Despite its increased security compared to monoalphabetic substitution ciphers, the Vigenère cipher is vulnerable to attacks based on Kasiski examination and Friedman tests, which can reveal the keyword length and aid in deciphering the ciphertext.
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"


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

    if mode.lower().startswith("e"):
        mode = "encrypt"
        translated = encryptMessage(key, message)
    elif mode.lower().startswith("d"):
        mode = "decrypt"
        translated = decryptMessage(key, message)

    print("\n%sed message:" % mode.title())
    print(translated)


def encryptMessage(key, message):
    """
    >>> encryptMessage('HDarji', 'This is Harshil Darji from Dharmaj.')
    'Akij ra Odrjqqs Gaisq muod Mphumrs.'
    """
    return translateMessage(key, message, "encrypt")


def decryptMessage(key, message):
    """
    >>> decryptMessage('HDarji', 'Akij ra Odrjqqs Gaisq muod Mphumrs.')
    'This is Harshil Darji from Dharmaj.'
    """
    return translateMessage(key, message, "decrypt")


def translateMessage(key, message, mode):
    translated = []
    keyIndex = 0
    key = key.upper()

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

            num %= len(LETTERS)

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

            keyIndex += 1
            if keyIndex == len(key):
                keyIndex = 0
        else:
            translated.append(symbol)
    return "".join(translated)


if __name__ == "__main__":
    main()

LANGUAGE:

DARK MODE: