excel - VBA AES CBC 加密

标签 excel vba encryption aes cbc-mode

我在 https://github.com/susam/aes.vbs 中提到了加密下面是我最终得到的代码

Function Min(a, b)
    Min = a
    If b < a Then Min = b
End Function

Function B64Encode(bytes)
    Dim result As String
    Dim b64Block() As Byte
    Dim b64Enc As Object
    Dim utf8 As Object
    Dim Offset, Length, BlockSize As Integer
    
    Set b64Enc = CreateObject("System.Security.Cryptography.ToBase64Transform")
    Set utf8 = CreateObject("System.Text.UTF8Encoding")
    BlockSize = b64Enc.InputBlockSize
    For Offset = 0 To LenB(bytes) - 1 Step BlockSize
        Length = Min(BlockSize, UBound(bytes) - Offset)
        b64Block = b64Enc.TransformFinalBlock((bytes), Offset, Length)
        result = result & utf8.GetString((b64Block))
    Next
    B64Encode = result
End Function

Function B64Decode(b64Str)
    Dim utf8 As Object
    Dim bytes() As Byte
    Dim b64Dec As Object
    
    Set utf8 = CreateObject("System.Text.UTF8Encoding")
    Set b64Dec = CreateObject("System.Security.Cryptography.FromBase64Transform")
    bytes = utf8.GetBytes_4(b64Str)
    B64Decode = b64Dec.TransformFinalBlock((bytes), 0, UBound(bytes))
End Function

Function Encrypt(plaintext, aesKey)
    Dim cipherBytes, aesKeyBytes, ivKeyBytes, plainBytes() As Byte
    
    Dim utf8, AES, aesEnc As Object
    Dim aesIV() As Byte
    Set AES = CreateObject("System.Security.Cryptography.RijndaelManaged")
    Set utf8 = CreateObject("System.Text.UTF8Encoding")
    'Set cipherMode = GetObject("System.Security.Cryptography.CipherMode")
    AES.KeySize = 256
    AES.Mode = 1
    AES.Key = CreateObject("System.Text.UTF8Encoding").GetBytes_4("V$ry300DP3r$0NM3")
    AES.IV = CreateObject("System.Text.UTF8Encoding").GetBytes_4("HR$2pIjHR$2pIjPa")
    plainBytes = utf8.GetBytes_4(plaintext)
    'Set aesEnc = AES.CreateEncryptor_2((aesKeyBytes), (ivKeyBytes))
    cipherBytes = AES.TransformFinalBlock((plainBytes), 0, UBound(plainBytes))
        
    Encrypt = B64Encode(cipherBytes)
End Function

现在上面的代码告诉我“指定的初始化向量 (iv) 与该算法的 block 大小不匹配”

我们不能设置 IV 动态吗?

最佳答案

Function Min(a, b)
    Min = a
    If b < a Then Min = b
End Function

Function B64Encode(bytes)
    Dim result As String
    Dim b64Block() As Byte
    Dim b64Enc As Object
    Dim utf8 As Object
    Dim Offset, Length, BlockSize As Integer
    
    Set b64Enc = CreateObject("System.Security.Cryptography.ToBase64Transform")
    Set utf8 = CreateObject("System.Text.UTF8Encoding")
    BlockSize = b64Enc.InputBlockSize
    For Offset = 0 To LenB(bytes) - 1 Step BlockSize
        Length = Min(BlockSize, UBound(bytes) - Offset)
        b64Block = b64Enc.TransformFinalBlock((bytes), Offset, Length)
        result = result & utf8.GetString((b64Block))
    Next
    B64Encode = result
End Function

Function B64Decode(b64Str)
    Dim utf8 As Object
    Dim bytes() As Byte
    Dim b64Dec As Object
    
    Set utf8 = CreateObject("System.Text.UTF8Encoding")
    Set b64Dec = CreateObject("System.Security.Cryptography.FromBase64Transform")
    bytes = utf8.GetBytes_4(b64Str)
    B64Decode = b64Dec.TransformFinalBlock((bytes), 0, UBound(bytes))
End Function


Function Encrypt(plaintext, aesKey)
    Dim cipherBytes, aesKeyBytes, ivKeyBytes, plainBytes() As Byte
    
    Dim utf8, AES, aesEnc, cipherMode As Object
    Dim aesIV() As Byte
        
    Set AES = CreateObject("System.Security.Cryptography.RijndaelManaged")
    Set utf8 = CreateObject("System.Text.UTF8Encoding")
    'Set cipherMode = CreateObject("System.Security.Cryptography.CipherMode")
    
    AES.KeySize = 256
    AES.BlockSize = 256
    'CipherMode.CBC
    AES.Mode = 1
    'PaddingMode.PKCS7
    AES.Padding = 2
    AES.Key = CreateObject("System.Text.UTF8Encoding").GetBytes_4("ThirtyTwoBytes3$ThirtyTwoBytes3$")
    AES.IV = CreateObject("System.Text.UTF8Encoding").GetBytes_4("3$ThreeTwoBytes3$ThreeTwoBytes3$")
    'plainBytes = utf8.GetBytes_4(plaintext)
    plainBytes = B64Decode(plaintext)
    'Set aesEnc = AES.CreateEncryptor_2((aesKeyBytes), (ivKeyBytes))
    cipherBytes = AES.CreateEncryptor().TransformFinalBlock((plainBytes), 0, UBound(plainBytes))
        
    Encrypt = B64Encode(cipherBytes)
End Function

Sub encrypt_hell()
Debug.Print Encrypt("Hello", "PattamuthuArumug")
End Sub

我使用的是 AES CBC 256 位,因此必须使用 32 字节的 key 和 IV

关于excel - VBA AES CBC 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63730549/

相关文章:

excel - 如何判断sumproduct是否需要用CSE插入?

excel - 将 Excel 图表发布到 Lotus Notes 电子邮件中

vba - 如何检查单元格值以更改另一个单元格的值?

c# - 加密和解密查询字符串导致附加未知文本

excel - 我可以使用 VBA 让 Excel 忘记它跟随超链接吗?

vba - 在多列中显示小计

vba - 根据另一个工作表中的行数在表中插入行

c# - rijndael 加密 - 只有部分字符串被解密

java - 安卓 API 22 | java.security.NoSuchAlgorithmException : KeyGenerator RSA implementation not found

excel - 使用 Gmail 发送包含电子表格内容的电子邮件的宏