Created
December 31, 2021 09:53
-
-
Save PageotD/994bed1da866e312a60bf586b5b02966 to your computer and use it in GitHub Desktop.
The Vigenère cipher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
def chartable(): | |
""" | |
Define universe, i.e the character table available for encryption and decryption | |
""" | |
universe = " " # space character | |
universe += "0123456789" # numbers | |
universe += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" # letters | |
universe += "!#$%&'()*+,-./:;<=>?@[\]^_`{|}" # special characters | |
return universe | |
def encrypt(msg, key, universe): | |
""" | |
Encrypt a message using a "secret" key | |
""" | |
# Initialize encrypted message | |
enmsg = "" | |
# Initialize key index | |
j = 0 | |
# Loop over characters in input message to encrypt | |
for i in range(0, len(msg)): | |
# If the character is in the universe, it is replaced | |
if msg[i] in universe: | |
# Get the index of the message char `i` in universe | |
ichrmsg = universe.index(msg[i]) | |
# Get the index of the key char `j` in universe | |
ichrkey = universe.index(key[j%len(key)]) | |
# Calculate the index of the encrypted message char | |
ichrencrypt = ( ichrmsg + ichrkey) % len(universe) | |
# Add the new char in the encrypted message | |
enmsg += universe[ichrencrypt] | |
# Increment key index by 1 | |
j += 1 | |
# If the character is not in the universe, it is not replaced | |
else: | |
# Add the unchanged char in the encrypted message | |
enmsg += msg[i] | |
return enmsg | |
def decrypt(msg, key, universe): | |
""" | |
Decrypt a message using a "secret" key | |
""" | |
# Initialize decrypted message | |
demsg = "" | |
# Initialize key index | |
j = 0 | |
# Loop over characters in input message to decrypt | |
for i in range(0, len(msg)): | |
# If the character is in the universe, it is replaced | |
if msg[i] in universe: | |
# Get the index of the message char `i` in universe | |
ichrmsg = universe.index(msg[i]) | |
# Get the index of the key char `j` in universe | |
ichrkey = universe.index(key[j%len(key)]) | |
# Calculate the index of the decrypted message char | |
ichrencrypt = ( ichrmsg - ichrkey) % len(universe) | |
# Add the new char in the decrypted message | |
demsg += universe[ichrencrypt] | |
# Increment key index by 1 | |
j += 1 | |
# If the character is not in the universe, it is not replaced | |
else: | |
# Add the unchanged char in the decrypted message | |
demsg += msg[i] | |
return demsg | |
if __name__ == "__main__": | |
# Initialize character table | |
universe = chartable() | |
# Message to encrypt | |
msg = "Happy New Year!" | |
key = "The Dude" | |
# Print message and key in terminal | |
print(msg+' | '+key) | |
# Encrypt message | |
enmsg = encrypt(msg, key, universe) | |
# Print encrypted message | |
print('encrypt:: ' + enmsg) | |
# Decrypt message | |
demsg = decrypt(enmsg, key, universe) | |
# Print decrypted message | |
print('decrypt:: ' + demsg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment