Skip to content

Instantly share code, notes, and snippets.

@PyYoshi
Created June 9, 2012 10:18
Show Gist options
  • Select an option

  • Save PyYoshi/2900433 to your computer and use it in GitHub Desktop.

Select an option

Save PyYoshi/2900433 to your computer and use it in GitHub Desktop.
Pythonで文字列をXORする関数 ref: http://qiita.com/items/dc60edfed7afc1d302e7
# coding:utf-8
import base64
from itertools import cycle,izip
def xor_enc_base64(ss, key):
key = cycle(key)
return base64.b64encode(''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(ss, key)))
def xor_dec_base64(ss, key):
key = cycle(key)
return ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(base64.b64decode(ss), key))
def main():
mes = 'hello! can you understand this message?'
key = 'world'
e = xor_enc_base64(mes,key)
d = xor_dec_base64(e,key)
print d
if __name__ in '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment