VB implementation code of Tea algorithm

Source: Internet
Author: User
Tags bitwise exit integer rounds
Algorithm

Some days ago many people have to hang QQ, there are customers need to study the nature, so many people began to study the QQ agreement. One of the most important steps is to study the encryption algorithm used in QQ---tea algorithm

About tea algorithm description and C language source code here is not much to say, now give the algorithm of VB implementation

'       ----------------------------------------------------------------------------------
'      | |
'      | Standard tea encryption and Decryption module |
'      | Zeffy Production |
'      | qq:8481862 |
'      | email:zephyrmage@163.com |
'      | |
'      | 2005.8.16 |
'      | |
'       ----------------------------------------------------------------------------------

Private Const offset_4 = value of 4294967296# ' &h100000000
The maximum positive value that the Private Const maxint_4 = 2147483647 ' integer data can represent (&H7FFFFFFF)
Private Const bit_32 = positive integer form of 2147483648# ' &h80000000
Private Const delta = delta value for &H9E3779B9 ' tea algorithm

Private function Addlong (LX as Long, LY as Long) as Long integer addition function
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 integer subtraction function
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 &AMP;HFFFFFFFF
ElseIf mresult > Maxint_4 Then
LRESULT = ((mresult-bit_32) Or &h80000000) and &AMP;HFFFFFFFF
Else
LRESULT = Mresult and &hffffffff
End If

Subtractlong = Lresult

End Function

Private function Leftrotatelong (lValue as Long, lbits as Integer) as long ' bitwise Left function
    Dim lngsign as Long, IntI as Integer
    Dim mvalue as Long
   
    lbits = lbits Mod
    mvalue = lValue
    If lbits = 0 Then leftrotatelong = mvalue:exit functi On
   
    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 ' bitwise right SHIFT functions
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) ' Standard tea encryption process, with parameter Ltype 1 for 16-wheel iterations (QQ uses 16-wheel iterations), or 32-round iterations
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) ' Standard tea decryption process, with parameter Ltype 1 for 16-wheel iterations (QQ uses 16-wheel iterations), or 32-round iterations    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, Roun DS as Integer
   Dim mresult (0 to 1) as Long
     
   Y = V (0)
& nbsp;  Z = V (1)
   K1 = k (0)
   K2 = k (1)
   K3 = k (2)
   K4 = k ( 3
  
   If ltype = 1 Then
      Rounds =
       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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.