在OFB模式下使用AesCryptoServiceProvider时出现CryptographicException

时间:2016-08-02 13:11:37

标签: .net vb.net cryptography .net-4.6.1

设置OFB模式时遇到问题。

我的代码就是这个

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

我运行此程序,然后进入&#34; 123&#34;。然后我遇到了名为CryptographicException的异常,它的详细信息是InnerException。

我不知道发生了什么。我不是一个专业的程序员。我只是一个业余爱好者,我是新人。我在中国工作,我使用的是中文版。所以我不知道如何将详细信息翻译成英文。但我想我应该把它贴在这里:

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: 

0 个答案:

没有答案