做个简单习题:输入明文给定秘钥,密文还原,按位异或处理。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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 异或加密字符串的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/u010649766/article/details/79174580