Created
June 9, 2012 10:18
-
-
Save PyYoshi/2900433 to your computer and use it in GitHub Desktop.
Pythonで文字列をXORする関数 ref: http://qiita.com/items/dc60edfed7afc1d302e7
This file contains hidden or 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
| # 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