Created
July 5, 2019 17:24
-
-
Save kupp1/78be4c69beace1df8212fae9dcb9119f to your computer and use it in GitHub Desktop.
Vigenere cipher
This file contains 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
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