分享
 
 
 

[原创]TEA算法的VB实现代码

王朝vb·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

前些日子不少人都要挂QQ,有客户需求就自然有人去研究,所以不少人开始研究QQ的协议.其中最重要的一步就是研究QQ使用的加密算法---TEA算法

关于TEA算法的描述以及C语言源码这里就不多说了,现在给出该算法的VB实现

' ----------------------------------------------------------------------------------

' | |

' | 标准TEA加密解密模块 |

' | Zeffy 制作 |

' | QQ:8481862 |

' | Email:Zephyrmage@163.com |

' | |

' | 2005.8.16 |

' | |

' ----------------------------------------------------------------------------------

Private Const OFFSET_4 = 4294967296# '&h100000000 的数值

Private Const MAXINT_4 = 2147483647 '整型数据所能表示的最大正数值 (&h7fffffff)

Private Const Bit_32 = 2147483648# '&h80000000 的正整数形式

Private Const DELTA = &H9E3779B9 'TEA算法的DELTA值

Private Function AddLong(lX As Long, lY As Long) As Long '长整数加法函数

Dim lX4 As Long

Dim lY4 As Long

Dim lX8 As Long

Dim lY8 As Long

Dim lResult As Long

lX8 = lX And &H80000000

lY8 = lY And &H80000000

lX4 = lX And &H40000000

lY4 = lY And &H40000000

lResult = (lX And &H3FFFFFFF) + (lY And &H3FFFFFFF)

If lX4 And lY4 Then

lResult = lResult Xor &H80000000 Xor lX8 Xor lY8

ElseIf lX4 Or lY4 Then

If lResult And &H40000000 Then

lResult = lResult Xor &HC0000000 Xor lX8 Xor lY8

Else

lResult = lResult Xor &H40000000 Xor lX8 Xor lY8

End If

Else

lResult = lResult Xor lX8 Xor lY8

End If

AddLong = lResult

End Function

Private Function SubtractLong(lX As Long, lY As Long) As Long '长整数减法函数

Dim lX8 As Long

Dim lY8 As Long

Dim mX As Double

Dim mY As Double

Dim mResult As Double

Dim lResult As Long

lX8 = lX And &H80000000

lY8 = lY And &H80000000

mX = lX And &H7FFFFFFF

mY = lY And &H7FFFFFFF

If lX8 Then

If lY8 Then

mResult = mX - mY

Else

mX = mX + Bit_32

mResult = mX - mY

End If

Else

If lY8 Then

mY = lY

mResult = mX - mY

Else

mResult = mX - mY

End If

End If

If mResult < 0 Then

lResult = ((Bit_32 + mResult) Or &H80000000) And &HFFFFFFFF

ElseIf mResult > MAXINT_4 Then

lResult = ((mResult - Bit_32) Or &H80000000) And &HFFFFFFFF

Else

lResult = mResult And &HFFFFFFFF

End If

SubtractLong = lResult

End Function

Private Function LeftRotateLong(lValue As Long, lBits As Integer) As Long '按位左移函数

Dim lngSign As Long, intI As Integer

Dim mValue As Long

lBits = lBits Mod 32

mValue = lValue

If lBits = 0 Then LeftRotateLong = mValue: Exit Function

For intI = 1 To lBits

lngSign = mValue And &H40000000

mValue = (mValue And &H3FFFFFFF) * 2

If lngSign And &H40000000 Then

mValue = mValue Or &H80000000

End If

Next

LeftRotateLong = mValue

End Function

Private Function RightRotateLong(lValue As Long, lBits As Integer) As Long '按位右移函数

Dim lngSign As Long, intI As Integer

Dim mValue As Long

mValue = lValue

lBits = lBits Mod 32

If lBits = 0 Then RightRotateLong = mValue: Exit Function

For intI = 1 To lBits

lngSign = mValue And &H80000000

mValue = (mValue And &H7FFFFFFF) \ 2

If lngSign Then

mValue = mValue Or &H40000000

End If

Next

RightRotateLong = mValue

End Function

Public Sub TeaEncode(v() As Long, k() As Long, lType As Integer) '标准的TEA加密过程,参数lType 为1时表示16轮迭代(QQ使用的就是16轮迭代),否则为32轮迭代

Dim Y As Long, Z As Long

Dim K1 As Long, K2 As Long, K3 As Long, K4 As Long

Dim L1 As Long, L2 As Long, L3 As Long, L4 As Long

Dim Sum As Long

Dim i As Integer, Rounds As Integer

Dim mResult(0 To 1) As Long

Y = v(0)

Z = v(1)

K1 = k(0)

K2 = k(1)

K3 = k(2)

K4 = k(3)

If lType = 1 Then

Rounds = 16

Else

Rounds = 32

End If

For i = 1 To Rounds

'sum += delta ;

Sum = AddLong(Sum, DELTA)

'y += (z<<4)+k[0] ^ z+sum ^ (z>>5)+k[1]

L1 = LeftRotateLong(Z, 4)

L1 = AddLong(L1, K1)

L2 = AddLong(Z, Sum)

L3 = RightRotateLong(Z, 5)

L3 = AddLong(L3, K2)

L4 = L1 Xor L2 Xor L3

Y = AddLong(Y, L4)

'z += (y<<4)+k[2] ^ y+sum ^ (y>>5)+k[3]

L1 = LeftRotateLong(Y, 4)

L1 = AddLong(L1, K3)

L2 = AddLong(Y, Sum)

L3 = RightRotateLong(Y, 5)

L3 = AddLong(L3, K4)

L4 = L1 Xor L2 Xor L3

Z = AddLong(Z, L4)

Next

v(0) = Y

v(1) = Z

End Sub

Public Sub TeaDecode(v() As Long, k() As Long, lType As Integer) '标准TEA解密过程,参数lType 为1时表示16轮迭代(QQ使用的就是16轮迭代),否则为32轮迭代

Dim Y As Long, Z As Long

Dim K1 As Long, K2 As Long, K3 As Long, K4 As Long

Dim L1 As Long, L2 As Long, L3 As Long, L4 As Long

Dim Sum As Long

Dim i As Integer, Rounds As Integer

Dim mResult(0 To 1) As Long

Y = v(0)

Z = v(1)

K1 = k(0)

K2 = k(1)

K3 = k(2)

K4 = k(3)

If lType = 1 Then

Rounds = 16

Sum = LeftRotateLong(DELTA, 4)

Else

Rounds = 32

Sum = LeftRotateLong(DELTA, 5)

End If

For i = 1 To Rounds

L1 = LeftRotateLong(Y, 4)

L1 = AddLong(L1, K3)

L2 = AddLong(Y, Sum)

L3 = RightRotateLong(Y, 5)

L3 = AddLong(L3, K4)

L4 = L1 Xor L2 Xor L3

Z = SubtractLong(Z, L4)

L1 = LeftRotateLong(Z, 4)

L1 = AddLong(L1, K1)

L2 = AddLong(Z, Sum)

L3 = RightRotateLong(Z, 5)

L3 = AddLong(L3, K2)

L4 = L1 Xor L2 Xor L3

Y = SubtractLong(Y, L4)

Sum = SubtractLong(Sum, DELTA)

Next

v(0) = Y

v(1) = Z

End Sub

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有