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 )
相关推荐
哈嘿Blog 2020-10-26
明月清风精进不止 2020-07-05
PythonMaker 2020-07-05
xirongxudlut 2020-06-28
kkpiece 2020-06-16
qscool 2020-06-12
CloudXli 2020-06-11
vs00ASPNET 2020-06-09
Dimples 2020-06-08
kuoying 2020-06-07
JJandYY 2020-05-31
Wyt00 2020-05-30
liuyh 2020-04-03
CloudXli 2020-05-11
世樹 2020-05-11
bizercsdn 2020-05-10
joyjoy0 2020-05-09