[再次求助]AES加密时,模式设置为OFB,报错,出现异常
将AesCryptoServiceProvider的Mode设为OFB (代码中我注释了一大堆等号的位置),然后就报错了,我也不知道为什么 (报错位置是下面我注释了一大堆加好的地方)。
以下是代码
.Net框架版本是4.6.1
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 '================
aesAlg.Padding = PaddingMode.PKCS7
aesAlg.BlockSize = 128 'AES只支持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) '++++++++++++++++
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()
‘为了方便起见,这里就设定了一个非常简单的key和iv
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) '这里要求用户输入值
End Sub
End Module
运行时我输入的内容是:
123
然后报错:
“System.Security.Cryptography.CryptographicException”类型的未经处理的异常在 System.Core.dll 中发生
其他信息: 出现了内部错误。
查看错误详细信息:
未处理System.Security.Cryptography.CryptographicException
HResult=-2147023537
Message=出现了内部错误。
Source=System.Core
StackTrace:
在 System.Security.Cryptography.CapiNative.SetKeyParameter(SafeCapiKeyHandle key, KeyParameter parameter, Byte[] value)
在 System.Security.Cryptography.CapiSymmetricAlgorithm.SetupKey(SafeCapiKeyHandle key, Byte[] iv, CipherMode cipherMode, Int32 feedbackSize)
在 System.Security.Cryptography.AesCryptoServiceProvider.CreateEncryptor(Byte[] key, Byte[] iv)
在 ConsoleApplication_Test01.Module1.EncryptStringToBytes_Aes(String plainText, Byte[] Key, Byte[] IV) 位置 C:\Users\73744\AppData\Local\Temporary Projects\ConsoleApplication_Test01\Module1.vb:行号 17
在 ConsoleApplication_Test01.Module1.Main() 位置 C:\Users\73744\AppData\Local\Temporary Projects\ConsoleApplication_Test01\Module1.vb:行号 40
在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException:
两张关于错误信息的截图:(抱歉我等级论坛等级不够不能发图片和链接)
ht防tps://soc和ial.ms谐dn.micro防soft.co和m/Foru谐ms/getfile/903638
ht防tps://soc和ial.ms谐dn.micro防soft.co和m/Foru谐ms/getfile/903639
如果Mode设为CFB、ECB、CBC都正常工作。设为CTS,异常信息是不支持这种模式。
但这回这个OFB是内部错误,我就搞不懂了。
求助各位大大,这个是什么原因,怎么解决呢?谢谢