keys_list = ['630731459228', '021812365430', '241780340594', '171247046833', '235740410948', '247480137613',
'001754439284', '460723819352', '632308295147', '384174723610', '597410324048', '246084171733']
ch = ['0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'a', 'b', 'c', 'd', 'e', 'f']
# 转换为二进制列表
def convert(tip):
tip1 = ''
for each in tip:
index = ch.index(each)
ea = bin(int(index)).replace("0b", '').zfill(4)
tip1 = tip1 + ea
tip_list = list(map(int,tip1))
return tip_list
# 转十六进制
def convert1(x):
y = 0
k = 0
while True:
k += 1
i = x % 10
if i == 1:
y = y + 2 ** (k - 1)
x = x // 10
if x == 0:
break
z = ''
while True:
k = y % 16
z = ch[k] + z
y = y // 16
if y == 0:
break
return z
# 加密
def encrypt(tips):
tip = tips[4:]
tip_list = convert(tip)
nu = len(tip_list)
# 调换一次顺序
for i in range(nu):
tip_list[i], tip_list[nu-1-i] = tip_list[nu-1-i], tip_list[i]
keys = int(tips[0:4])
key = keys_list[keys]
key_list = convert(key)
secre = []
secret = []
for i in range(nu):
secre.append(tip_list[i] ^ key_list[i])
for i in range(nu//4):
re = secre[4*i:i*4+4]
res = ''.join([str(i) for i in re])
secret.append(convert1(int(res)))
return tips[0:4]+"".join(secret)
# 解密
def decrypt(cipher):
tip = cipher[4:]
tip_list = convert(tip)
nu = len(tip_list)
keys = int(tips[0:4])
key = keys_list[keys]
key_list = convert(key)
secre = []
secret = []
for i in range(nu):
secre.append(tip_list[i] ^ key_list[i])
# 调换一次顺序
for i in range(nu):
secre[i], secre[nu-1-i] = secre[nu-1-i], secre[i]
for i in range(nu // 4):
re = secre[4*i:i*4+4]
res = ''.join([str(i) for i in re])
secret.append(convert1(int(res)))
return cipher[0:4] + "".join(secret)
if __name__ == '__main__':
tips = "000320180975f0ac"
encrypt = encrypt(tips)
decrypt = decrypt(encrypt)
print("加密:", encrypt)
print("解密:", decrypt)
网友评论