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

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

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

本文关键:[原创]TEA算法的VB实现代码
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top