Skip to content

Instantly share code, notes, and snippets.

@adrianmester
Created July 23, 2013 20:21
Show Gist options
  • Save adrianmester/6f9431e4588015ecb194 to your computer and use it in GitHub Desktop.
Save adrianmester/6f9431e4588015ecb194 to your computer and use it in GitHub Desktop.
Vigenere Cypther
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
@csmithd
Copy link

csmithd commented Oct 18, 2014

Missing the colon on line 4.

@keeganjk
Copy link

keeganjk commented Jul 7, 2017

You're missing a colon on line 4. def encode(key, string) should be def encode(key, string):.

@myegorov
Copy link

myegorov commented Oct 9, 2017

Modulo operator (%) has higher precedence than either + or -, so line 8 is missing parens:

encoded_c = chr( (ord(string[i]) + ord(key_c)) % 256)

Ditto line 19.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment