tea/xtea/xxtea加密算法
概述
这里记录一下采用python实现三种加密算法的方式,话不多说,直接上代码。
tea
from ctypes import *
def encrypt(v, k):
v0, v1 = c_uint32(v[0]), c_uint32(v[1])
delta = 0x9e3779b9
k0, k1, k2, k3 = k[0], k[1], k[2], k[3]
total = c_uint32(0)
for i in range(32):
total.value += delta
v0.value += ((v1.value<<4) + k0) ^ (v1.value + total.value) ^ ((v1.value>>5) + k1)
v1.value += ((v0.value<<4) + k2) ^ (v0.value + total.value) ^ ((v0.value>>5) + k3)
return v0.value, v1.value
def decrypt(v, k):
v0, v1 = c_uint32(v[0]), c_uint32(v[1])
delta = 0x9e3779b9
k0, k1, k2, k3 = k[0], k[1], k[2], k[3]
total = c_uint32(delta * 32)
for i in range(32):
v1.value -= ((v0.value<<4) + k2) ^ (v0.value + total.value) ^ ((v0.value>>5) + k3)
v0.value -= ((v1.value<<4) + k0) ^ (v1.value + total.value) ^ ((v1.value>>5) + k1)
total.value -= delta
return v0.value, v1.value
# test
if __name__ == "__main__":
# 待加密的明文,两个32位整型,即64bit的明文数据
value = [0x12345678, 0x78563412]
# 四个key,每个是32bit,即密钥长度为128bit
key = [0x1, 0x2, 0x3, 0x4]
print("Data is : ", hex(value[0]), hex(value[1]))
res = encrypt(value, key)
print("Encrypted data is : ", hex(res[0]), hex(res[1]))
res = decrypt(res, key)
print("Decrypted data is : ", hex(res[0]), hex(res[1]))
"""
Data is : 0x12345678 0x78563412
Encrypted data is : 0x9a65a69a 0x67ed00f6
Decrypted data is : 0x12345678 0x78563412
"""
xtea
from ctypes import *
def encrypt(v, key):
v0, v1 = c_uint32(v[0]), c_uint32(v[1])
delta = 0x9E3779B9
total = c_uint32(0)
for i in range(32):
v0.value += (((v1.value << 4) ^ (v1.value >> 5)) + v1.value) ^ (total.value + key[total.value & 3])
total.value += delta
v1.value += (((v0.value << 4) ^ (v0.value >> 5)) + v0.value) ^ (total.value + key[(total.value>>11) & 3])
return v0.value, v1.value
def decrypt(v, key):
v0, v1 = c_uint32(v[0]), c_uint32(v[1])
delta = 0x9E3779B9
total = c_uint32(delta * 32)
for i in range(32):
v1.value -= (((v0.value << 4) ^ (v0.value >> 5)) + v0.value) ^ (total.value + key[(total.value>>11) & 3])
total.value -= delta
v0.value -= (((v1.value << 4) ^ (v1.value >> 5)) + v1.value) ^ (total.value + key[total.value & 3])
return v0.value, v1.value
# test
if __name__ == "__main__":
# 待加密的明文,两个32位整型,即64bit的明文数据
value = [0x12345678, 0x78563412]
# 四个key,每个是32bit,即密钥长度为128bit
key = [0x1, 0x2, 0x3, 0x4]
print("Data is : ", hex(value[0]), hex(value[1]))
res = encrypt(value, key)
print("Encrypted data is : ", hex(res[0]), hex(res[1]))
res = decrypt(res, key)
print("Decrypted data is : ", hex(res[0]), hex(res[1]))
"""
Data is : 0x12345678 0x78563412
Encrypted data is : 0xae685ec7 0x59af4238
Decrypted data is : 0x12345678 0x78563412
"""
xxtea
from ctypes import *
def MX(z, y, total, key, p, e):
temp1 = (z.value>>5 ^ y.value<<2) + (y.value>>3 ^ z.value<<4)
temp2 = (total.value ^ y.value) + (key[(p&3) ^ e.value] ^ z.value)
return c_uint32(temp1 ^ temp2)
def encrypt(n, v, key):
delta = 0x9e3779b9
rounds = 6 + 52//n
total = c_uint32(0)
z = c_uint32(v[n-1])
e = c_uint32(0)
while rounds > 0:
total.value += delta
e.value = (total.value >> 2) & 3
for p in range(n-1):
y = c_uint32(v[p+1])
v[p] = c_uint32(v[p] + MX(z,y,total,key,p,e).value).value
z.value = v[p]
y = c_uint32(v[0])
v[n-1] = c_uint32(v[n-1] + MX(z,y,total,key,n-1,e).value).value
z.value = v[n-1]
rounds -= 1
return v
def decrypt(n, v, key):
delta = 0x9e3779b9
rounds = 6 + 52//n
total = c_uint32(rounds * delta)
y = c_uint32(v[0])
e = c_uint32(0)
while rounds > 0:
e.value = (total.value >> 2) & 3
for p in range(n-1, 0, -1):
z = c_uint32(v[p-1])
v[p] = c_uint32((v[p] - MX(z,y,total,key,p,e).value)).value
y.value = v[p]
z = c_uint32(v[n-1])
v[0] = c_uint32(v[0] - MX(z,y,total,key,0,e).value).value
y.value = v[0]
total.value -= delta
rounds -= 1
return v
# test
if __name__ == "__main__":
# 该算法中每次可加密不只64bit的数据,并且加密的轮数由加密数据长度决定
v = [0x12345678, 0x78563412]
k = [0x1, 0x2, 0x3, 0x4]
n = 2
print("Data is : ", hex(v[0]), hex(v[1]))
res = encrypt(n, v, k)
print("Encrypted data is : ", hex(res[0]), hex(res[1]))
res = decrypt(n, res, k)
print("Decrypted data is : ", hex(res[0]), hex(res[1]))
"""
Data is : 0x12345678 0x78563412
Encrypted data is : 0xef86c2bb 0x25f31b5e
Decrypted data is : 0x12345678 0x78563412
"""
总结
不忘初心,砥砺前行!