本文主要是介绍xxtea加密算法-忘记是从哪里看到的了,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
- xxtea.py
#!/usr/bin/python
# -*- coding:utf-8 -*-import structdef btea(v, n, k): #xxtea加密算法MX = lambda: ((z>>5)^(y<<2)) + ((y>>3)^(z<<4))^(sum^y) + (k[(p & 3)^e]^z)u32 = lambda x: x & 0xffffffffy = v[0]sum = 0DELTA = 0x9e3779b9if n > 1: z = v[n-1]q = 6 + 52 / nwhile q > 0:q -= 1sum = u32(sum + DELTA)e = u32(sum >> 2) & 3p = 0while p < n - 1:y = v[p+1]z = v[p] = u32(v[p] + MX())p += 1y = v[0]z = v[n-1] = u32(v[n-1] + MX())return Trueelif n < -1:n = -nq = 6 + 52 / nsum = u32(q * DELTA)while sum != 0:e = u32(sum >> 2) & 3p = n - 1while p > 0:z = v[p-1]y = v[p] = u32(v[p] - MX())p -= 1z = v[n-1]y = v[0] = u32(v[0] - MX())sum = u32(sum - DELTA)return Truereturn Falseif __name__ == '__main__': #简单试验print "key", "1234567890123456"key = struct.unpack("=iiii", "1234567890123456")v = [110, 111, 112, 113]print vbtea(v, 4, key)print vbtea(v, -4, key)print v
- 测试
admin$ python xxtea.py
key 1234567890123456
[110, 111, 112, 113]
[2210921364, 249748794, 3317857568, 1737089717]
[110, 111, 112, 113]
admin$
这篇关于xxtea加密算法-忘记是从哪里看到的了的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!