1

I have to use AES encryption with OFB cipher mode , I use VB.NET 4.5 ,windows 8 and the following code:

   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

I have an error "Invalid algorithm specified" at

  swEncrypt.Write(Buffer)

any suggestions?

  • 1
    OFB is not supported. Examine the remarks at the bottom of [AesManaged.Mode Property](https://msdn.microsoft.com/en-us/library/system.security.cryptography.aesmanaged.mode(v=vs.110).aspx): `The CFB and OFB modes are not supported.` I'm not sure how different the Aes class is from AesManaged, but it would be weird for one to support it and not the other. You might look at BouncyCastle – Ňɏssa Pøngjǣrdenlarp May 31 '15 at 20:26
  • Thank you very much @Plutonix , I will try the Bouncy Castle from nuget and give a feed back , the weird thing is when I tried to use CFB it works correctly :( – Wadea Asad Hijjawi May 31 '15 at 21:00

1 Answers1

1

Yes it works using BouncyCastle as @Plutonix mentioned my new code after install BouncyCastle using Nuget is:

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

Thank you :)