The Vigenère Cipher is a method of encrypting alphabetic text using a keyword to shift each letter in the message. It’s a polyalphabetic substitution cipher that is more secure than the Caesar Cipher by utilizing different shifts based on a keyword. However, traditional key management introduces vulnerabilities that can be improved using the Auto-Key System.
This implementation offers encryption and decryption using both the traditional Vigenère cipher and an enhanced version with the Auto-Key System to strengthen security.
- Encrypts and decrypts messages using both traditional Vigenère cipher and the Auto-Key System.
- Customizable keyword for encryption and decryption.
- Supports both uppercase and lowercase letters.
- Ignores non-alphabetic characters (like punctuation and spaces).
- Provides a more secure Auto-Key System to reduce repetition vulnerabilities.
The Vigenère Cipher relies on a repeating key to determine how much each letter of the message is shifted. Traditionally, the key is repeated to match the length of the message. With the Auto-Key System, the key is extended by appending part of the message itself to the key, making the cipher more resilient to certain cryptographic attacks.
- The key is either repeated (in the traditional Vigenère cipher) or extended using the Auto-Key System.
- Each letter in the plaintext is shifted forward in the alphabet by the position of the corresponding letter in the key.
- The key is repeated or extended to match the length of the ciphertext.
- Each letter in the ciphertext is shifted backward in the alphabet by the position of the corresponding letter in the key, returning the original plaintext.
In the traditional Vigenère Cipher, the keyword is repeated to match the length of the plaintext. This introduces a vulnerability: key repetition. If the keyword is short and the plaintext is long, the repeated key can be detected through frequency analysis, weakening the security of the cipher.
- Plaintext:
HELLOWORLD
- Keyword:
KEY
- Repeated Key:
KEYKEYKEYK
This repetition exposes patterns in the ciphertext, allowing cryptanalysts to perform statistical attacks and deduce the key.
To mitigate the problem of key repetition, we can use the Auto-Key System. Instead of repeating the key, we append the message itself to the key, reducing redundancy and making it harder to detect patterns.
- Initial Key: The original keyword is used for the first part of the message.
- Auto-Key Extension: After the keyword is exhausted, the remaining characters of the key are derived from the plaintext itself.
- Plaintext:
HELLOWORLD
- Keyword:
KEY
- Auto-Key:
KEYHELLOWO
This method ensures that the key is non-repetitive and unique for every message, significantly enhancing security against frequency analysis.
You can use the provided Python functions to encrypt and decrypt messages with either the traditional Vigenère cipher or the Auto-Key system. The encrypt_vigenere
and decrypt_vigenere
functions both require a message and a keyword, while the generate_auto_key
function allows you to apply the Auto-Key system for enhanced security.
text_to_encrypt = "HELLOWORLD"
key = "KEY"
'''
To test the autokey implementation, change the generate_key()
to generate_auto_key() inside encrypt_vigenere() and decrypt_vigenere()
functions
'''
# Encrypt the message with the auto-key
encrypted_text = encrypt_vigenere(text_to_encrypt, key)
print(f"Encrypted Text with Auto-Key: {encrypted_text}")
# Decrypt the message with the auto-key
decrypted_text = decrypt_vigenere(encrypted_text, key)
print(f"Decrypted Text with Auto-Key: {decrypted_text}")
generate_key(msg, key)
: Generates the traditional repeated key by matching the message length.generate_auto_key(msg, key)
: Generates an extended key using the Auto-Key System for enhanced security.encrypt_vigenere(msg, key)
: Encrypts the message using the Vigenère Cipher.decrypt_vigenere(msg, key)
: Decrypts the message using the Vigenère Cipher.
- Plaintext:
HELLOWORLD
- Keyword:
KEY
- Repeated Key:
KEYKEYKEYK
- Encrypted Text:
RIJVSUYVJN
- Plaintext:
HELLOWORLD
- Keyword:
KEY
- Auto-Key:
KEYHELLOWO
- Encrypted Text:
RIJVS UYVJN
- Ciphertext:
RIJVSUYVJN
- Auto-Key:
KEYHELLOWO
- Decrypted Text:
HELLOWORLD