返回列表 发帖

[转载] VB实现DES

  1.     '
  2.     '======= 私有变量 =======
  3.      
  4.     Private ip(63) As Byte, ip_1(63) As Byte, e(47) As Byte '数据变换
  5.     Private pc_1(55) As Integer, pc_2(47) As Integer, ccmovebit(15) As Integer '密钥生成
  6.     Private p(31) As Byte, ss(7, 3, 15) As Byte 'S变换
  7.     Private key_n1(15, 7) As Byte '密钥1
  8.     Private key_n2(15, 7) As Byte '密钥2
  9.      
  10.     '
  11.     '======= API =========
  12.     Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
  13.      
  14.      
  15.     '
  16.     '***************************************************************
  17.     '
  18.     '==================== 下面是类的函数及方法 ===================
  19.     '
  20.     '***************************************************************
  21.      
  22.      
  23.      
  24.      
  25.      
  26.      
  27.     '==========================================================================================
  28.     ' SetKey 函数说明:
  29.     ' 设置3DES加/解密的密钥
  30.     ' 返回:
  31.     ' 无
  32.     ' 参数:
  33.     ' K_Bit() AS Byte 密钥,不少于16个元素
  34.     '==========================================================================================
  35.     Sub SetKey(K_Bit() As Byte)
  36.      Dim Key() As Byte
  37.      Dim K_Bit1(7) As Byte
  38.      Dim K_Bit2(7) As Byte
  39.      
  40.      Key = K_Bit
  41.      ReDim Preserve Key(15) As Byte
  42.      
  43.      
  44.      CopyMemory K_Bit1(0), Key(0), 8
  45.      CopyMemory K_Bit2(0), Key(8), 8
  46.      
  47.      '//根据密钥生成16个子密钥
  48.      GenSubKey K_Bit1(), key_n1()
  49.      GenSubKey K_Bit2(), key_n2()
  50.     End Sub
  51.      
  52.      
  53.     '==========================================================================================
  54.     ' SetKey1 函数说明:
  55.     ' 设置3DES加/解密的的第一个密钥
  56.     ' 返回:
  57.     ' 无
  58.     ' 参数:
  59.     ' K_Bit() AS Byte 密钥,不少于8个元素
  60.     '==========================================================================================
  61.     Sub SetKey1(K_Bit() As Byte)
  62.      '//根据密钥生成16个子密钥
  63.      Dim Key() As Byte
  64.      ReDim Preserve Key(7) As Byte
  65.      GenSubKey Key(), key_n1()
  66.     End Sub
  67.      
  68.      
  69.      
  70.     '=========================================================================================
  71.     ' SetKey2 函数说明:
  72.     ' 设置3DES加/解密的的第二个密钥
  73.     ' 返回:
  74.     ' 无
  75.     ' 参数:
  76.     ' K_Bit() AS Byte 密钥,不少于8个元素
  77.     '==========================================================================================
  78.     Sub SetKey2(K_Bit() As Byte)
  79.      '//根据密钥生成16个子密钥
  80.      Dim Key() As Byte
  81.      ReDim Preserve Key(7) As Byte
  82.      GenSubKey Key(), key_n2()
  83.     End Sub
  84.      
  85.      
  86.      
  87.     '==========================================================================================
  88.     ' Encrypt3Des_ArrToArr 函数说明:
  89.     ' 3DES加/解密
  90.     ' 返回:
  91.     ' 无
  92.     ' 参数:
  93.     ' m_bit() As Byte 输入
  94.     ' e_bit() As Byte 输出 ,元素个数为8的倍数
  95.     ' bFlag As Byte 可选输入,为2则解密,其它或为缺省时为加密
  96.     '==========================================================================================
  97.     Sub Encrypt3Des_ArrToArr(m_bit() As Byte, e_bit() As Byte, Optional bFlag As Byte = 1)
  98.      Dim i As Integer
  99.      Dim ina() As Byte, outa() As Byte
  100.      
  101.      ina = m_bit
  102.      EncryptDes_ArrToArr ina(), outa(), 1, bFlag
  103.      ina = outa
  104.      EncryptDes_ArrToArr ina(), outa(), 2, 3 - bFlag
  105.      ina = outa
  106.      EncryptDes_ArrToArr ina(), outa(), 1, bFlag
  107.      ReDim e_bit(UBound(outa)) As Byte
  108.      'For i = 0 To UBound(outa)
  109.      ' e_bit(i) = outa(i)
  110.      'Next
  111.      CopyMemory e_bit(0), outa(0), UBound(outa) + 1
  112.      
  113.     End Sub
  114.      
  115.      
  116.      
  117.     '==========================================================================================
  118.     ' EncryptDes_ArrToArr 函数说明:
  119.     ' DES加/解密
  120.     ' 返回:
  121.     ' 无
  122.     ' 参数:
  123.     ' m_bit() As Byte 输入
  124.     ' e_bit() As Byte 输出 ,元素个数为8的倍数
  125.     ' bUseKeyNo As Byte 可选输入,为2则使用第二个密钥加/解密,否则使用第一个密钥加/解密
  126.     ' bFlag As Byte 可选输入,为2则解密,否则加密
  127.     '==========================================================================================
  128.     Sub EncryptDes_ArrToArr(m_bit() As Byte, e_bit() As Byte, Optional bUseKeyNo As Byte = 1, Optional bFlag As Byte = 1)
  129.      Dim iSL As Integer
  130.      Dim ina(7) As Byte
  131.      Dim lPos As Long
  132.      Dim outa(7) As Byte
  133.      Dim L As Long, M As Long
  134.      Dim i As Long
  135.      
  136.      If bUseKeyNo <> 2 Then bUseKeyNo = 1
  137.      
  138.      iSL = UBound(m_bit) + 1
  139.      If iSL Mod 8 <> 0 Then
  140.      iSL = ((iSL \ 8) + 1) * 8
  141.      End If
  142.      
  143.      ReDim e_bit(iSL - 1) As Byte
  144.      L = 0
  145.      Do While L <= UBound(m_bit)
  146.      M = L + 7
  147.      If M > UBound(m_bit) Then M = UBound(m_bit)
  148.      For i = 0 To 7
  149.      ina(i) = 0
  150.      Next
  151.      For i = L To M
  152.      ina(i - L) = m_bit(i)
  153.      Next
  154.      
  155.      
  156.      endes1 ina(), outa(), bUseKeyNo, bFlag
  157.      
  158.      
  159.      For i = 0 To 7
  160.      e_bit(i + L) = outa(i)
  161.      Next
  162.      L = L + 8
  163.      Loop
  164.     End Sub
  165.      
  166.     '
  167.     '***************************************************************
  168.     '
  169.     '==================== 下面是私有函数及过程 ===================
  170.     '
  171.     '***************************************************************
  172.      
  173.      
  174.     '
  175.     '* ArrXor 函数说明:
  176.     '* 将输入的两个数组中的字节元素分别作异或运算
  177.     '* 返回:
  178.     '* 无
  179.     '* 参数:'
  180.     '* const BYTE in1[] 输入字符串1
  181.     '* const BYTE in2[] 输入字符串2
  182.     '* BYTE out[] 输出的结果字符串
  183.     '*/
  184.     Private Sub ArrXor(in1() As Byte, in2() As Byte, outa() As Byte)
  185.      Dim i As Integer
  186.      For i = 0 To UBound(in1)
  187.      outa(i) = in1(i) Xor in2(i)
  188.      Next
  189.     End Sub
  190.      
  191.     '/*
  192.     ' * Bin2ASCII 函数说明:
  193.     ' * 将64字节的01字符串转换成对应的8个字节
  194.     ' * 返回:
  195.     ' * 转换后结果的指针
  196.     ' * 参数:
  197.     ' * const BYTE abyte(64) 输入字符串
  198.     ' * BYTE bit(8) 输出的转换结果
  199.     ' */
  200.     Private Sub Bin2ASCII(abyte() As Byte, bit() As Byte)
  201.      Dim i As Integer
  202.      For i = 0 To 7
  203.      bit(i) = abyte(i * 8) * 128 + abyte(i * 8 + 1) * 64 + _
  204.      abyte(i * 8 + 2) * 32 + abyte(i * 8 + 3) * 16 + _
  205.      abyte(i * 8 + 4) * 8 + abyte(i * 8 + 5) * 4 + _
  206.      abyte(i * 8 + 6) * 2 + abyte(i * 8 + 7)
  207.      Next
  208.     End Sub
  209.      
  210.     '/*
  211.     ' * ASCII2Bin 函数说明:
  212.     ' * 将8个字节输入转换成对应的64字节的01字符串
  213.     ' * 返回:
  214.     ' * 转换后结果的指针
  215.     ' * 参数:
  216.     ' * const BYTE bit[8] 输入字符串
  217.     ' * BYTE byte[64] 输出的转换结果
  218.     ' */
  219.     Private Sub ASCII2Bin(bit() As Byte, abyte() As Byte)
  220.      Dim i As Integer, j As Integer
  221.      For i = 0 To 7
  222.      For j = 0 To 7
  223.      abyte(i * 8 + j) = (bit(i) \ (2 ^ (7 - j))) And &H1
  224.      Next
  225.      Next
  226.     End Sub
  227.      
  228.     '/*
  229.     ' * GenSubKey 函数说明:
  230.     ' * 由输入的密钥得到16个子密钥
  231.     ' * 返回:
  232.     ' * 无
  233.     ' * 参数:
  234.     ' * const BYTE oldkey[8] 输入密钥
  235.     ' * BYTE newkey[16][8] 输出的子密钥
  236.     ' */
  237.     Private Sub GenSubKey(oldkey() As Byte, newkey() As Byte)
  238.      Dim i As Integer, k As Integer, rol As Integer
  239.      Dim s As String
  240.      
  241.      Dim oldkey_byte(63) As Byte ' BYTE oldkey_byte[64];
  242.      Dim oldkey_byte1(63) As Byte ' BYTE oldkey_byte1[64];
  243.      Dim oldkey_byte2(63) As Byte ' BYTE oldkey_byte2[64];
  244.      Dim oldkey_c(55) As Byte ' BYTE oldkey_c[56];
  245.      Dim oldkey_d(55) As Byte ' BYTE oldkey_d[56];
  246.      Dim newkey_byte(15, 63) As Byte ' BYTE newkey_byte[16][64];
  247.      Dim aT
  248.      Dim abyte(63) As Byte, bbyte(7) As Byte
  249.      
  250.      rol = 0
  251.      
  252.      
  253.      
  254.      ASCII2Bin oldkey(), oldkey_byte()
  255.      
  256.      '//位变换--根据换位表换位 压缩成56位密码
  257.      'for(i = 0; i < 56; i++)
  258.      ' oldkey_byte1[i] = oldkey_byte[pc_1[i] - 1];
  259.      
  260.      For i = 0 To 55
  261.      oldkey_byte1(i) = oldkey_byte(pc_1(i) - 1)
  262.      Next
  263.      
  264.      
  265.      
  266.      '//分为左右两部分,复制一遍以便于循环左移
  267.      'for(i = 0; i < 28; i++)
  268.      ' oldkey_c[i] = oldkey_byte1[i], oldkey_c[i + 28] = oldkey_byte1[i],
  269.      ' oldkey_d[i] = oldkey_byte1[i + 28], oldkey_d[i + 28] = oldkey_byte1[i + 28];
  270.      
  271.      'For i = 0 To 27
  272.      ' oldkey_c(i) = oldkey_byte1(i)
  273.      ' oldkey_c(i + 28) = oldkey_byte1(i)
  274.      ' oldkey_d(i) = oldkey_byte1(i + 28)
  275.      ' oldkey_d(i + 28) = oldkey_byte1(i + 28)
  276.      'Next
  277.      CopyMemory oldkey_c(0), oldkey_byte1(0), 28
  278.      CopyMemory oldkey_c(28), oldkey_byte1(0), 28
  279.      CopyMemory oldkey_d(0), oldkey_byte1(28), 28
  280.      CopyMemory oldkey_d(28), oldkey_byte1(28), 28
  281.      
  282.      '//分别生成16个子密钥
  283.      'for(i = 0; i < 16; i++)
  284.      '{
  285.      ' //循环左移
  286.      ' rol += ccmovebit[i];
  287.      ' //合并左移后的结果
  288.      ' for(k = 0; k < 28; k++)
  289.      ' oldkey_byte2[k] = oldkey_c[k + rol], oldkey_byte2[k + 28] = oldkey_d[k + rol];
  290.      ' //位变换
  291.      ' for(k = 0; k < 48; k++)
  292.      ' newkey_byte[i][k] = oldkey_byte2[pc_2[k] - 1];
  293.      '}
  294.      For i = 0 To 15
  295.      '循环左移
  296.      rol = rol + ccmovebit(i)
  297.      '合并左移后的结果
  298.      'For k = 0 To 27
  299.      ' oldkey_byte2(k) = oldkey_c(k + rol)
  300.      ' oldkey_byte2(k + 28) = oldkey_d(k + rol)
  301.      'Next
  302.      
  303.      CopyMemory oldkey_byte2(0), oldkey_c(rol), 28
  304.      CopyMemory oldkey_byte2(28), oldkey_d(rol), 28
  305.      
  306.      '位变换
  307.      For k = 0 To 47
  308.      newkey_byte(i, k) = oldkey_byte2(pc_2(k) - 1)
  309.      Next
  310.      Next
  311.      
  312.      '生成最终结果
  313.      'for(i = 0; i < 16; i++)
  314.      ' Bin2ASCII(newkey_byte[i], newkey[i]);
  315.      For i = 0 To 15
  316.      For k = 0 To 63
  317.      abyte(k) = newkey_byte(i, k)
  318.      Next
  319.      
  320.      
  321.      
  322.      Bin2ASCII abyte(), bbyte()
  323.      For k = 0 To 7
  324.      newkey(i, k) = bbyte(k)
  325.      Next
  326.      Next
  327.     End Sub
  328.      
  329.      
  330.     '/*
  331.     ' * endes1 函数说明:
  332.     ' * DES加密
  333.     ' * 返回:
  334.     ' * 无
  335.     ' * 参数:
  336.     ' * const BYTE m_bit[8] 输入的原文
  337.     ' * const BYTE k_bit[8] 输入的密钥
  338.     ' * BYTE e_bit[8] 输出的密文
  339.     ' bFlag=1 加 =2解
  340.     ' */
  341.     Private Sub endes1(m_bit() As Byte, e_bit() As Byte, Optional bUseKeyNo As Byte = 1, Optional bFlag As Byte = 1)
  342.      
  343.      Dim s As String
  344.      
  345.      Dim m_bit1(7) As Byte ' BYTE m_bit1[8] = {0};
  346.      Dim m_byte(63) As Byte ' BYTE m_byte[64] = {0};
  347.      Dim m_byte1(63) As Byte ' BYTE m_byte1[64] = {0};
  348.      'Dim key_n(15, 7) As Byte ' BYTE key_n[16][8] = {0};
  349.      Dim l_bit(16, 7) As Byte ' BYTE l_bit[17][8] = {0};
  350.      Dim r_bit(16, 7) As Byte ' BYTE r_bit[17][8] = {0};
  351.      Dim e_byte(63) As Byte ' BYTE e_byte[64] = {0};
  352.      Dim e_byte1(63) As Byte ' BYTE e_byte1[64] = {0};
  353.      Dim r_byte(63) As Byte ' BYTE r_byte[64] = {0};
  354.      Dim r_byte1(63) As Byte ' BYTE r_byte1[64] = {0};
  355.      Dim key_n
  356.      
  357.      Dim l_bit0(7) As Byte, r_bit0(7) As Byte
  358.      Dim l_bit1(7) As Byte, r_bit1(7) As Byte
  359.      
  360.      Dim abyte8(7) As Byte
  361.      
  362.      
  363.      Dim i As Integer, j As Integer, k As Integer
  364.      
  365.      If bUseKeyNo <> 2 Then bUseKeyNo = 1
  366.      
  367.      If bUseKeyNo = 1 Then key_n = key_n1 Else key_n = key_n2
  368.      
  369.      
  370.      '//根据密钥生成16个子密钥
  371.      'GenSubKey k_bit(), key_n()
  372.      
  373.      '//将待加密字串变换成01串
  374.      ASCII2Bin m_bit(), m_byte()
  375.      
  376.      '//按照ip表对待加密字串进行位变换
  377.      'for(i = 0; i < 64; i++)
  378.      ' m_byte1[i] = m_byte[ip[i] - 1];
  379.      For i = 0 To 63
  380.      m_byte1(i) = m_byte(ip(i) - 1)
  381.      Next
  382.      
  383.      '位变换后的待加密字串
  384.      Bin2ASCII m_byte1(), m_bit1()
  385.      
  386.      '//将位变换后的待加密字串分成两组,分别为前4字节L和后4字节R,作为迭代的基础(第0次迭代)
  387.      'for(i = 0; i < 4; i++)
  388.      ' l_bit[0][i] = m_bit1[i], r_bit[0][i] = m_bit1[i + 4];
  389.      For i = 0 To 3
  390.      'l_bit(0, i) = m_bit1(i)
  391.      'r_bit(0, i) = m_bit1(i + 4)
  392.      CopyMemory l_bit0(0), m_bit1(0), 4
  393.      CopyMemory r_bit0(0), m_bit1(4), 4
  394.      Next
  395.      
  396.      '//16次迭代运算
  397.      'for(i = 1; i <= 16; i++)
  398.      
  399.      For i = 1 To 16
  400.      '//R的上一次的迭代结果作为L的当前次迭代结果
  401.      'for(j = 0; j < 4; j++)
  402.      ' l_bit[i][j] = r_bit[i-1][j];
  403.      'For j = 0 To 3
  404.      ' l_bit(i, j) = r_bit(i - 1, j)
  405.      'Next
  406.      CopyMemory l_bit1(0), r_bit0(0), 4
  407.      
  408.      'ASCII2Bin(r_bit[i-1], r_byte);
  409.      'For j = 0 To 7
  410.      ' abyte8(j) = r_bit(i - 1, j)
  411.      'Next
  412.      CopyMemory abyte8(0), r_bit0(0), 8
  413.      
  414.      ASCII2Bin abyte8(), r_byte()
  415.      
  416.      '//将R的上一次迭代结果按E表进行位扩展得到48位中间结果
  417.      'for(j = 0; j < 48; j++)
  418.      ' r_byte1[j] = r_byte[e[j] - 1];
  419.      'Bin2ASCII(r_byte1, r_bit[i-1]);
  420.      For j = 0 To 47
  421.      r_byte1(j) = r_byte(e(j) - 1)
  422.      Next
  423.      Bin2ASCII r_byte1(), abyte8()
  424.      'For j = 0 To 7
  425.      ' r_bit(i - 1, j) = abyte8(j)
  426.      'Next
  427.      CopyMemory r_bit0(0), abyte8(0), 8
  428.      
  429.      '//与第I-1个子密钥进行异或运算
  430.      'for(j = 0; j < 6; j++)
  431.      ' r_bit[i-1][j] = r_bit[i-1][j] ^ key_n[i-1][j];
  432.      For j = 0 To 5
  433.      If bFlag = 1 Then
  434.      '加
  435.      'r_bit(i - 1, j) = r_bit(i - 1, j) Xor key_n(i - 1, j)
  436.      r_bit0(j) = r_bit0(j) Xor key_n(i - 1, j)
  437.      Else
  438.      '解
  439.      'r_bit(i - 1, j) = r_bit(i - 1, j) Xor key_n((17 - i) - 1, j)
  440.      r_bit0(j) = r_bit0(j) Xor key_n((17 - i) - 1, j)
  441.      End If
  442.      Next
  443.      
  444.      '//进行S选择,得到32位中间结果
  445.      'SReplace(r_bit[i - 1]);
  446.      'For j = 0 To 7
  447.      ' abyte8(j) = r_bit(i - 1, j)
  448.      'Next
  449.      CopyMemory abyte8(0), r_bit0(0), 8
  450.      
  451.      SReplace abyte8()
  452.      'For j = 0 To 7
  453.      ' r_bit(i - 1, j) = abyte8(j)
  454.      'Next
  455.      CopyMemory r_bit0(0), abyte8(0), 8
  456.      
  457.      '//结果与L的上次迭代结果异或得到R的此次迭代结果
  458.      'for(j = 0; j < 4; j++)
  459.      '{
  460.      ' r_bit[i][j] = l_bit[i-1][j] ^ r_bit[i-1][j];
  461.      '}
  462.      For j = 0 To 3
  463.      'r_bit(i, j) = l_bit(i - 1, j) Xor r_bit(i - 1, j)
  464.      r_bit1(j) = l_bit0(j) Xor r_bit0(j)
  465.      Next
  466.      
  467.      CopyMemory l_bit0(0), l_bit1(0), 8
  468.      CopyMemory r_bit0(0), r_bit1(0), 8
  469.      Next
  470.      
  471.      
  472.      '//组合最终迭代结果
  473.      'for(i = 0; i < 4; i++)
  474.      ' e_bit[i] = r_bit[16][i], e_bit[i + 4] = l_bit[16][i];
  475.      'For i = 0 To 3
  476.      ' e_bit(i) = r_bit(16, i)
  477.      ' e_bit(i + 4) = l_bit(16, i)
  478.      'Next
  479.      CopyMemory e_bit(0), r_bit1(0), 4
  480.      CopyMemory e_bit(4), l_bit1(0), 4
  481.      
  482.      ASCII2Bin e_bit(), e_byte()
  483.      
  484.      '//按照表IP-1进行位变换
  485.      'for(i = 0; i < 64; i++)
  486.      ' e_byte1[i] = e_byte[ip_1[i] - 1];
  487.      For i = 0 To 63
  488.      e_byte1(i) = e_byte(ip_1(i) - 1)
  489.      Next
  490.      
  491.      '//得到最后的加密结果
  492.      Bin2ASCII e_byte1(), e_bit()
  493.     End Sub
  494.      
  495.      
  496.      
  497.      
  498.     '/*
  499.     ' * SReplace 函数说明:
  500.     ' * S选择
  501.     ' * 返回:
  502.     ' * 无
  503.     ' * 参数:
  504.     ' * BYTE s_bit[8] 输入暨选择后的输出
  505.     ' */
  506.     Private Sub SReplace(s_bit() As Byte)
  507.      
  508.      Dim i As Integer
  509.      
  510.      
  511.      Dim s_byte(63) As Byte ' BYTE s_byte[64] = {0};
  512.      Dim s_byte1(63) As Byte ' BYTE s_byte1[64] = {0};
  513.      Dim row As Byte, col As Byte
  514.      Dim s_out_bit(7) As Byte 'BYTE s_out_bit[8] = {0};
  515.      
  516.      
  517.      
  518.      
  519.      row = 0: col = 0
  520.      
  521.      '//转成二进制字符串处理
  522.      ASCII2Bin s_bit(), s_byte()
  523.      'for(int i = 0; i < 8; i++)
  524.      '{
  525.      ' //0、5位为row,1、2、3、4位为col,在S表中选择一个八位的数
  526.      ' row = s_byte[i * 6] * 2 + s_byte[i * 6 + 5];
  527.      ' col = s_byte[i * 6 + 1] * 8 + s_byte[i * 6 + 2] * 4 + s_byte[i * 6 + 3] * 2 + s_byte[i * 6 + 4];
  528.      ' s_out_bit[i] = s[i][row][col];
  529.      '}
  530.      For i = 0 To 7
  531.      '0、5位为row,1、2、3、4位为col,在S表中选择一个八位的数
  532.      row = s_byte(i * 6) * 2 + s_byte(i * 6 + 5)
  533.      col = s_byte(i * 6 + 1) * 8 + s_byte(i * 6 + 2) * 4 + s_byte(i * 6 + 3) * 2 + s_byte(i * 6 + 4)
  534.      s_out_bit(i) = ss(i, row, col)
  535.      Next
  536.      
  537.      '//将八个选择的八位数据压缩表示
  538.      's_out_bit[0] = (s_out_bit[0] << 4) + s_out_bit[1];
  539.      's_out_bit[1] = (s_out_bit[2] << 4) + s_out_bit[3];
  540.      's_out_bit[2] = (s_out_bit[4] << 4) + s_out_bit[5];
  541.      's_out_bit[3] = (s_out_bit[6] << 4) + s_out_bit[7];
  542.      s_out_bit(0) = s_out_bit(0) * (2 ^ 4) + s_out_bit(1)
  543.      s_out_bit(1) = s_out_bit(2) * (2 ^ 4) + s_out_bit(3)
  544.      s_out_bit(2) = s_out_bit(4) * (2 ^ 4) + s_out_bit(5)
  545.      s_out_bit(3) = s_out_bit(6) * (2 ^ 4) + s_out_bit(7)
  546.      
  547.      '//转成二进制字符串处理
  548.      ASCII2Bin s_out_bit(), s_byte()
  549.      
  550.      '//换位
  551.      'for(i = 0; i < 32; i++)
  552.      ' s_byte1[i] = s_byte[p[i] - 1];
  553.      For i = 0 To 31
  554.      s_byte1(i) = s_byte(p(i) - 1)
  555.      Next
  556.      
  557.      '//生成最后结果
  558.      Bin2ASCII s_byte1(), s_bit()
  559.      
  560.     End Sub
  561.      
  562.      
  563.      
  564.      
  565.      
  566.     Private Sub Class_Initialize()
  567.      Dim i As Integer, j As Integer, k As Integer, L As Integer
  568.      Dim aT, s As String
  569.      '//换位表IP
  570.      s = "58,50,42,34,26,18,10,2," & _
  571.      "60,52,44,36,28,20,12,4," & _
  572.      "62,54,46,38,30,22,14,6," & _
  573.      "64,56,48,40,32,24,16,8," & _
  574.      "57,49,41,33,25,17,9,1," & _
  575.      "59,51,43,35,27,19,11,3," & _
  576.      "61,53,45,37,29,21,13,5," & _
  577.      "63,55,47,39,31,23,15,7"
  578.      aT = Split(s, ",")
  579.      For i = 0 To 63: ip(i) = Val(aT(i)): Next
  580.      
  581.      '//换位表IP_1
  582.      s = "40,8,48,16,56,24,64,32," & _
  583.      "39,7,47,15,55,23,63,31," & _
  584.      "38,6,46,14,54,22,62,30," & _
  585.      "37,5,45,13,53,21,61,29," & _
  586.      "36,4,44,12,52,20,60,28," & _
  587.      "35,3,43,11,51,19,59,27," & _
  588.      "34,2,42,10,50,18,58,26," & _
  589.      "33,1,41,9,49,17,57,25"
  590.      aT = Split(s, ",")
  591.      For i = 0 To 63: ip_1(i) = Val(aT(i)): Next
  592.      
  593.      '//放大换位表
  594.      s = "32,1, 2, 3, 4, 5," & _
  595.      "4, 5, 6, 7, 8, 9," & _
  596.      "8, 9, 10,11,12,13," & _
  597.      "12,13,14,15,16,17," & _
  598.      "16,17,18,19,20,21," & _
  599.      "20,21,22,23,24,25," & _
  600.      "24,25,26,27,28,29," & _
  601.      "28,29,30,31,32,1"
  602.      aT = Split(s, ",")
  603.      For i = 0 To 47: e(i) = Val(aT(i)): Next
  604.      
  605.      
  606.      '缩小换位表1
  607.      s = "57,49,41,33,25,17,9," & _
  608.      "1,58,50,42,34,26,18," & _
  609.      "10,2,59,51,43,35,27," & _
  610.      "19,11,3,60,52,44,36," & _
  611.      "63,55,47,39,31,23,15," & _
  612.      "7,62,54,46,38,30,22," & _
  613.      "14,6,61,53,45,37,29," & _
  614.      "21,13,5,28,20,12,4"
  615.      aT = Split(s, ",")
  616.      For i = 0 To 55: pc_1(i) = Val(aT(i)): Next
  617.      
  618.      
  619.      '//缩小换位表2
  620.      s = "14,17,11,24,1,5," & _
  621.      "3,28,15,6,21,10," & _
  622.      "23,19,12,4,26,8," & _
  623.      "16,7,27,20,13,2," & _
  624.      "41,52,31,37,47,55," & _
  625.      "30,40,51,45,33,48," & _
  626.      "44,49,39,56,34,53," & _
  627.      "46,42,50,36,29,32"
  628.      aT = Split(s, ",")
  629.      For i = 0 To 47: pc_2(i) = Val(aT(i)): Next
  630.      
  631.      '//16次循环左移对应的左移位数
  632.      s = "1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1"
  633.      aT = Split(s, ",")
  634.      For i = 0 To 15: ccmovebit(i) = Val(aT(i)): Next
  635.      
  636.      
  637.      s = "16,7,20,21," & _
  638.      "29,12,28,17," & _
  639.      "1,15,23,26," & _
  640.      "5,18,31,10," & _
  641.      "2,8,24,14," & _
  642.      "32,27,3,9," & _
  643.      "19,13,30,6," & _
  644.      "22,11,4,25"
  645.      aT = Split(s, ",")
  646.      For i = 0 To 31
  647.      p(i) = Val(aT(i))
  648.      Next
  649.      
  650.      s = "14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7," & _
  651.      "0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8," & _
  652.      "4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0," & _
  653.      "15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13," & _
  654.      "15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10," & _
  655.      "3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5," & _
  656.      "0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15," & _
  657.      "13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9," & _
  658.      "10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8," & _
  659.      "13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1," & _
  660.      "13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7," & _
  661.      "1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12," & _
  662.      "7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15," & _
  663.      "13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9," & _
  664.      "10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4," & _
  665.      "3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14,"
  666.      
  667.      s = s & _
  668.      "2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9," & _
  669.      "14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6," & _
  670.      "4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14," & _
  671.      "11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3," & _
  672.      "12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11," & _
  673.      "10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8," & _
  674.      "9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6," & _
  675.      "4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13," & _
  676.      "4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1," & _
  677.      "13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6," & _
  678.      "1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2," & _
  679.      "6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12," & _
  680.      "13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7," & _
  681.      "1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2," & _
  682.      "7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8," & _
  683.      "2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11"
  684.      aT = Split(s, ",")
  685.      L = 0
  686.      For i = 0 To 7
  687.      For j = 0 To 3
  688.      For k = 0 To 15
  689.      ss(i, j, k) = Val(aT(L))
  690.      L = L + 1
  691.      Next
  692.      Next
  693.      Next
  694.      
  695.     End Sub
复制代码
天行健,君子以自强不息
地势坤,君子以厚德载物
黑色海岸线欢迎您

QQ群:7212260
致力于探索WEB技术精髓:http://www.bitechcn.com
点这里加我!

返回列表 回复 发帖