Skip to content

Instantly share code, notes, and snippets.

@kupp1
Created July 5, 2019 17:24
Show Gist options
  • Save kupp1/78be4c69beace1df8212fae9dcb9119f to your computer and use it in GitHub Desktop.
Save kupp1/78be4c69beace1df8212fae9dcb9119f to your computer and use it in GitHub Desktop.
Vigenere cipher
def vigenere(msg, key, crypt_flag=True):
"""
vigenere cipher for small latin letters
crypt_flag == True mean crypt
crypt_flag == False mean decrypt
"""
msg, key = list(map(ord, msg)), list(map(ord, key))
msglen, keylen = len(msg), len(key)
new_msg = ''
for i in range(msglen):
offset = key[i % keylen] - 97
old_chr = msg[i] - 97
new_chr = (old_chr + (offset if crypt else -offset)) % 26
new_msg += chr(97 + new_chr)
return new_msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment