PythonXOR处理
Python密码学-XOR处理详细操作教程
在本章中,让我们了解XOR流程及其在Python中的编码。
算法
加密和解密的XOR算法将纯文本格式转换为ASCII字节,并使用XOR过程将其转换为指定的字节。它为用户提供了以下优势-
快速计算
左右没有区别
易于理解和分析
代码
您可以使用以下代码执行XOR处理-
# Filename : example.py
# Copyright : 2020 By Lidihuo
# Author by : www.lidihuo.com
# Date : 2020-08-28
def xor_crypt_string(data, key = 'awesomepassword', encode = False, decode = False):
from itertools import izip, cycle
import base64
if decode:
data = base64.decodestring(data)
xored = ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(data, cycle(key)))
if encode:
return base64.encodestring(xored).strip()
return xored
secret_data = "Xor procedure"
print("The cipher text is")
print xor_crypt_string(secret_data, encode = True)
print("The plain text fetched")
print xor_crypt_string(xor_crypt_string(secret_data, encode = True), decode = True)
输出
XOR处理的代码为您提供以下输出-
说明
函数 xor_crypt_string()包含用于指定编码和解码模式以及字符串值的参数。
base64模块具有基本功能,该模块遵循XOR过程/操作对明文/密文进行加密或解密。
注意-XOR加密用于加密数据,很难通过蛮力方法破解,即通过生成随机加密密钥来匹配正确的密文。