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: