首页 > 开发 > 综合 > 正文

TEA算法的VB实现代码

2024-07-21 02:20:36
字体:
来源:转载
供稿:网友
 

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

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

'       ----------------------------------------------------------------------------------
'      |                                                                                  |
'      |                                标准tea加密解密模块                               |
'      |                                     zeffy 制作                                   |
'      |                                     qq:8481862                                   |
'      |                             email:[email protected]                             |
'      |                                                                                  |
'      |                                     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


中国最大的web开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表