|
发表于 2016-11-15 13:20:11
|
显示全部楼层
本帖最后由 SixPy 于 2016-11-15 13:47 编辑
XOR crypt
- from itertools import islice, cycle
- def fn_XOR(txt,key):
- key=bytes(key,'ascii')
- return (a^b for a,b in zip(islice(cycle(key),len(txt)),txt))
- def crypt(txt,key):
- return ','.join(map(str, fn_XOR(bytes(txt,'ascii'),key)))
- def decry(txt,key):
- txt=bytes(map(int, txt.split(',')))
- return bytes(fn_XOR(txt,key)).decode()
- if __name__ == '__main__':
- while 1:
- flg = int(input('加密 0\n解密 1\n>'))
- if flg in (0,1): break
-
- txt = input('输入%s文内容:'% '明密'[flg])
- key = input('输入密码:')
- print('%s文:%s' % ('密明'[flg], decry(txt,key)if flg else crypt(txt,key)))
复制代码
- 加密 0
- 解密 1
- >0
- 输入明文内容:Father
- 输入密码:god
- 密文:33,14,16,15,10,22
- >>>
- 加密 0
- 解密 1
- >1
- 输入密文内容:33,14,16,15,10,22
- 输入密码:god
- 明文:Father
复制代码 |
|