-
-
Save JoshuaWierenga/2237129380d93697bc90 to your computer and use it in GitHub Desktop.
vigenere encoding and decoding in python
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
#----------------------------------------------------- | |
# Uses vigenere.py by ilogik | |
# https://gist.github.com/ilogik/6f9431e4588015ecb194 | |
#----------------------------------------------------- | |
import base64 | |
def encode(key, string): | |
encoded_chars = [] | |
for i in xrange(len(string)): | |
key_c = key[i % len(key)] | |
encoded_c = chr(ord(string[i]) + ord(key_c) % 256) | |
encoded_chars.append(encoded_c) | |
encoded_string = "".join(encoded_chars) | |
return base64.urlsafe_b64encode(encoded_string) | |
def decode(key, string): | |
decoded_chars = [] | |
string = base64.urlsafe_b64decode(string) | |
for i in xrange(len(string)): | |
key_c = key[i % len(key)] | |
encoded_c = chr(abs(ord(string[i]) - ord(key_c) % 256)) | |
decoded_chars.append(encoded_c) | |
decoded_string = "".join(decoded_chars) | |
return decoded_string | |
while True: | |
program = raw_input("encode or decode:") | |
if program == "encode": | |
string = raw_input("Thing to encode:") | |
key = raw_input("key:") | |
encrypted = encode(key, string) | |
print(encrypted) | |
if program == "decode": | |
string = raw_input("Thing to decode:") | |
key = raw_input("key:") | |
decrypted = decode(key, string) | |
print(decrypted) | |
else: | |
print("Unknown input, Please enter encode or decode") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment