⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rijndael.cls

📁 这是一套完整的AES加密解密程序,用VB编写,调试可用.
💻 CLS
📖 第 1 页 / 共 5 页
字号:
        i = 4
        CopyMemory fkey(0), pass(0), 4& * i
        For j = 0 To 28
            CopyMemory s(0), fkey(i - 1), 4&
            fkey(i) = fkey(i - 4) Xor (Te4(s(0)) And &HFF000000) _
                                  Xor (Te4(s(3)) And &HFF0000) _
                                  Xor (Te4(s(2)) And &HFF00&) _
                                  Xor (Te4(s(1)) And &HFF&) _
                                  Xor rco(j)
            fkey(i + 1) = fkey(i - 3) Xor fkey(i)
            fkey(i + 2) = fkey(i - 2) Xor fkey(i + 1)
            fkey(i + 3) = fkey(i - 1) Xor fkey(i + 2)
            i = i + 4
        Next j
        Nr = 14
'Debug.Assert i = (Nr + 1) * 8
#If SUPPORT_LEVEL = 2 Then
    Case 160
        i = 5
        CopyMemory fkey(0), pass(0), 4& * i
        For j = 0 To 22
            CopyMemory s(0), fkey(i - 1), 4&
            fkey(i) = fkey(i - 5) Xor (Te4(s(0)) And &HFF000000) _
                                  Xor (Te4(s(3)) And &HFF0000) _
                                  Xor (Te4(s(2)) And &HFF00&) _
                                  Xor (Te4(s(1)) And &HFF&) _
                                  Xor rco(j)
            fkey(i + 1) = fkey(i - 4) Xor fkey(i)
            fkey(i + 2) = fkey(i - 3) Xor fkey(i + 1)
            fkey(i + 3) = fkey(i - 2) Xor fkey(i + 2)
            fkey(i + 4) = fkey(i - 1) Xor fkey(i + 3)
            i = i + 5
        Next j
        Nr = 14
'Debug.Assert i = (Nr + 1) * 8
#End If
    Case 192
        i = 6
        CopyMemory fkey(0), pass(0), 4& * i
        For j = 0 To 18
            CopyMemory s(0), fkey(i - 1), 4&
            fkey(i) = fkey(i - 6) Xor (Te4(s(0)) And &HFF000000) _
                                  Xor (Te4(s(3)) And &HFF0000) _
                                  Xor (Te4(s(2)) And &HFF00&) _
                                  Xor (Te4(s(1)) And &HFF&) _
                                  Xor rco(j)
            fkey(i + 1) = fkey(i - 5) Xor fkey(i)
            fkey(i + 2) = fkey(i - 4) Xor fkey(i + 1)
            fkey(i + 3) = fkey(i - 3) Xor fkey(i + 2)
            fkey(i + 4) = fkey(i - 2) Xor fkey(i + 3)
            fkey(i + 5) = fkey(i - 1) Xor fkey(i + 4)
            i = i + 6
        Next j
        Nr = 14
'Debug.Assert i = (Nr + 1) * 8
#If SUPPORT_LEVEL = 2 Then
    Case 224
        i = 7
        j = 0
        CopyMemory fkey(0), pass(0), 4& * i
        Do
            CopyMemory s(0), fkey(i - 1), 4&
            fkey(i) = fkey(i - 7) Xor (Te4(s(0)) And &HFF000000) _
                                  Xor (Te4(s(3)) And &HFF0000) _
                                  Xor (Te4(s(2)) And &HFF00&) _
                                  Xor (Te4(s(1)) And &HFF&) _
                                  Xor rco(j)
            If j = 16 Then Exit Do
            fkey(i + 1) = fkey(i - 6) Xor fkey(i)
            fkey(i + 2) = fkey(i - 5) Xor fkey(i + 1)
            fkey(i + 3) = fkey(i - 4) Xor fkey(i + 2)
            CopyMemory s(0), fkey(i + 3), 4&
            fkey(i + 4) = fkey(i - 3) Xor (Te4(s(3)) And &HFF000000) _
                                      Xor (Te4(s(2)) And &HFF0000) _
                                      Xor (Te4(s(1)) And &HFF00&) _
                                      Xor (Te4(s(0)) And &HFF&)
            fkey(i + 5) = fkey(i - 2) Xor fkey(i + 4)
            fkey(i + 6) = fkey(i - 1) Xor fkey(i + 5)
            i = i + 7
            j = j + 1
        Loop
        Nr = 14
'Debug.Assert i + 1 = (Nr + 1) * 8
#End If
    Case 256
        i = 8
        CopyMemory fkey(0), pass(0), 4& * i
        For j = 0 To 13
            CopyMemory s(0), fkey(i - 1), 4&
            fkey(i) = fkey(i - 8) Xor (Te4(s(0)) And &HFF000000) _
                                  Xor (Te4(s(3)) And &HFF0000) _
                                  Xor (Te4(s(2)) And &HFF00&) _
                                  Xor (Te4(s(1)) And &HFF&) _
                                  Xor rco(j)
            fkey(i + 1) = fkey(i - 7) Xor fkey(i)
            fkey(i + 2) = fkey(i - 6) Xor fkey(i + 1)
            fkey(i + 3) = fkey(i - 5) Xor fkey(i + 2)
            CopyMemory s(0), fkey(i + 3), 4&
            fkey(i + 4) = fkey(i - 4) Xor (Te4(s(3)) And &HFF000000) _
                                      Xor (Te4(s(2)) And &HFF0000) _
                                      Xor (Te4(s(1)) And &HFF00&) _
                                      Xor (Te4(s(0)) And &HFF&)
            fkey(i + 5) = fkey(i - 3) Xor fkey(i + 4)
            fkey(i + 6) = fkey(i - 2) Xor fkey(i + 5)
            fkey(i + 7) = fkey(i - 1) Xor fkey(i + 6)
            i = i + 8
        Next j
        Nr = 14
'Debug.Assert i = (Nr + 1) * 8
    Case Else
        Err.Raise 1, , "cRijndael.SetCipherKey - Illegal KeyBits Value"
        SetCipherKey = 1
        Exit Function
    End Select
    CreateDecryptionKeys 8

    Case Else
        Err.Raise 1, , "cRijndael.SetCipherKey - Illegal BlockBits Value"
        SetCipherKey = 1
        Exit Function
    End Select
#End If
End Function

#If SUPPORT_LEVEL Then
Public Function SetCipherKeyString(PassPhrase As String, KeyBits As Long, BlockBits As Long) As Long
    Dim pass() As Byte

    pass = StrConv(PassPhrase, vbFromUnicode)
    ReDim Preserve pass(31)
    SetCipherKeyString = SetCipherKey(pass, KeyBits, BlockBits)
End Function
#Else
Public Function SetCipherKeyString(PassPhrase As String, KeyBits As Long) As Long
    Dim pass() As Byte

    pass = StrConv(PassPhrase, vbFromUnicode)
    ReDim Preserve pass(31)
    SetCipherKeyString = SetCipherKey(pass, KeyBits)
End Function
#End If


'Encrypt a 128 bit block.  plaintext(p ... p+15) is input, ciphertext(q ... q+15) is output.
'plaintext and ciphertext can be the same array.  Will crash if ciphertext(q ... q+15) is not allocated.
Public Sub BlockEncrypt(plaintext() As Byte, ciphertext() As Byte, p As Long, q As Long)
    Dim i     As Long
    Dim k     As Long
    Dim t0    As Long
    Dim t1    As Long
    Dim t2    As Long
    Dim t3    As Long
    Dim s(15) As Byte

    CopyMemory t0, plaintext(p + 0), 4&
    CopyMemory t1, plaintext(p + 4), 4&
    CopyMemory t2, plaintext(p + 8), 4&
    CopyMemory t3, plaintext(p + 12), 4&
    t0 = t0 Xor fkey(0)
    t1 = t1 Xor fkey(1)
    t2 = t2 Xor fkey(2)
    t3 = t3 Xor fkey(3)
    k = 4

    For i = 1 To Nr - 1 'Nr is number of rounds
        CopyMemory s(0), t0, 4&
        CopyMemory s(4), t1, 4&
        CopyMemory s(8), t2, 4&
        CopyMemory s(12), t3, 4&
        t0 = Te0(s(0)) Xor Te1(s(5)) Xor Te2(s(10)) Xor Te3(s(15)) Xor fkey(k + 0)
        t1 = Te0(s(4)) Xor Te1(s(9)) Xor Te2(s(14)) Xor Te3(s(3)) Xor fkey(k + 1)
        t2 = Te0(s(8)) Xor Te1(s(13)) Xor Te2(s(2)) Xor Te3(s(7)) Xor fkey(k + 2)
        t3 = Te0(s(12)) Xor Te1(s(1)) Xor Te2(s(6)) Xor Te3(s(11)) Xor fkey(k + 3)
        k = k + 4
    Next i

    'Final round
    CopyMemory s(0), t0, 4&
    CopyMemory s(4), t1, 4&
    CopyMemory s(8), t2, 4&
    CopyMemory s(12), t3, 4&
    t0 = (Te4(s(0)) And &HFF&) Xor (Te4(s(5)) And &HFF00&) Xor (Te4(s(10)) And &HFF0000) Xor (Te4(s(15)) And &HFF000000) Xor fkey(k + 0)
    t1 = (Te4(s(4)) And &HFF&) Xor (Te4(s(9)) And &HFF00&) Xor (Te4(s(14)) And &HFF0000) Xor (Te4(s(3)) And &HFF000000) Xor fkey(k + 1)
    t2 = (Te4(s(8)) And &HFF&) Xor (Te4(s(13)) And &HFF00&) Xor (Te4(s(2)) And &HFF0000) Xor (Te4(s(7)) And &HFF000000) Xor fkey(k + 2)
    t3 = (Te4(s(12)) And &HFF&) Xor (Te4(s(1)) And &HFF00&) Xor (Te4(s(6)) And &HFF0000) Xor (Te4(s(11)) And &HFF000000) Xor fkey(k + 3)
    CopyMemory ciphertext(q + 0), t0, 4&
    CopyMemory ciphertext(q + 4), t1, 4&
    CopyMemory ciphertext(q + 8), t2, 4&
    CopyMemory ciphertext(q + 12), t3, 4&
End Sub

'Decrypt a 128 bit block.  ciphertext(q ... q+15) is input, plaintext(p ... p+15) is output.
'plaintext and ciphertext can be the same array.  Will crash if plaintext(p ... p+15) is not allocated.
Public Sub BlockDecrypt(plaintext() As Byte, ciphertext() As Byte, p As Long, q As Long)
    Dim i     As Long
    Dim k     As Long
    Dim t0    As Long
    Dim t1    As Long
    Dim t2    As Long
    Dim t3    As Long
    Dim s(15) As Byte

    CopyMemory t0, ciphertext(q + 0), 4&
    CopyMemory t1, ciphertext(q + 4), 4&
    CopyMemory t2, ciphertext(q + 8), 4&
    CopyMemory t3, ciphertext(q + 12), 4&
    t0 = t0 Xor rkey(0)
    t1 = t1 Xor rkey(1)
    t2 = t2 Xor rkey(2)
    t3 = t3 Xor rkey(3)
    k = 4

    For i = 1 To Nr - 1 'Nr is number of rounds
        CopyMemory s(0), t0, 4&
        CopyMemory s(4), t1, 4&
        CopyMemory s(8), t2, 4&
        CopyMemory s(12), t3, 4&
        t0 = Td0(s(0)) Xor Td1(s(13)) Xor Td2(s(10)) Xor Td3(s(7)) Xor rkey(k + 0)
        t1 = Td0(s(4)) Xor Td1(s(1)) Xor Td2(s(14)) Xor Td3(s(11)) Xor rkey(k + 1)
        t2 = Td0(s(8)) Xor Td1(s(5)) Xor Td2(s(2)) Xor Td3(s(15)) Xor rkey(k + 2)
        t3 = Td0(s(12)) Xor Td1(s(9)) Xor Td2(s(6)) Xor Td3(s(3)) Xor rkey(k + 3)
        k = k + 4
    Next i

    'Final round
    CopyMemory s(0), t0, 4&
    CopyMemory s(4), t1, 4&
    CopyMemory s(8), t2, 4&
    CopyMemory s(12), t3, 4&
    t0 = (Td4(s(0)) And &HFF&) Xor (Td4(s(13)) And &HFF00&) Xor (Td4(s(10)) And &HFF0000) Xor (Td4(s(7)) And &HFF000000) Xor rkey(k + 0)
    t1 = (Td4(s(4)) And &HFF&) Xor (Td4(s(1)) And &HFF00&) Xor (Td4(s(14)) And &HFF0000) Xor (Td4(s(11)) And &HFF000000) Xor rkey(k + 1)
    t2 = (Td4(s(8)) And &HFF&) Xor (Td4(s(5)) And &HFF00&) Xor (Td4(s(2)) And &HFF0000) Xor (Td4(s(15)) And &HFF000000) Xor rkey(k + 2)
    t3 = (Td4(s(12)) And &HFF&) Xor (Td4(s(9)) And &HFF00&) Xor (Td4(s(6)) And &HFF0000) Xor (Td4(s(3)) And &HFF000000) Xor rkey(k + 3)
    CopyMemory plaintext(p + 0), t0, 4&
    CopyMemory plaintext(p + 4), t1, 4&
    CopyMemory plaintext(p + 8), t2, 4&
    CopyMemory plaintext(p + 12), t3, 4&
End Sub

#If SUPPORT_LEVEL Then
#If SUPPORT_LEVEL = 2 Then
'Encrypt a 160 bit block.  plaintext(p ... p+19) is input, ciphertext(q ... q+19) is output.
'plaintext and ciphertext can be the same array.  Will crash if ciphertext(q ... q+19) is not allocated.
Public Sub Block160Encrypt(plaintext() As Byte, ciphertext() As Byte, p As Long, q As Long)
    Dim i     As Long
    Dim k     As Long
    Dim t0    As Long
    Dim t1    As Long
    Dim t2    As Long
    Dim t3    As Long
    Dim t4    As Long
    Dim s(19) As Byte

    CopyMemory t0, plaintext(p + 0), 4&
    CopyMemory t1, plaintext(p + 4), 4&
    CopyMemory t2, plaintext(p + 8), 4&
    CopyMemory t3, plaintext(p + 12), 4&
    CopyMemory t4, plaintext(p + 16), 4&
    t0 = t0 Xor fkey(0)
    t1 = t1 Xor fkey(1)
    t2 = t2 Xor fkey(2)
    t3 = t3 Xor fkey(3)
    t4 = t4 Xor fkey(4)
    k = 5

    For i = 1 To Nr - 1 'Nr is number of rounds
        CopyMemory s(0), t0, 4&
        CopyMemory s(4), t1, 4&
        CopyMemory s(8), t2, 4&
        CopyMemory s(12), t3, 4&
        CopyMemory s(16), t4, 4&
        t0 = Te0(s(0)) Xor Te1(s(5)) Xor Te2(s(10)) Xor Te3(s(15)) Xor fkey(k + 0)
        t1 = Te0(s(4)) Xor Te1(s(9)) Xor Te2(s(14)) Xor Te3(s(19)) Xor fkey(k + 1)
        t2 = Te0(s(8)) Xor Te1(s(13)) Xor Te2(s(18)) Xor Te3(s(3)) Xor fkey(k + 2)
        t3 = Te0(s(12)) Xor Te1(s(17)) Xor Te2(s(2)) Xor Te3(s(7)) Xor fkey(k + 3)
        t4 = Te0(s(16)) Xor Te1(s(1)) Xor Te2(s(6)) Xor Te3(s(11)) Xor fkey(k + 4)
        k = k + 5
    Next i

    'Final round
    CopyMemory s(0), t0, 4&
    CopyMemory s(4), t1, 4&
    CopyMemory s(8), t2, 4&
    CopyMemory s(12), t3, 4&
    CopyMemory s(16), t4, 4&
    t0 = (Te4(s(0)) And &HFF&) Xor (Te4(s(5)) And &HFF00&) Xor (Te4(s(10)) And &HFF0000) Xor (Te4(s(15)) And &HFF000000) Xor fkey(k + 0)
    t1 = (Te4(s(4)) And &HFF&) Xor (Te4(s(9)) And &HFF00&) Xor (Te4(s(14)) And &HFF0000) Xor (Te4(s(19)) And &HFF000000) Xor fkey(k + 1)
    t2 = (Te4(s(8)) And &HFF&) Xor (Te4(s(13)) And &HFF00&) Xor (Te4(s(18)) And &HFF0000) Xor (Te4(s(3)) And &HFF000000) Xor fkey(k + 2)
    t3 = (Te4(s(12)) And &HFF&) Xor (Te4(s(17)) And &HFF00&) Xor (Te4(s(2)) And &HFF0000) Xor (Te4(s(7)) And &HFF000000) Xor fkey(k + 3)
    t4 = (Te4(s(16)) And &HFF&) Xor (Te4(s(1)) And &HFF00&) Xor (Te4(s(6)) And &HFF0000) Xor (Te4(s(11)) And &HFF000000) Xor fkey(k + 4)
    CopyMemory ciphertext(q + 0), t0, 4&
    CopyMemory ciphertext(q + 4), t1, 4&
    CopyMemory ciphertext(q + 8), t2, 4&
    CopyMemory ciphertext(q + 12), t3, 4&
    CopyMemory ciphertext(q + 16), t4, 4&
End Sub

'Decrypt a 160 bit block.  ciphertext(q ... q+19) is input, plaintext(p ... p+19) is output.
'plaintext and ciphertext can be the same array.  Will crash if plaintext(p ... p+19) is not allocated.
Public Sub Block160Decrypt(plaintext() As Byte, ciphertext() As Byte, p As Long, q As Long)
    Dim i     As Long
    Dim k     As Long
    Dim t0    As Long
    Dim t1    As Long
    Dim t2    As Long
    Dim t3    As Long
    Dim t4    As Long
    Dim s(19) As Byte

    CopyMemory t0, ciphertext(q + 0), 4&
    CopyMemory t1, ciphertext(q + 4), 4&
    CopyMemory t2, ciphertext(q + 8), 4&
    CopyMemory t3, ciphertext(q + 12), 4&
    CopyMemory t4, ciphertext(q + 16), 4&
    t0 = t0 Xor rkey(0)
    t1 = t1 Xor rkey(1)
    t2 = t2 Xor rkey(2)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -