python利用异或算法实现字符串加密解密
代码:
def crypt(source,key):
from itertools import cycle
result=''
temp=cycle(key)
for ch in source:
result = result +chr(ord(ch)^ord(next(temp)))
return result
source ='jiangnan Institute of Business and Technology'
key = 'xxk'
print('Before Encrypted:'+source)
encrypted = crypt(source, key)#加密
print('After Encrypted:'+encrypted)
decrypted = crypt(encrypted, key)#解密
print('After decrypted:'+decrypted)
结果: