Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 29.45 KB | None | 0 0
  1. '*******************************************************************************
  2. ' MODULE:       CRijndael
  3. ' FILENAME:     CRijndael.cls
  4. ' AUTHOR:       Phil Fresle
  5. ' CREATED:      16-Feb-2001
  6. ' COPYRIGHT:    Copyright 2001 Phil Fresle
  7. ' EMAIL:        phil@frez.co.uk
  8. ' WEB:          http://www.frez.co.uk
  9. '
  10. ' DESCRIPTION:
  11. ' Implementation of the AES Rijndael Block Cipher. Inspired by Mike Scott's
  12. ' implementation in C. Permission for free direct or derivative use is granted
  13. ' subject to compliance with any conditions that the originators of the
  14. ' algorithm place on its exploitation.
  15. '
  16. ' MODIFICATION HISTORY:
  17. ' 16-Feb-2001   Phil Fresle     Initial Version
  18. ' 03-Apr-2001   Phil Fresle     Added EncryptData and DecryptData functions to
  19. '                               make it easier to use by VB developers for
  20. '                               encrypting and decrypting strings. These procs
  21. '                               take large byte arrays, the resultant encoded
  22. '                               data includes the message length inserted on
  23. '                               the front four bytes prior to encryption.
  24. ' 19-Apr-2001   Phil Fresle     Thanks to Paolo Migliaccio for finding a bug
  25. '                               with 256 bit key. Problem was in the gkey
  26. '                               function. Now properly matches NIST values.
  27. '*******************************************************************************
  28. Option Explicit
  29.  
  30. Private m_lOnBits(30)   As Long
  31. Private m_l2Power(30)   As Long
  32. Private m_bytOnBits(7)  As Byte
  33. Private m_byt2Power(7)  As Byte
  34.  
  35. Private m_InCo(3) As Byte
  36.  
  37. Private m_fbsub(255)    As Byte
  38. Private m_rbsub(255)    As Byte
  39. Private m_ptab(255)     As Byte
  40. Private m_ltab(255)     As Byte
  41. Private m_ftable(255)   As Long
  42. Private m_rtable(255)   As Long
  43. Private m_rco(29)       As Long
  44.  
  45. Private m_Nk        As Long
  46. Private m_Nb        As Long
  47. Private m_Nr        As Long
  48. Private m_fi(23)    As Byte
  49. Private m_ri(23)    As Byte
  50. Private m_fkey(119) As Long
  51. Private m_rkey(119) As Long
  52.  
  53. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
  54.     (ByVal Destination As Any, ByVal Source As Any, ByVal Length As Long)
  55.  
  56. '*******************************************************************************
  57. ' Class_Initialize (SUB)
  58. '*******************************************************************************
  59. Private Sub Class_Initialize()
  60.     m_InCo(0) = &HB
  61.     m_InCo(1) = &HD
  62.     m_InCo(2) = &H9
  63.     m_InCo(3) = &HE
  64.    
  65.     ' Could have done this with a loop calculating each value, but simply
  66.    ' assigning the values is quicker - BITS SET FROM RIGHT
  67.    m_bytOnBits(0) = 1          ' 00000001
  68.    m_bytOnBits(1) = 3          ' 00000011
  69.    m_bytOnBits(2) = 7          ' 00000111
  70.    m_bytOnBits(3) = 15         ' 00001111
  71.    m_bytOnBits(4) = 31         ' 00011111
  72.    m_bytOnBits(5) = 63         ' 00111111
  73.    m_bytOnBits(6) = 127        ' 01111111
  74.    m_bytOnBits(7) = 255        ' 11111111
  75.    
  76.     ' Could have done this with a loop calculating each value, but simply
  77.    ' assigning the values is quicker - POWERS OF 2
  78.    m_byt2Power(0) = 1          ' 00000001
  79.    m_byt2Power(1) = 2          ' 00000010
  80.    m_byt2Power(2) = 4          ' 00000100
  81.    m_byt2Power(3) = 8          ' 00001000
  82.    m_byt2Power(4) = 16         ' 00010000
  83.    m_byt2Power(5) = 32         ' 00100000
  84.    m_byt2Power(6) = 64         ' 01000000
  85.    m_byt2Power(7) = 128        ' 10000000
  86.    
  87.     ' Could have done this with a loop calculating each value, but simply
  88.    ' assigning the values is quicker - BITS SET FROM RIGHT
  89.    m_lOnBits(0) = 1            ' 00000000000000000000000000000001
  90.    m_lOnBits(1) = 3            ' 00000000000000000000000000000011
  91.    m_lOnBits(2) = 7            ' 00000000000000000000000000000111
  92.    m_lOnBits(3) = 15           ' 00000000000000000000000000001111
  93.    m_lOnBits(4) = 31           ' 00000000000000000000000000011111
  94.    m_lOnBits(5) = 63           ' 00000000000000000000000000111111
  95.    m_lOnBits(6) = 127          ' 00000000000000000000000001111111
  96.    m_lOnBits(7) = 255          ' 00000000000000000000000011111111
  97.    m_lOnBits(8) = 511          ' 00000000000000000000000111111111
  98.    m_lOnBits(9) = 1023         ' 00000000000000000000001111111111
  99.    m_lOnBits(10) = 2047        ' 00000000000000000000011111111111
  100.    m_lOnBits(11) = 4095        ' 00000000000000000000111111111111
  101.    m_lOnBits(12) = 8191        ' 00000000000000000001111111111111
  102.    m_lOnBits(13) = 16383       ' 00000000000000000011111111111111
  103.    m_lOnBits(14) = 32767       ' 00000000000000000111111111111111
  104.    m_lOnBits(15) = 65535       ' 00000000000000001111111111111111
  105.    m_lOnBits(16) = 131071      ' 00000000000000011111111111111111
  106.    m_lOnBits(17) = 262143      ' 00000000000000111111111111111111
  107.    m_lOnBits(18) = 524287      ' 00000000000001111111111111111111
  108.    m_lOnBits(19) = 1048575     ' 00000000000011111111111111111111
  109.    m_lOnBits(20) = 2097151     ' 00000000000111111111111111111111
  110.    m_lOnBits(21) = 4194303     ' 00000000001111111111111111111111
  111.    m_lOnBits(22) = 8388607     ' 00000000011111111111111111111111
  112.    m_lOnBits(23) = 16777215    ' 00000000111111111111111111111111
  113.    m_lOnBits(24) = 33554431    ' 00000001111111111111111111111111
  114.    m_lOnBits(25) = 67108863    ' 00000011111111111111111111111111
  115.    m_lOnBits(26) = 134217727   ' 00000111111111111111111111111111
  116.    m_lOnBits(27) = 268435455   ' 00001111111111111111111111111111
  117.    m_lOnBits(28) = 536870911   ' 00011111111111111111111111111111
  118.    m_lOnBits(29) = 1073741823  ' 00111111111111111111111111111111
  119.    m_lOnBits(30) = 2147483647  ' 01111111111111111111111111111111
  120.    
  121.     ' Could have done this with a loop calculating each value, but simply
  122.    ' assigning the values is quicker - POWERS OF 2
  123.    m_l2Power(0) = 1            ' 00000000000000000000000000000001
  124.    m_l2Power(1) = 2            ' 00000000000000000000000000000010
  125.    m_l2Power(2) = 4            ' 00000000000000000000000000000100
  126.    m_l2Power(3) = 8            ' 00000000000000000000000000001000
  127.    m_l2Power(4) = 16           ' 00000000000000000000000000010000
  128.    m_l2Power(5) = 32           ' 00000000000000000000000000100000
  129.    m_l2Power(6) = 64           ' 00000000000000000000000001000000
  130.    m_l2Power(7) = 128          ' 00000000000000000000000010000000
  131.    m_l2Power(8) = 256          ' 00000000000000000000000100000000
  132.    m_l2Power(9) = 512          ' 00000000000000000000001000000000
  133.    m_l2Power(10) = 1024        ' 00000000000000000000010000000000
  134.    m_l2Power(11) = 2048        ' 00000000000000000000100000000000
  135.    m_l2Power(12) = 4096        ' 00000000000000000001000000000000
  136.    m_l2Power(13) = 8192        ' 00000000000000000010000000000000
  137.    m_l2Power(14) = 16384       ' 00000000000000000100000000000000
  138.    m_l2Power(15) = 32768       ' 00000000000000001000000000000000
  139.    m_l2Power(16) = 65536       ' 00000000000000010000000000000000
  140.    m_l2Power(17) = 131072      ' 00000000000000100000000000000000
  141.    m_l2Power(18) = 262144      ' 00000000000001000000000000000000
  142.    m_l2Power(19) = 524288      ' 00000000000010000000000000000000
  143.    m_l2Power(20) = 1048576     ' 00000000000100000000000000000000
  144.    m_l2Power(21) = 2097152     ' 00000000001000000000000000000000
  145.    m_l2Power(22) = 4194304     ' 00000000010000000000000000000000
  146.    m_l2Power(23) = 8388608     ' 00000000100000000000000000000000
  147.    m_l2Power(24) = 16777216    ' 00000001000000000000000000000000
  148.    m_l2Power(25) = 33554432    ' 00000010000000000000000000000000
  149.    m_l2Power(26) = 67108864    ' 00000100000000000000000000000000
  150.    m_l2Power(27) = 134217728   ' 00001000000000000000000000000000
  151.    m_l2Power(28) = 268435456   ' 00010000000000000000000000000000
  152.    m_l2Power(29) = 536870912   ' 00100000000000000000000000000000
  153.    m_l2Power(30) = 1073741824  ' 01000000000000000000000000000000
  154. End Sub
  155.  
  156. '*******************************************************************************
  157. ' LShift (FUNCTION)
  158. '*******************************************************************************
  159. Private Function LShift(ByVal lValue As Long, _
  160.                         ByVal iShiftBits As Integer) As Long
  161.     If iShiftBits = 0 Then
  162.         LShift = lValue
  163.         Exit Function
  164.     ElseIf iShiftBits = 31 Then
  165.         If lValue And 1 Then
  166.             LShift = &H80000000
  167.         Else
  168.             LShift = 0
  169.         End If
  170.         Exit Function
  171.     ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  172.         Err.Raise 6
  173.     End If
  174.    
  175.     If (lValue And m_l2Power(31 - iShiftBits)) Then
  176.         LShift = ((lValue And m_lOnBits(31 - (iShiftBits + 1))) * _
  177.             m_l2Power(iShiftBits)) Or &H80000000
  178.     Else
  179.         LShift = ((lValue And m_lOnBits(31 - iShiftBits)) * _
  180.             m_l2Power(iShiftBits))
  181.     End If
  182. End Function
  183.  
  184. '*******************************************************************************
  185. ' RShift (FUNCTION)
  186. '*******************************************************************************
  187. Private Function RShift(ByVal lValue As Long, _
  188.                         ByVal iShiftBits As Integer) As Long
  189.     If iShiftBits = 0 Then
  190.         RShift = lValue
  191.         Exit Function
  192.     ElseIf iShiftBits = 31 Then
  193.         If lValue And &H80000000 Then
  194.             RShift = 1
  195.         Else
  196.             RShift = 0
  197.         End If
  198.         Exit Function
  199.     ElseIf iShiftBits < 0 Or iShiftBits > 31 Then
  200.         Err.Raise 6
  201.     End If
  202.    
  203.     RShift = (lValue And &H7FFFFFFE) \ m_l2Power(iShiftBits)
  204.    
  205.     If (lValue And &H80000000) Then
  206.         RShift = (RShift Or (&H40000000 \ m_l2Power(iShiftBits - 1)))
  207.     End If
  208. End Function
  209.  
  210. '*******************************************************************************
  211. ' LShiftByte (FUNCTION)
  212. '*******************************************************************************
  213. Private Function LShiftByte(ByVal bytValue As Byte, _
  214.                             ByVal bytShiftBits As Byte) As Byte
  215.     If bytShiftBits = 0 Then
  216.         LShiftByte = bytValue
  217.         Exit Function
  218.     ElseIf bytShiftBits = 7 Then
  219.         If bytValue And 1 Then
  220.             LShiftByte = &H80
  221.         Else
  222.             LShiftByte = 0
  223.         End If
  224.         Exit Function
  225.     ElseIf bytShiftBits < 0 Or bytShiftBits > 7 Then
  226.         Err.Raise 6
  227.     End If
  228.    
  229.     LShiftByte = ((bytValue And m_bytOnBits(7 - bytShiftBits)) * _
  230.         m_byt2Power(bytShiftBits))
  231. End Function
  232.  
  233. '*******************************************************************************
  234. ' RShiftByte (FUNCTION)
  235. '*******************************************************************************
  236. Private Function RShiftByte(ByVal bytValue As Byte, _
  237.                             ByVal bytShiftBits As Byte) As Byte
  238.     If bytShiftBits = 0 Then
  239.         RShiftByte = bytValue
  240.         Exit Function
  241.     ElseIf bytShiftBits = 7 Then
  242.         If bytValue And &H80 Then
  243.             RShiftByte = 1
  244.         Else
  245.             RShiftByte = 0
  246.         End If
  247.         Exit Function
  248.     ElseIf bytShiftBits < 0 Or bytShiftBits > 7 Then
  249.         Err.Raise 6
  250.     End If
  251.    
  252.     RShiftByte = bytValue \ m_byt2Power(bytShiftBits)
  253. End Function
  254.  
  255. '*******************************************************************************
  256. ' RotateLeft (FUNCTION)
  257. '*******************************************************************************
  258. Private Function RotateLeft(ByVal lValue As Long, _
  259.                             ByVal iShiftBits As Integer) As Long
  260.     RotateLeft = LShift(lValue, iShiftBits) Or RShift(lValue, (32 - iShiftBits))
  261. End Function
  262.  
  263. ''*******************************************************************************
  264. '' RotateLeftByte (FUNCTION)
  265. '*******************************************************************************
  266. Private Function RotateLeftByte(ByVal bytValue As Byte, _
  267.                                 ByVal bytShiftBits As Byte) As Byte
  268.     RotateLeftByte = LShiftByte(bytValue, bytShiftBits) Or _
  269.         RShiftByte(bytValue, (8 - bytShiftBits))
  270. End Function
  271.  
  272. '*******************************************************************************
  273. ' Pack (FUNCTION)
  274. '*******************************************************************************
  275. Private Function Pack(b() As Byte) As Long
  276.     Dim lCount As Long
  277.     Dim lTemp  As Long
  278.    
  279.     For lCount = 0 To 3
  280.         lTemp = b(lCount)
  281.         Pack = Pack Or LShift(lTemp, (lCount * 8))
  282.     Next
  283. End Function
  284.  
  285. '*******************************************************************************
  286. ' PackFrom (FUNCTION)
  287. '*******************************************************************************
  288. Private Function PackFrom(b() As Byte, _
  289.                           ByVal k As Long) As Long
  290.     Dim lCount As Long
  291.     Dim lTemp  As Long
  292.    
  293.     For lCount = 0 To 3
  294.         lTemp = b(lCount + k)
  295.         PackFrom = PackFrom Or LShift(lTemp, (lCount * 8))
  296.     Next
  297. End Function
  298.  
  299. '*******************************************************************************
  300. ' Unpack (SUB)
  301. '*******************************************************************************
  302. Private Sub Unpack(ByVal a As Long, _
  303.                    b() As Byte)
  304.     b(0) = a And m_lOnBits(7)
  305.     b(1) = RShift(a, 8) And m_lOnBits(7)
  306.     b(2) = RShift(a, 16) And m_lOnBits(7)
  307.     b(3) = RShift(a, 24) And m_lOnBits(7)
  308. End Sub
  309.  
  310. '*******************************************************************************
  311. ' UnpackFrom (SUB)
  312. '*******************************************************************************
  313. Private Sub UnpackFrom(ByVal a As Long, _
  314.                        b() As Byte, _
  315.                        ByVal k As Long)
  316.     b(0 + k) = a And m_lOnBits(7)
  317.     b(1 + k) = RShift(a, 8) And m_lOnBits(7)
  318.     b(2 + k) = RShift(a, 16) And m_lOnBits(7)
  319.     b(3 + k) = RShift(a, 24) And m_lOnBits(7)
  320. End Sub
  321.  
  322. '*******************************************************************************
  323. ' xtime (FUNCTION)
  324. '*******************************************************************************
  325. Private Function xtime(ByVal a As Byte) As Byte
  326.     Dim b As Byte
  327.    
  328.     If (a And &H80) Then
  329.         b = &H1B
  330.     Else
  331.         b = 0
  332.     End If
  333.    
  334.     a = LShiftByte(a, 1)
  335.     a = a Xor b
  336.    
  337.     xtime = a
  338. End Function
  339.  
  340. '*******************************************************************************
  341. ' bmul (FUNCTION)
  342. '*******************************************************************************
  343. Private Function bmul(ByVal x As Byte, _
  344.                       y As Byte) As Byte
  345.     If x <> 0 And y <> 0 Then
  346.         bmul = m_ptab((CLng(m_ltab(x)) + CLng(m_ltab(y))) Mod 255)
  347.     Else
  348.         bmul = 0
  349.     End If
  350. End Function
  351.  
  352. '*******************************************************************************
  353. ' SubByte (FUNCTION)
  354. '*******************************************************************************
  355. Private Function SubByte(ByVal a As Long) As Long
  356.     Dim b(3) As Byte
  357.    
  358.     Unpack a, b
  359.     b(0) = m_fbsub(b(0))
  360.     b(1) = m_fbsub(b(1))
  361.     b(2) = m_fbsub(b(2))
  362.     b(3) = m_fbsub(b(3))
  363.    
  364.     SubByte = Pack(b)
  365. End Function
  366.  
  367. '*******************************************************************************
  368. ' product (FUNCTION)
  369. '*******************************************************************************
  370. Private Function product(ByVal x As Long, _
  371.                          ByVal y As Long) As Long
  372.     Dim xb(3) As Byte
  373.     Dim yb(3) As Byte
  374.    
  375.     Unpack x, xb
  376.     Unpack y, yb
  377.     product = bmul(xb(0), yb(0)) Xor bmul(xb(1), yb(1)) Xor bmul(xb(2), yb(2)) _
  378.         Xor bmul(xb(3), yb(3))
  379. End Function
  380.  
  381. '*******************************************************************************
  382. ' InvMixCol (FUNCTION)
  383. '*******************************************************************************
  384. Private Function InvMixCol(ByVal x As Long) As Long
  385.     Dim y       As Long
  386.     Dim m       As Long
  387.     Dim b(3)    As Byte
  388.    
  389.     m = Pack(m_InCo)
  390.     b(3) = product(m, x)
  391.     m = RotateLeft(m, 24)
  392.     b(2) = product(m, x)
  393.     m = RotateLeft(m, 24)
  394.     b(1) = product(m, x)
  395.     m = RotateLeft(m, 24)
  396.     b(0) = product(m, x)
  397.     y = Pack(b)
  398.    
  399.     InvMixCol = y
  400. End Function
  401.  
  402. '*******************************************************************************
  403. ' ByteSub (FUNCTION)
  404. '*******************************************************************************
  405. Private Function ByteSub(ByVal x As Byte) As Byte
  406.     Dim y As Byte
  407.    
  408.     y = m_ptab(255 - m_ltab(x))
  409.     x = y
  410.     x = RotateLeftByte(x, 1)
  411.     y = y Xor x
  412.     x = RotateLeftByte(x, 1)
  413.     y = y Xor x
  414.     x = RotateLeftByte(x, 1)
  415.     y = y Xor x
  416.     x = RotateLeftByte(x, 1)
  417.     y = y Xor x
  418.     y = y Xor &H63
  419.    
  420.     ByteSub = y
  421. End Function
  422.  
  423. '*******************************************************************************
  424. ' gentables (SUB)
  425. '*******************************************************************************
  426. Public Sub gentables()
  427.     Dim i       As Long
  428.     Dim y       As Byte
  429.     Dim b(3)    As Byte
  430.     Dim ib      As Byte
  431.    
  432.     m_ltab(0) = 0
  433.     m_ptab(0) = 1
  434.     m_ltab(1) = 0
  435.     m_ptab(1) = 3
  436.     m_ltab(3) = 1
  437.    
  438.     For i = 2 To 255
  439.         m_ptab(i) = m_ptab(i - 1) Xor xtime(m_ptab(i - 1))
  440.         m_ltab(m_ptab(i)) = i
  441.     Next
  442.    
  443.     m_fbsub(0) = &H63
  444.     m_rbsub(&H63) = 0
  445.    
  446.     For i = 1 To 255
  447.         ib = i
  448.         y = ByteSub(ib)
  449.         m_fbsub(i) = y
  450.         m_rbsub(y) = i
  451.     Next
  452.    
  453.         y = 1
  454.     For i = 0 To 29
  455.         m_rco(i) = y
  456.         y = xtime(y)
  457.     Next
  458.    
  459.     For i = 0 To 255
  460.         y = m_fbsub(i)
  461.         b(3) = y Xor xtime(y)
  462.         b(2) = y
  463.         b(1) = y
  464.         b(0) = xtime(y)
  465.         m_ftable(i) = Pack(b)
  466.        
  467.         y = m_rbsub(i)
  468.         b(3) = bmul(m_InCo(0), y)
  469.         b(2) = bmul(m_InCo(1), y)
  470.         b(1) = bmul(m_InCo(2), y)
  471.         b(0) = bmul(m_InCo(3), y)
  472.         m_rtable(i) = Pack(b)
  473.     Next
  474. End Sub
  475.  
  476. '*******************************************************************************
  477. ' gkey (SUB)
  478. '*******************************************************************************
  479. Public Sub gkey(ByVal nb As Long, _
  480.                 ByVal nk As Long, _
  481.                 KEY() As Byte)
  482.                
  483.     Dim i               As Long
  484.     Dim j               As Long
  485.     Dim k               As Long
  486.     Dim m               As Long
  487.     Dim N               As Long
  488.     Dim C1              As Long
  489.     Dim C2              As Long
  490.     Dim C3              As Long
  491.     Dim CipherKey(7)    As Long
  492.    
  493.     m_Nb = nb
  494.     m_Nk = nk
  495.    
  496.     If m_Nb >= m_Nk Then
  497.         m_Nr = 6 + m_Nb
  498.     Else
  499.         m_Nr = 6 + m_Nk
  500.     End If
  501.    
  502.     C1 = 1
  503.     If m_Nb < 8 Then
  504.         C2 = 2
  505.         C3 = 3
  506.     Else
  507.         C2 = 3
  508.         C3 = 4
  509.     End If
  510.    
  511.     For j = 0 To nb - 1
  512.         m = j * 3
  513.        
  514.         m_fi(m) = (j + C1) Mod nb
  515.         m_fi(m + 1) = (j + C2) Mod nb
  516.         m_fi(m + 2) = (j + C3) Mod nb
  517.         m_ri(m) = (nb + j - C1) Mod nb
  518.         m_ri(m + 1) = (nb + j - C2) Mod nb
  519.         m_ri(m + 2) = (nb + j - C3) Mod nb
  520.     Next
  521.    
  522.     N = m_Nb * (m_Nr + 1)
  523.    
  524.     For i = 0 To m_Nk - 1
  525.         j = i * 4
  526.         CipherKey(i) = PackFrom(KEY, j)
  527.     Next
  528.    
  529.     For i = 0 To m_Nk - 1
  530.         m_fkey(i) = CipherKey(i)
  531.     Next
  532.    
  533.     j = m_Nk
  534.     k = 0
  535.     Do While j < N
  536.         m_fkey(j) = m_fkey(j - m_Nk) Xor _
  537.             SubByte(RotateLeft(m_fkey(j - 1), 24)) Xor m_rco(k)
  538.         If m_Nk <= 6 Then
  539.             i = 1
  540.             Do While i < m_Nk And (i + j) < N
  541.                 m_fkey(i + j) = m_fkey(i + j - m_Nk) Xor _
  542.                     m_fkey(i + j - 1)
  543.                 i = i + 1
  544.             Loop
  545.         Else
  546.             ' Problem fixed here
  547.            i = 1
  548.             Do While i < 4 And (i + j) < N
  549.                 m_fkey(i + j) = m_fkey(i + j - m_Nk) Xor _
  550.                     m_fkey(i + j - 1)
  551.                 i = i + 1
  552.             Loop
  553.             If j + 4 < N Then
  554.                 m_fkey(j + 4) = m_fkey(j + 4 - m_Nk) Xor _
  555.                     SubByte(m_fkey(j + 3))
  556.             End If
  557.             i = 5
  558.             Do While i < m_Nk And (i + j) < N
  559.                 m_fkey(i + j) = m_fkey(i + j - m_Nk) Xor _
  560.                     m_fkey(i + j - 1)
  561.                 i = i + 1
  562.             Loop
  563.         End If
  564.        
  565.         j = j + m_Nk
  566.         k = k + 1
  567.     Loop
  568.    
  569.     For j = 0 To m_Nb - 1
  570.         m_rkey(j + N - nb) = m_fkey(j)
  571.     Next
  572.    
  573.     i = m_Nb
  574.     Do While i < N - m_Nb
  575.         k = N - m_Nb - i
  576.         For j = 0 To m_Nb - 1
  577.             m_rkey(k + j) = InvMixCol(m_fkey(i + j))
  578.         Next
  579.         i = i + m_Nb
  580.     Loop
  581.    
  582.     j = N - m_Nb
  583.     Do While j < N
  584.         m_rkey(j - N + m_Nb) = m_fkey(j)
  585.         j = j + 1
  586.     Loop
  587. End Sub
  588.  
  589. '*******************************************************************************
  590. ' encrypt (SUB)
  591. '*******************************************************************************
  592. Public Sub Encrypt(buff() As Byte)
  593.     Dim i       As Long
  594.     Dim j       As Long
  595.     Dim k       As Long
  596.     Dim m       As Long
  597.     Dim a(7)    As Long
  598.     Dim b(7)    As Long
  599.     Dim x()     As Long
  600.     Dim y()     As Long
  601.     Dim t()     As Long
  602.    
  603.     For i = 0 To m_Nb - 1
  604.         j = i * 4
  605.        
  606.         a(i) = PackFrom(buff, j)
  607.         a(i) = a(i) Xor m_fkey(i)
  608.     Next
  609.    
  610.     k = m_Nb
  611.     x = a
  612.     y = b
  613.    
  614.     For i = 1 To m_Nr - 1
  615.         For j = 0 To m_Nb - 1
  616.             m = j * 3
  617.             y(j) = m_fkey(k) Xor m_ftable(x(j) And m_lOnBits(7)) Xor _
  618.                 RotateLeft(m_ftable(RShift(x(m_fi(m)), 8) And m_lOnBits(7)), 8) Xor _
  619.                 RotateLeft(m_ftable(RShift(x(m_fi(m + 1)), 16) And m_lOnBits(7)), 16) Xor _
  620.                 RotateLeft(m_ftable(RShift(x(m_fi(m + 2)), 24) And m_lOnBits(7)), 24)
  621.             k = k + 1
  622.         Next
  623.         t = x
  624.         x = y
  625.         y = t
  626.     Next
  627.    
  628.     For j = 0 To m_Nb - 1
  629.         m = j * 3
  630.         y(j) = m_fkey(k) Xor m_fbsub(x(j) And m_lOnBits(7)) Xor _
  631.             RotateLeft(m_fbsub(RShift(x(m_fi(m)), 8) And m_lOnBits(7)), 8) Xor _
  632.             RotateLeft(m_fbsub(RShift(x(m_fi(m + 1)), 16) And m_lOnBits(7)), 16) Xor _
  633.             RotateLeft(m_fbsub(RShift(x(m_fi(m + 2)), 24) And m_lOnBits(7)), 24)
  634.         k = k + 1
  635.     Next
  636.    
  637.     For i = 0 To m_Nb - 1
  638.         j = i * 4
  639.         UnpackFrom y(i), buff, j
  640.         x(i) = 0
  641.         y(i) = 0
  642.     Next
  643. End Sub
  644.  
  645. '*******************************************************************************
  646. ' decrypt (SUB)
  647. '*******************************************************************************
  648. Public Sub Decrypt(buff() As Byte)
  649.     Dim i As Long
  650.     Dim j As Long
  651.     Dim k As Long
  652.     Dim m As Long
  653.     Dim a(7) As Long
  654.     Dim b(7) As Long
  655.     Dim x() As Long
  656.     Dim y() As Long
  657.     Dim t() As Long
  658.    
  659.     For i = 0 To m_Nb - 1
  660.         j = i * 4
  661.         a(i) = PackFrom(buff, j)
  662.         a(i) = a(i) Xor m_rkey(i)
  663.     Next
  664.    
  665.     k = m_Nb
  666.     x = a
  667.     y = b
  668.    
  669.     For i = 1 To m_Nr - 1
  670.         For j = 0 To m_Nb - 1
  671.             m = j * 3
  672.             y(j) = m_rkey(k) Xor m_rtable(x(j) And m_lOnBits(7)) Xor _
  673.                 RotateLeft(m_rtable(RShift(x(m_ri(m)), 8) And m_lOnBits(7)), 8) Xor _
  674.                 RotateLeft(m_rtable(RShift(x(m_ri(m + 1)), 16) And m_lOnBits(7)), 16) Xor _
  675.                 RotateLeft(m_rtable(RShift(x(m_ri(m + 2)), 24) And m_lOnBits(7)), 24)
  676.             k = k + 1
  677.         Next
  678.         t = x
  679.         x = y
  680.         y = t
  681.     Next
  682.    
  683.     For j = 0 To m_Nb - 1
  684.         m = j * 3
  685.        
  686.         y(j) = m_rkey(k) Xor m_rbsub(x(j) And m_lOnBits(7)) Xor _
  687.             RotateLeft(m_rbsub(RShift(x(m_ri(m)), 8) And m_lOnBits(7)), 8) Xor _
  688.             RotateLeft(m_rbsub(RShift(x(m_ri(m + 1)), 16) And m_lOnBits(7)), 16) Xor _
  689.             RotateLeft(m_rbsub(RShift(x(m_ri(m + 2)), 24) And m_lOnBits(7)), 24)
  690.         k = k + 1
  691.     Next
  692.    
  693.     For i = 0 To m_Nb - 1
  694.         j = i * 4
  695.        
  696.         UnpackFrom y(i), buff, j
  697.         x(i) = 0
  698.         y(i) = 0
  699.     Next
  700. End Sub
  701.  
  702. ''*******************************************************************************
  703. '' CopyBytesASP (SUB)
  704. ''
  705. '' Slower non-API function you can use to copy array data
  706. ''*******************************************************************************
  707. 'Private Sub CopyBytesASP(bytDest() As Byte, _
  708. '                         lDestStart As Long, _
  709. '                         bytSource() As Byte, _
  710. '                         lSourceStart As Long, _
  711. '                         lLength As Long)
  712. '    Dim lCount As Long
  713. '
  714. '    lCount = 0
  715. '    Do
  716. '        bytDest(lDestStart + lCount) = bytSource(lSourceStart + lCount)
  717. '        lCount = lCount + 1
  718. '    Loop Until lCount = lLength
  719. 'End Sub
  720.  
  721. '*******************************************************************************
  722. ' IsInitialized (FUNCTION)
  723. '*******************************************************************************
  724. Private Function IsInitialized(ByRef vArray As Variant) As Boolean
  725.     On Error Resume Next
  726.    
  727.     IsInitialized = IsNumeric(UBound(vArray))
  728. End Function
  729.  
  730. '*******************************************************************************
  731. ' EncryptData (FUNCTION)
  732. '
  733. ' Takes the message, whatever the size, and password in one call and does
  734. ' everything for you to return an encoded/encrypted message
  735. '*******************************************************************************
  736. Public Function EncryptData(bytMessage() As Byte, _
  737.                             bytPassword() As Byte) As Byte()
  738.     Dim bytKey(31)      As Byte
  739.     Dim bytIn()         As Byte
  740.     Dim bytOut()        As Byte
  741.     Dim bytTemp(31)     As Byte
  742.     Dim lCount          As Long
  743.     Dim lLength         As Long
  744.     Dim lEncodedLength  As Long
  745.     Dim bytLen(3)       As Byte
  746.     Dim lPosition       As Long
  747.    
  748.     If Not IsInitialized(bytMessage) Then
  749.         Exit Function
  750.     End If
  751.     If Not IsInitialized(bytPassword) Then
  752.         Exit Function
  753.     End If
  754.    
  755.     ' Use first 32 bytes of the password for the key
  756.    For lCount = 0 To UBound(bytPassword)
  757.         bytKey(lCount) = bytPassword(lCount)
  758.         If lCount = 31 Then
  759.             Exit For
  760.         End If
  761.     Next
  762.    
  763.     ' Prepare the key; assume 256 bit block and key size
  764.    gentables
  765.     gkey 8, 8, bytKey
  766.    
  767.     ' We are going to put the message size on the front of the message
  768.    ' in the first 4 bytes. If the length is more than a max int we are
  769.    ' in trouble
  770.    lLength = UBound(bytMessage) + 1
  771.     lEncodedLength = lLength + 4
  772.    
  773.     ' The encoded length includes the 4 bytes stuffed on the front
  774.    ' and is padded out to be modulus 32
  775.    If lEncodedLength Mod 32 <> 0 Then
  776.         lEncodedLength = lEncodedLength + 32 - (lEncodedLength Mod 32)
  777.     End If
  778.     ReDim bytIn(lEncodedLength - 1)
  779.     ReDim bytOut(lEncodedLength - 1)
  780.    
  781.     ' Put the length on the front
  782.    '* Unpack lLength, bytIn
  783.    CopyMemory VarPtr(bytIn(0)), VarPtr(lLength), 4
  784.     ' Put the rest of the message after it
  785.    '* CopyBytesASP bytIn, 4, bytMessage, 0, lLength
  786.    CopyMemory VarPtr(bytIn(4)), VarPtr(bytMessage(0)), lLength
  787.  
  788.     ' Encrypt a block at a time
  789.    For lCount = 0 To lEncodedLength - 1 Step 32
  790.         '* CopyBytesASP bytTemp, 0, bytIn, lCount, 32
  791.        CopyMemory VarPtr(bytTemp(0)), VarPtr(bytIn(lCount)), 32
  792.         Encrypt bytTemp
  793.         '* CopyBytesASP bytOut, lCount, bytTemp, 0, 32
  794.        CopyMemory VarPtr(bytOut(lCount)), VarPtr(bytTemp(0)), 32
  795.     Next
  796.    
  797.     EncryptData = bytOut
  798. End Function
  799.  
  800. '*******************************************************************************
  801. ' DecryptData (FUNCTION)
  802. '
  803. ' Opposite of Encryptdata
  804. '*******************************************************************************
  805. Public Function DecryptData(bytIn() As Byte, _
  806.                             bytPassword() As Byte) As Byte()
  807.     Dim bytMessage()    As Byte
  808.     Dim bytKey(31)      As Byte
  809.     Dim bytOut()        As Byte
  810.     Dim bytTemp(31)     As Byte
  811.     Dim lCount          As Long
  812.     Dim lLength         As Long
  813.     Dim lEncodedLength  As Long
  814.     Dim bytLen(3)       As Byte
  815.     Dim lPosition       As Long
  816.    
  817.     If Not IsInitialized(bytIn) Then
  818.         Exit Function
  819.     End If
  820.     If Not IsInitialized(bytPassword) Then
  821.         Exit Function
  822.     End If
  823.    
  824.     lEncodedLength = UBound(bytIn) + 1
  825.    
  826.     If lEncodedLength Mod 32 <> 0 Then
  827.         Exit Function
  828.     End If
  829.    
  830.     ' Use first 32 bytes of the password for the key
  831.    For lCount = 0 To UBound(bytPassword)
  832.         bytKey(lCount) = bytPassword(lCount)
  833.         If lCount = 31 Then
  834.             Exit For
  835.         End If
  836.     Next
  837.    
  838.     ' Prepare the key; assume 256 bit block and key size
  839.    gentables
  840.     gkey 8, 8, bytKey
  841.  
  842.     ' The output array needs to be the same size as the input array
  843.    ReDim bytOut(lEncodedLength - 1)
  844.    
  845.     ' Decrypt a block at a time
  846.    For lCount = 0 To lEncodedLength - 1 Step 32
  847.         '* CopyBytesASP bytTemp, 0, bytIn, lCount, 32
  848.        CopyMemory VarPtr(bytTemp(0)), VarPtr(bytIn(lCount)), 32
  849.         Decrypt bytTemp
  850.         '* CopyBytesASP bytOut, lCount, bytTemp, 0, 32
  851.        CopyMemory VarPtr(bytOut(lCount)), VarPtr(bytTemp(0)), 32
  852.     Next
  853.  
  854.     ' Get the original length of the string from the first 4 bytes
  855.    '* lLength = Pack(bytOut)
  856.    CopyMemory VarPtr(lLength), VarPtr(bytOut(0)), 4
  857.    
  858.     ' Make sure the length is consistent with our data
  859.    If lLength > lEncodedLength - 4 Then
  860.         Exit Function
  861.     End If
  862.    
  863.     ' Prepare the output message byte array
  864.    ReDim bytMessage(lLength - 1)
  865.     '* CopyBytesASP bytMessage, 0, bytOut, 4, lLength
  866.    CopyMemory VarPtr(bytMessage(0)), VarPtr(bytOut(4)), lLength
  867.    
  868.     DecryptData = bytMessage
  869. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement