首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >VBA AES CBC加密

VBA AES CBC加密
EN

Stack Overflow用户
提问于 2020-09-04 03:36:48
回答 1查看 2.3K关注 0票数 3

我在https://github.com/susam/aes.vbs中提到了加密,下面是我用来实现的代码

代码语言:javascript
运行
复制
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)与此算法的块大小不匹配”

我们不能将IV设置为动态的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-10 04:16:40

代码语言:javascript
运行
复制
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字节密钥和IV

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63730549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档