secret
Created

Vigenere Cypther

  • Download Gist
vigenere.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.