0

I encountered a problem when setting OFB mode.

My code is this

Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Module Module1

    Public Function EncryptStringToBytes_Aes(ByVal plainText As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
        Dim encrypted() As Byte
        Using aesAlg As New AesCryptoServiceProvider()
            aesAlg.Mode = CipherMode.OFB '▲▲▲Here I set the mode as OFB
            aesAlg.Padding = PaddingMode.PKCS7
            aesAlg.BlockSize = 128 'this class only supports 128
            aesAlg.Key = Key
            If aesAlg.Mode <> CipherMode.ECB Then
                aesAlg.IV = IV
            End If
            Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV) '★★★Here I met the Exception
            Dim msEncrypt As New MemoryStream()
            Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                Using swEncrypt As New StreamWriter(csEncrypt)
                    swEncrypt.Write(plainText)
                End Using
                encrypted = msEncrypt.ToArray()
            End Using
        End Using
    Return encrypted
    End Function

    Sub Main()
        'To simplfy the question, here I used a very simple Key and Initialization Vector.
        Dim key(31) As Byte
        Dim iv(15) As Byte
        Dim i As Integer
        For i = 0 To 31
            key(i) = CByte(i + 1)
            If i <= 15 Then
                iv(i) = CByte(i + 1)
            End If
        Next i
        Dim temp As Byte() = EncryptStringToBytes_Aes(Console.ReadLine, key, iv) 'Here I asked users to type some words for encrypting.
    End Sub

End Module

I run this program, and entered "123". Then I met the exception called CryptographicException, and it's detailed information is InnerException.

I don't know what happened. I am not a professional programmer. I am just a amateur, and I am new. I work in China and I am using the Chinese version. So I don't know how to translate the detailed information into English. But I think I should paste it here:

System.Security.Cryptography.CryptographicException was unhandled
  HResult=-2147023537
  Message=met Inner Exception。

  Source=System.Core
  StackTrace:
       at System.Security.Cryptography.CapiNative.SetKeyParameter(SafeCapiKeyHandle key, KeyParameter parameter, Byte[] value)
       at System.Security.Cryptography.CapiSymmetricAlgorithm.SetupKey(SafeCapiKeyHandle key, Byte[] iv, CipherMode cipherMode, Int32 feedbackSize)
       at System.Security.Cryptography.AesCryptoServiceProvider.CreateEncryptor(Byte[] key, Byte[] iv)
       at ConsoleApplication1.Module1.EncryptStringToBytes_Aes(String plainText, Byte[] Key, Byte[] IV) in C:\Users\73744\AppData\Local\Temporary Projects\ConsoleApplication1\Module1.vb:line 17
       at ConsoleApplication1.Module1.Main() in C:\Users\73744\AppData\Local\Temporary Projects\ConsoleApplication1\Module1.vb:line 40
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
djv
  • 15,168
  • 7
  • 48
  • 72
Kane Green
  • 71
  • 7
  • Can you show a test key and IV with the same size, in hexadecimals please? What if you remove `PaddingMode.PKCS7`? OFB doesn't require padding. – Maarten Bodewes Aug 02 '16 at 13:28
  • The top exception shows that the key is not accepted. What is the size of the key in bytes? – Maarten Bodewes Aug 02 '16 at 13:40
  • 1
    According to [this on MSDN](https://social.msdn.microsoft.com/Forums/vstudio/en-US/891955cd-3125-487c-9746-9fd07f24b12f/why-net-doesnt-support-aes-ofb?forum=netfxbcl) AES doesnt support OFB – Ňɏssa Pøngjǣrdenlarp Aug 02 '16 at 13:48
  • @Plutonix Interesting, but I'm wondering why you'd get an exception in `SetKeyParameter` in that case. Kane, could you also show the inner exception? Please react to comments. – Maarten Bodewes Aug 02 '16 at 14:25
  • 1
    When I tested the code I got a different error (invalid algorithm) at the end of the using block. @MaartenBodewes And it worked fine using the default block mode – Ňɏssa Pøngjǣrdenlarp Aug 02 '16 at 14:27
  • 1
    @Plutonix funny, I think [I implemented OFB once for .NET](http://stackoverflow.com/questions/24515521/aes-ofb-encryption-for-rijndaelmanaged), completely forgot about it after. Horrid exception and horrid that the OFB mode is not algorithm agnostic (then again, it's a mode that is basically not used anymore and maybe they don't want to create tests for it). – Maarten Bodewes Aug 02 '16 at 16:49
  • @MaartenBodewes Firstly, thank you for answering my question. (1)The default value of PaddingMode is just PKCS7. While I remove this sentence, it doesn't help. While I set it as PaddingMode.None, in OFB mode, still the inner exception. In other mode like CFB, it cannot work, it needs padding. (2)This code is using 256-bit key. I've tried to use 128-bit key, but the inner exception still occurs. (3).Net provide 5 modes: ECB, CBC, CFB, OFB, CTS. I know AESManaged doesn't support OFB, CFB and CTS. And I know that AES is exactly Rijndael, but according MSDN, they are different implementations. – Kane Green Aug 04 '16 at 02:26
  • @Plutonix Follwing my last reply: I wander if there are some differences in two methods in .Net. (4)I got the detailed information just as I've pasted. You can see, it is empty after the words "InnerException:". So, I don't understand what has happened. (5)Plutonix, I don't understand "at the end of the using block" and "the default block mode" (maybe my English is poor). Do you mean BlockSize ? And you said "it worked fine", Oh, my God! Does .Net frameworks with different language packages work differently?? (I heard Java in English supports 256-bit AES, but the Chinese version does NOT) – Kane Green Aug 04 '16 at 02:41

0 Answers0