python 异或加密字符串的实例
更新时间:2018年10月14日 16:12:51 作者:独一无二的小个性
今天小编就为大家分享一篇python 异或加密字符串的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
做个简单习题:输入明文给定秘钥,密文还原,按位异或处理。
import base64 as b64 def xor_encrypt(tips,key): ltips=len(tips) lkey=len(key) secret=[] num=0 for each in tips: if num>=lkey: num=num%lkey secret.append( chr( ord(each)^ord(key[num]) ) ) num+=1 return b64.b64encode( "".join( secret ).encode() ).decode() def xor_decrypt(secret,key): tips = b64.b64decode( secret.encode() ).decode() ltips=len(tips) lkey=len(key) secret=[] num=0 for each in tips: if num>=lkey: num=num%lkey secret.append( chr( ord(each)^ord(key[num]) ) ) num+=1 return "".join( secret ) tips= "1234567" key= "owen" secret = xor_encrypt(tips,key) print( "cipher_text:", secret ) plaintxt = xor_decrypt( secret, key ) print( "plain_text:",plaintxt )
以上这篇python 异或加密字符串的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Python itertools.product方法代码实例
这篇文章主要介绍了Python itertools.product方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2020-03-03


最新评论