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

📄 rijndael.cls

📁 一个类化的完整的AES实现方案.包含128Bit 256 Bit.不同的加密强度的完整的AES算法.同时也是一个成型的程序.可以用来直接加密需要保护的文件.
💻 CLS
📖 第 1 页 / 共 5 页
字号:
            fkey(i + 3) = fkey(i - 5) Xor fkey(i + 2)
            If j = 6 Then Exit Do
            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
            j = j + 1
        Loop
        Nr = 14
'Debug.Assert i + 4 = (Nr + 1) * 4
    Case Else
        Err.Raise 1, , "cRijndael.SetCipherKey - Illegal KeyBits Value"
        SetCipherKey = 1
        Exit Function
    End Select
    CreateDecryptionKeys 4

End Function

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


'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


'Encrypt an arbritrary size array in blocks.  plaintext(0 ... n-1) is input,
'ciphertext(0 ... m-1) is output, where m is n padded to a multiple of (BlockBits\8) bytes.
'plaintext and ciphertext must be distinct, and ciphertext must be a redimensionable array.
'If appendsize is non-zero, a one byte length field is appended to the plaintext
'before encrypting so the original length can be retrieved after decryption.
Public Function ArrayEncrypt(plaintext() As Byte, ciphertext() As Byte, appendsize As Long) As Long
    Dim i           As Long
    Dim m           As Long
    Dim n           As Long
    Const BlockSize As Long = 16 'bytes

    If LBound(plaintext) <> 0 Then Err.Raise 1, , "cRijndael.ArrayEncrypt - plaintext must be zero based array"

    n = UBound(plaintext) + 1
    If appendsize = 0 Then

⌨️ 快捷键说明

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