XOR加密

XOR加密

参考链接:XOR 加密简介

文章中第五点为基础

#coding=utf-8

#md5算法
import hashlib
h_md5 = hashlib.md5()
x = input("请输入要加密的文字:")
print( "Text:",x)
data = x.encode('utf-8', errors='ignore')   #加密,同时忽略可能带来的错误
h_md5.update(data)
md5_str = h_md5.hexdigest()
print("MD5:",md5_str)


#形成key
#随机生成一个32位的十六进制,0~F,作为一次性key
from random import *
n = "".join([choice("0123456789ABCDEF") for i in range(32)]) 
print ( "Key:", n)


#xor运算
txt1 = []
k = ''
u = ''
for a in range(32):
    i = '0x' + md5_str[a]
    j = '0x' + n[a]
    x=bin(eval(i)).replace('0b','')        #十六进制转二进制
    y=bin(eval(j)).replace('0b','')
    for c in range(min(len(x),len(y))):
        if (x[-c] == y[-c])  :
            e = '0'
        elif(x[-c] != y[-c]):
            e = '1'
        k = e + k
    k ='0b' + k
    d = k
    k = ''    
    s = hex(eval(d)).replace('0x','')      #二进制转十六进制
    u = u + s
print("CipherText:",u)

posted @ 2021-10-25 21:24  20211408王其  阅读(231)  评论(1编辑  收藏  举报