none
此实现不支持输出反馈模式(OFB),如何解决? RRS feed

  • 问题

  • Dim des As New System.Security.Cryptography.DESCryptoServiceProvider

    des.IV=...

    des.Key=...

    des.Mode = Security.Cryptography.CipherMode.OFB

    Dim mem_stream As New System.IO.MemoryStream

    Dim crypt_stream As New System.Security.Cryptography.CryptoStream(mem_stream, des.CreateEncryptor, Security.Cryptography.CryptoStreamMode.Write)      '运行到这里的时候提示:此实现不支持输出反馈模式(OFB),MSDN上的例子是这么写的,但是Security.Cryptography.CipherMode.CBC、Security.Cryptography.CipherMode.CFB等其他的模式能正常运行,求教到底是怎么回事

    crypt_stream.Write(...)

    crypt_stream.FlushFinalBlock()

    • 已移动 Caillen 2014年1月6日 8:37
    2014年1月4日 7:45

答案

  • 请看这里:http://msdn.microsoft.com/zh-cn/library/0dh224hh(v=vs.110).aspx

    注意对异常的描述,表明 DESCryptoServiceProvider 不支持 OFB。

    2014年1月6日 8:55
  •  Dim crypt_stream As New System.Security.Cryptography.CryptoStream(mem_stream, des.CreateEncryptor, Security.Cryptography.CryptoStreamMode.Write)      '运行到这里的时候提示:此实现不支持输出反馈模式(OFB),MSDN上的例子是这么写的,但是Security.Cryptography.CipherMode.CBC、Security.Cryptography.CipherMode.CFB等其他的模式能正常运行

    据我所知该模式与 CFB 模式基本相同,只不过本次输入是上次迭代中尚未与明文异或时的输出 与 CFB 模式一样, OFB模式也可以作为流加密模式使用,除此之外,每次迭代的输入不是上次迭代的密文。

    你可以把MSDN上的例子的链接地址贴出来,好让我们对照着查看一下。 : )


    remember make the reply as answer and vote the reply as helpful if it helps.


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

    Module DESCSPSample

        Sub Main()
            Try
                ' Create a new DESCryptoServiceProvider object
                ' to generate a key and initialization vector (IV).
                Dim DESalg As New DESCryptoServiceProvider

                ' Create a string to encrypt.
                Dim sData As String = "Here is some data to encrypt."

                ' Encrypt the string to an in-memory buffer.
                Dim Data As Byte() = EncryptTextToMemory(sData, DESalg.Key, DESalg.IV)

                ' Decrypt the buffer back to a string.
                Dim Final As String = DecryptTextFromMemory(Data, DESalg.Key, DESalg.IV)

                ' Display the decrypted string to the console.
                Console.WriteLine(Final)
            Catch e As Exception
                Console.WriteLine(e.Message)
            End Try
        End Sub

        Function EncryptTextToMemory(ByVal Data As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
            Try
                ' Create a MemoryStream.
                Dim mStream As New MemoryStream

                ' Create a CryptoStream using the MemoryStream
                ' and the passed key and initialization vector (IV).
                Dim cStream As New CryptoStream(mStream, _ New DESCryptoServiceProvider().CreateEncryptor(Key, IV), _
                                                CryptoStreamMode.Write)

                ' Convert the passed string to a byte array.
                Dim toEncrypt As Byte() = New ASCIIEncoding().GetBytes(Data)

                ' Write the byte array to the crypto stream and flush it.
                cStream.Write(toEncrypt, 0, toEncrypt.Length)
                cStream.FlushFinalBlock()

                ' Get an array of bytes from the
                ' MemoryStream that holds the
                ' encrypted data.
                Dim ret As Byte() = mStream.ToArray()

                ' Close the streams.
                cStream.Close()
                mStream.Close()

                ' Return the encrypted buffer.
                Return ret
            Catch e As CryptographicException
                Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
                Return Nothing
            End Try
        End Function

        Function DecryptTextFromMemory(ByVal Data() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
            Try
                ' Create a new MemoryStream using the passed
                ' array of encrypted data.
                Dim msDecrypt As New MemoryStream(Data)

                ' Create a CryptoStream using the MemoryStream
                ' and the passed key and initialization vector (IV).
                Dim csDecrypt As New CryptoStream(msDecrypt, _
                                                  New DESCryptoServiceProvider().CreateDecryptor(Key, IV), _ CryptoStreamMode.Read) ' Create buffer to hold the decrypted data.
                Dim fromEncrypt(Data.Length) As Byte

                ' Read the decrypted data out of the crypto stream
                ' and place it into the temporary buffer.
                csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)

                'Convert the buffer into a string and return it.
                Return New ASCIIEncoding().GetString(fromEncrypt)
            Catch e As CryptographicException
                Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
                Return Nothing
            End Try
        End Function


    End Module

    谢谢你的分享~

    对照着DroidXgnaW的建议,上面的异常信息中指出了不支持OFB……

    你可以换一种加密方式这样更安全:

    AesCryptoServiceProvider.CreateDecryptor 方法 (Byte[], Byte[]) (System.Security.Cryptography)

     


    remember make the reply as answer and vote the reply as helpful if it helps.


    • 已标记为答案 ASR-06 2014年1月7日 3:36
    • 已编辑 Risa- 2014年1月7日 3:38
    2014年1月7日 3:20

全部回复

  • 你好:

    我把你的问题移到Visual Basic论坛,也许可以获得更多帮助。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年1月6日 8:38
  •  Dim crypt_stream As New System.Security.Cryptography.CryptoStream(mem_stream, des.CreateEncryptor, Security.Cryptography.CryptoStreamMode.Write)      '运行到这里的时候提示:此实现不支持输出反馈模式(OFB),MSDN上的例子是这么写的,但是Security.Cryptography.CipherMode.CBC、Security.Cryptography.CipherMode.CFB等其他的模式能正常运行

    据我所知该模式与 CFB 模式基本相同,只不过本次输入是上次迭代中尚未与明文异或时的输出 与 CFB 模式一样, OFB模式也可以作为流加密模式使用,除此之外,每次迭代的输入不是上次迭代的密文。

    你可以把MSDN上的例子的链接地址贴出来,好让我们对照着查看一下。 : )


    remember make the reply as answer and vote the reply as helpful if it helps.


    • 已编辑 Risa- 2014年1月6日 8:57
    2014年1月6日 8:47
  • 请看这里:http://msdn.microsoft.com/zh-cn/library/0dh224hh(v=vs.110).aspx

    注意对异常的描述,表明 DESCryptoServiceProvider 不支持 OFB。

    2014年1月6日 8:55
  •  Dim crypt_stream As New System.Security.Cryptography.CryptoStream(mem_stream, des.CreateEncryptor, Security.Cryptography.CryptoStreamMode.Write)      '运行到这里的时候提示:此实现不支持输出反馈模式(OFB),MSDN上的例子是这么写的,但是Security.Cryptography.CipherMode.CBC、Security.Cryptography.CipherMode.CFB等其他的模式能正常运行

    据我所知该模式与 CFB 模式基本相同,只不过本次输入是上次迭代中尚未与明文异或时的输出 与 CFB 模式一样, OFB模式也可以作为流加密模式使用,除此之外,每次迭代的输入不是上次迭代的密文。

    你可以把MSDN上的例子的链接地址贴出来,好让我们对照着查看一下。 : )


    remember make the reply as answer and vote the reply as helpful if it helps.


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

    Module DESCSPSample

        Sub Main()
            Try
                ' Create a new DESCryptoServiceProvider object
                ' to generate a key and initialization vector (IV).
                Dim DESalg As New DESCryptoServiceProvider

                ' Create a string to encrypt.
                Dim sData As String = "Here is some data to encrypt."

                ' Encrypt the string to an in-memory buffer.
                Dim Data As Byte() = EncryptTextToMemory(sData, DESalg.Key, DESalg.IV)

                ' Decrypt the buffer back to a string.
                Dim Final As String = DecryptTextFromMemory(Data, DESalg.Key, DESalg.IV)

                ' Display the decrypted string to the console.
                Console.WriteLine(Final)
            Catch e As Exception
                Console.WriteLine(e.Message)
            End Try
        End Sub

        Function EncryptTextToMemory(ByVal Data As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
            Try
                ' Create a MemoryStream.
                Dim mStream As New MemoryStream

                ' Create a CryptoStream using the MemoryStream
                ' and the passed key and initialization vector (IV).
                Dim cStream As New CryptoStream(mStream, _ New DESCryptoServiceProvider().CreateEncryptor(Key, IV), _
                                                CryptoStreamMode.Write)

                ' Convert the passed string to a byte array.
                Dim toEncrypt As Byte() = New ASCIIEncoding().GetBytes(Data)

                ' Write the byte array to the crypto stream and flush it.
                cStream.Write(toEncrypt, 0, toEncrypt.Length)
                cStream.FlushFinalBlock()

                ' Get an array of bytes from the
                ' MemoryStream that holds the
                ' encrypted data.
                Dim ret As Byte() = mStream.ToArray()

                ' Close the streams.
                cStream.Close()
                mStream.Close()

                ' Return the encrypted buffer.
                Return ret
            Catch e As CryptographicException
                Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
                Return Nothing
            End Try
        End Function

        Function DecryptTextFromMemory(ByVal Data() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
            Try
                ' Create a new MemoryStream using the passed
                ' array of encrypted data.
                Dim msDecrypt As New MemoryStream(Data)

                ' Create a CryptoStream using the MemoryStream
                ' and the passed key and initialization vector (IV).
                Dim csDecrypt As New CryptoStream(msDecrypt, _
                                                  New DESCryptoServiceProvider().CreateDecryptor(Key, IV), _ CryptoStreamMode.Read) ' Create buffer to hold the decrypted data.
                Dim fromEncrypt(Data.Length) As Byte

                ' Read the decrypted data out of the crypto stream
                ' and place it into the temporary buffer.
                csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)

                'Convert the buffer into a string and return it.
                Return New ASCIIEncoding().GetString(fromEncrypt)
            Catch e As CryptographicException
                Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
                Return Nothing
            End Try
        End Function


    End Module

    2014年1月7日 3:08
  •  Dim crypt_stream As New System.Security.Cryptography.CryptoStream(mem_stream, des.CreateEncryptor, Security.Cryptography.CryptoStreamMode.Write)      '运行到这里的时候提示:此实现不支持输出反馈模式(OFB),MSDN上的例子是这么写的,但是Security.Cryptography.CipherMode.CBC、Security.Cryptography.CipherMode.CFB等其他的模式能正常运行

    据我所知该模式与 CFB 模式基本相同,只不过本次输入是上次迭代中尚未与明文异或时的输出 与 CFB 模式一样, OFB模式也可以作为流加密模式使用,除此之外,每次迭代的输入不是上次迭代的密文。

    你可以把MSDN上的例子的链接地址贴出来,好让我们对照着查看一下。 : )


    remember make the reply as answer and vote the reply as helpful if it helps.


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

    Module DESCSPSample

        Sub Main()
            Try
                ' Create a new DESCryptoServiceProvider object
                ' to generate a key and initialization vector (IV).
                Dim DESalg As New DESCryptoServiceProvider

                ' Create a string to encrypt.
                Dim sData As String = "Here is some data to encrypt."

                ' Encrypt the string to an in-memory buffer.
                Dim Data As Byte() = EncryptTextToMemory(sData, DESalg.Key, DESalg.IV)

                ' Decrypt the buffer back to a string.
                Dim Final As String = DecryptTextFromMemory(Data, DESalg.Key, DESalg.IV)

                ' Display the decrypted string to the console.
                Console.WriteLine(Final)
            Catch e As Exception
                Console.WriteLine(e.Message)
            End Try
        End Sub

        Function EncryptTextToMemory(ByVal Data As String, ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
            Try
                ' Create a MemoryStream.
                Dim mStream As New MemoryStream

                ' Create a CryptoStream using the MemoryStream
                ' and the passed key and initialization vector (IV).
                Dim cStream As New CryptoStream(mStream, _ New DESCryptoServiceProvider().CreateEncryptor(Key, IV), _
                                                CryptoStreamMode.Write)

                ' Convert the passed string to a byte array.
                Dim toEncrypt As Byte() = New ASCIIEncoding().GetBytes(Data)

                ' Write the byte array to the crypto stream and flush it.
                cStream.Write(toEncrypt, 0, toEncrypt.Length)
                cStream.FlushFinalBlock()

                ' Get an array of bytes from the
                ' MemoryStream that holds the
                ' encrypted data.
                Dim ret As Byte() = mStream.ToArray()

                ' Close the streams.
                cStream.Close()
                mStream.Close()

                ' Return the encrypted buffer.
                Return ret
            Catch e As CryptographicException
                Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
                Return Nothing
            End Try
        End Function

        Function DecryptTextFromMemory(ByVal Data() As Byte, ByVal Key() As Byte, ByVal IV() As Byte) As String
            Try
                ' Create a new MemoryStream using the passed
                ' array of encrypted data.
                Dim msDecrypt As New MemoryStream(Data)

                ' Create a CryptoStream using the MemoryStream
                ' and the passed key and initialization vector (IV).
                Dim csDecrypt As New CryptoStream(msDecrypt, _
                                                  New DESCryptoServiceProvider().CreateDecryptor(Key, IV), _ CryptoStreamMode.Read) ' Create buffer to hold the decrypted data.
                Dim fromEncrypt(Data.Length) As Byte

                ' Read the decrypted data out of the crypto stream
                ' and place it into the temporary buffer.
                csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)

                'Convert the buffer into a string and return it.
                Return New ASCIIEncoding().GetString(fromEncrypt)
            Catch e As CryptographicException
                Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
                Return Nothing
            End Try
        End Function


    End Module

    谢谢你的分享~

    对照着DroidXgnaW的建议,上面的异常信息中指出了不支持OFB……

    你可以换一种加密方式这样更安全:

    AesCryptoServiceProvider.CreateDecryptor 方法 (Byte[], Byte[]) (System.Security.Cryptography)

     


    remember make the reply as answer and vote the reply as helpful if it helps.


    • 已标记为答案 ASR-06 2014年1月7日 3:36
    • 已编辑 Risa- 2014年1月7日 3:38
    2014年1月7日 3:20