【问题标题】:"Invalid algorithm specified" when using AES with OFB cipher mode (VB.NET)将 AES 与 OFB 密码模式 (VB.NET) 一起使用时出现“指定的算法无效”
【发布时间】:2015-06-02 04:55:34
【问题描述】:

我必须使用 AES 加密和 OFB 密码模式,我使用 VB.NET 4.5、windows 8 和以下代码:

   Public Function DoEncryption(ByVal KeyArray() As Byte, ByVal IVArray() As Byte, ByVal Buffer As String) As Byte()
    Dim encrypted() As Byte
    Using a As Aes = Aes.Create()
        a.Mode = CipherMode.OFB

        Dim encryptor As ICryptoTransform
        encryptor = a.CreateEncryptor(KeyArray, IVArray)


        ' Create the streams used for encryption. 
        Using msEncrypt As New MemoryStream()
            Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                Using swEncrypt As New StreamWriter(csEncrypt)

                    'Write all data to the stream.
                    swEncrypt.Write(Buffer)
                End Using
                encrypted = msEncrypt.ToArray()
            End Using
        End Using
    End Using

    Return encrypted

End Function

我有一个错误“指定的算法无效”

  swEncrypt.Write(Buffer)

有什么建议吗?

【问题讨论】:

  • 不支持OFB。检查AesManaged.Mode Property 底部的注释:The CFB and OFB modes are not supported. 我不确定 Aes 类与 AesManaged 有多大不同,但如果支持它而不支持另一个会很奇怪。你可以看看 BouncyCastle
  • 非常感谢@Plutonix,我会尝试使用 nuget 的 Bouncy Castle 并提供反馈,奇怪的是当我尝试使用 CFB 时它可以正常工作:(

标签: vb.net algorithm encryption aes


【解决方案1】:

是的,正如@Plutonix 提到的,它使用 BouncyCastle 工作 我使用 Nuget 安装 BouncyCastle 后的新代码是:

Imports System.Security.Cryptography
Imports System.IO
Imports Org.BouncyCastle.Crypto
Imports Org.BouncyCastle.Security
Imports Org.BouncyCastle.Crypto.Parameters
Public Class EncryptionFunction
Public Function DoEncryption(ByVal KeyArray() As Byte, ByVal IVArray() As Byte, ByVal Buffer As Byte()) As Byte()

        Dim ae As New CipherKeyGenerator()
        ae.Init(New KeyGenerationParameters(New SecureRandom(), 256))
        Dim aesKeyParam As KeyParameter = ParameterUtilities.CreateKeyParameter("AES", KeyArray)
        Dim aesIVKeyParam As ParametersWithIV = New ParametersWithIV(aesKeyParam, IVArray)
        Dim cipher As IBufferedCipher = CipherUtilities.GetCipher("AES/OFB/NoPadding")
        cipher.Init(True, aesIVKeyParam)
        Dim encrypted() As Byte = cipher.DoFinal(Buffer)


        Return encrypted

    End Function
End Class

谢谢你:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-06
    • 2019-11-01
    • 2014-08-22
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多