1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| from ctypes import * def MX(z, y, sum1, k, p, e): return c_uint32(((z.value>>5^y.value<<2)+(y.value>>3^z.value<<4))^((sum1.value^y.value)+(k[(p&3)^e.value]^z.value))) def btea(v,k,n,delta): if n>1: sum1=c_uint32(0) z=c_uint32(v[n-1]) rounds=6+52//n e=c_uint32(0) while rounds>0: sum1.value+=delta e.value=((sum1.value>>2)&3) for p in range(n-1): y=c_uint32(v[p+1]) v[p] = c_uint32(v[p] + MX(z,y,sum1,k,p,e).value).value z.value=v[p] y=c_uint32(v[0]) v[n-1] = c_uint32(v[n-1] + MX(z,y,sum1,k,n-1,e).value).value z.value=v[n-1] rounds-=1 else: sum1=c_uint32(0) n=-n rounds=6+52//n sum1.value=rounds*delta y=c_uint32(v[0]) e=c_uint32(0) while rounds>0: e.value=((sum1.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,sum1,k,p,e).value).value y.value=v[p] z=c_uint32(v[n-1]) v[0] = c_uint32(v[0] - MX(z,y,sum1,k,0,e).value).value y.value=v[0] sum1.value-=delta rounds-=1 return v if __name__=='__main__': a=[689085350 ,626885696 ,1894439255 ,1204672445 ,1869189675 ,475967424 ,1932042439 ,1280104741 ,2808893494] k=[12345678 ,12398712 ,91283904 ,12378192] delta=0x9e3779b9 n=9 res=btea(a,k,-n,delta) print("解密后数据:",res)
|