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

📄 rijndael.cls

📁 一个类化的完整的AES实现方案.包含128Bit 256 Bit.不同的加密强度的完整的AES算法.同时也是一个成型的程序.可以用来直接加密需要保护的文件.
💻 CLS
📖 第 1 页 / 共 5 页
字号:
        m = (n + BlockSize - 1) And &HFFFFFFF0  'BlockSize=16 specific
        ReDim ciphertext(m - 1)
    Else
        m = (n + BlockSize) And &HFFFFFFF0      'BlockSize=16 specific
        ReDim ciphertext(m - 1)
        ciphertext(m - 1) = n Mod BlockSize
    End If

    For i = 0 To n - BlockSize Step BlockSize
        BlockEncrypt plaintext, ciphertext, i, i
    Next i

    If (n Mod BlockSize) <> 0 Then CopyMemory ciphertext(i), plaintext(i), n Mod BlockSize

    If i <> m Then BlockEncrypt ciphertext, ciphertext, i, i

End Function

'Decrypts an array encrypted with the ArrayEncrypt function
Public Function ArrayDecrypt(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(ciphertext) <> 0 Then Err.Raise 1, , "cRijndael.ArrayDecrypt - ciphertext must be zero based array"

    n = UBound(ciphertext) + 1
    If ((n Mod BlockSize) = 0) Then
        ReDim plaintext(n - 1)


        For i = 0 To n - BlockSize Step BlockSize
            BlockDecrypt plaintext, ciphertext, i, i
        Next i

        If appendsize Then
            If plaintext(n - 1) < BlockSize Then
                n = n - BlockSize + plaintext(n - 1)
                If n > 0 Then ReDim Preserve plaintext(n - 1)
            Else
                MsgBox "warning - incorrect length field"
                ArrayDecrypt = 1
            End If
        End If
    Else
        MsgBox "ciphertext size not a multiple of block size"
        ArrayDecrypt = -1
    End If

End Function




'File encryption.  The ciphertext file will be padded to a multiple of (BlockBits\8) bytes.
'One byte is appended to the ciphertext to indicate the length of the final block.
'FileDecrypt recovers this length field and restores the original plaintext file length.
'PlaintextFileName and CiphertextFileName must be distinct, PlaintextFileName must exist,
'CiphertextFileName must not exist, etc.  Be sure to do file checking before calling this.
Public Function FileEncrypt(PlaintextFileName As String, CiphertextFileName As String) As Long
    Dim hFile           As Long
    Dim hFile2          As Long
    Dim FileSizeHiWord  As Long
    Dim i               As Long
    Dim m               As Long 'ciphertext file size
    Dim n               As Long 'plaintext file size
    Dim data()          As Byte
    Dim nSize           As Long
    Const BlockSize     As Long = 16 'bytes
    Const MaxBlocks     As Long = MaxFileChunkSize \ BlockSize

    hFile = CreateFile(StrPtr(PlaintextFileName), GENERIC_READ, 0, ByVal 0&, OPEN_EXISTING, 0, 0)
    hFile2 = CreateFile(StrPtr(CiphertextFileName), GENERIC_WRITE, 0, ByVal 0&, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
    
    n = GetFileSize(hFile, FileSizeHiWord)
    m = (n + BlockSize) And (-BlockSize) 'BlockSize=16 specific
    'For large files, encrypt in pieces no larger than MaxFileChunkSize
    
    If m > MaxFileChunkSize Then
        ReDim data(MaxFileChunkSize - 1)
        Do
            ReadFile hFile, data(0), MaxFileChunkSize, nSize, ByVal 0&
            For i = 0 To (MaxBlocks - 1) * BlockSize Step BlockSize
                BlockEncrypt data, data, i, i
            Next i
            
            WriteFile hFile2, data(0), MaxFileChunkSize, nSize, ByVal 0&
            m = m - MaxFileChunkSize
        Loop While m > MaxFileChunkSize
    End If

    'Encrypt the last piece of the file
    ReDim data(m - 1)
    ReadFile hFile, data(0), m, nSize, ByVal 0&
    data(m - 1) = n Mod BlockSize
    
    For i = 0 To m - BlockSize Step BlockSize
        BlockEncrypt data, data, i, i
    Next i
    
    WriteFile hFile2, data(0), m, nSize, ByVal 0&

    CloseHandle hFile
    CloseHandle hFile2
End Function

'File decryption.
Public Function FileDecrypt(PlaintextFileName As String, CiphertextFileName As String) As Long
    Dim FileNum     As Integer
    Dim FileNum2    As Integer
    Dim i           As Long
    Dim m           As Long 'ciphertext file size
    Dim n           As Long 'plaintext file size
    Dim data()      As Byte
    Const BlockSize As Long = 16 'bytes
    Const MaxBlocks As Long = MaxFileChunkSize \ BlockSize

    m = FileLen(CiphertextFileName)
    If (m = 0) Or ((m And (BlockSize - 1)) <> 0) Then 'BlockSize=16 specific
        MsgBox "File Size Error - ciphertext file not a multiple of block size"
        FileDecrypt = 1
    Else
        FileNum = FreeFile
        Open CiphertextFileName For Binary Access Read As FileNum
        FileNum2 = FreeFile
        Open PlaintextFileName For Binary Access Write As FileNum2

        'For large files, decrypt in pieces no larger than MaxFileChunkSize
        If m > MaxBlocks * BlockSize Then
            ReDim data(MaxBlocks * BlockSize - 1)
            Do
                Get #FileNum, , data
                For i = 0 To (MaxBlocks - 1) * BlockSize Step BlockSize
                    BlockDecrypt data, data, i, i
                Next i
                
                Put #FileNum2, , data
                m = m - MaxBlocks * BlockSize
            Loop While m > MaxBlocks * BlockSize
        End If

        'Decrypt the last piece of the file
        ReDim data(m - 1)
        Get #FileNum, , data

⌨️ 快捷键说明

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