Rijndael 托管:明文长度检测

发布于 2024-09-03 21:50:26 字数 3033 浏览 1 评论 0原文

我花了一些时间学习如何在 .NET 中使用 RijndaelManaged 库,并开发了以下函数来测试加密文本,并对 MSDN 库进行了轻微修改:

Function encryptBytesToBytes_AES(ByVal plainText As Byte(), ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
        ' Check arguments.
        If plainText Is Nothing OrElse plainText.Length <= 0 Then
            Throw New ArgumentNullException("plainText")
        End If
        If Key Is Nothing OrElse Key.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        If IV Is Nothing OrElse IV.Length <= 0 Then
            Throw New ArgumentNullException("IV")
        End If

        ' Declare the RijndaelManaged object
        ' used to encrypt the data.
        Dim aesAlg As RijndaelManaged = Nothing

        ' Declare the stream used to encrypt to an in memory
        ' array of bytes.
        Dim msEncrypt As MemoryStream = Nothing

        Try
            ' Create a RijndaelManaged object
            ' with the specified key and IV.
            aesAlg = New RijndaelManaged()
            aesAlg.BlockSize = 128
            aesAlg.KeySize = 128
            aesAlg.Mode = CipherMode.ECB
            aesAlg.Padding = PaddingMode.None
            aesAlg.Key = Key
            aesAlg.IV = IV

                ' Create a decrytor to perform the stream transform.
                Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)

                ' Create the streams used for encryption.
                msEncrypt = New MemoryStream()
                Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                    Using swEncrypt As New StreamWriter(csEncrypt)

                        'Write all data to the stream.
                        swEncrypt.Write(plainText)
                    End Using
                End Using

        Finally
            ' Clear the RijndaelManaged object.
            If Not (aesAlg Is Nothing) Then
                aesAlg.Clear()
            End If
        End Try
        ' Return the encrypted bytes from the memory stream.
        Return msEncrypt.ToArray()

    End Function

这是我调用 encryptBytesToBytes_AES() 的实际代码:

Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
    Dim bZeroKey As Byte() = {&H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0}
    PrintBytesToRTF(encryptBytesToBytes_AES(bZeroKey, bZeroKey, bZeroKey))
End Sub

但是,我得到了swEncrypt.Write(plainText) 引发异常,指出“要加密的数据长度无效”。

但是,我知道我的密钥、iv 和明文的大小是 16 字节 == 128 位 == aesAlg.BlockSize。 为什么会抛出此异常?是否是因为 StreamWriter 正在尝试创建一个字符串(表面上使用某种编码)并且它不喜欢 &H0 作为值?


编辑:我想我需要想出一种新的方法来加密字节数组,而不是使用 StreamWriter。 MSDN 页面 上的一目了然这将首先进行某种字符串转换,这是我不想要的。有什么想法吗?

I am spending some time learning how to use the RijndaelManaged library in .NET, and developed the following function to test encrypting text with slight modifications from the MSDN library:

Function encryptBytesToBytes_AES(ByVal plainText As Byte(), ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
        ' Check arguments.
        If plainText Is Nothing OrElse plainText.Length <= 0 Then
            Throw New ArgumentNullException("plainText")
        End If
        If Key Is Nothing OrElse Key.Length <= 0 Then
            Throw New ArgumentNullException("Key")
        End If
        If IV Is Nothing OrElse IV.Length <= 0 Then
            Throw New ArgumentNullException("IV")
        End If

        ' Declare the RijndaelManaged object
        ' used to encrypt the data.
        Dim aesAlg As RijndaelManaged = Nothing

        ' Declare the stream used to encrypt to an in memory
        ' array of bytes.
        Dim msEncrypt As MemoryStream = Nothing

        Try
            ' Create a RijndaelManaged object
            ' with the specified key and IV.
            aesAlg = New RijndaelManaged()
            aesAlg.BlockSize = 128
            aesAlg.KeySize = 128
            aesAlg.Mode = CipherMode.ECB
            aesAlg.Padding = PaddingMode.None
            aesAlg.Key = Key
            aesAlg.IV = IV

                ' Create a decrytor to perform the stream transform.
                Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)

                ' Create the streams used for encryption.
                msEncrypt = New MemoryStream()
                Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                    Using swEncrypt As New StreamWriter(csEncrypt)

                        'Write all data to the stream.
                        swEncrypt.Write(plainText)
                    End Using
                End Using

        Finally
            ' Clear the RijndaelManaged object.
            If Not (aesAlg Is Nothing) Then
                aesAlg.Clear()
            End If
        End Try
        ' Return the encrypted bytes from the memory stream.
        Return msEncrypt.ToArray()

    End Function

Here's the actual code I am calling encryptBytesToBytes_AES() with:

Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
    Dim bZeroKey As Byte() = {&H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0, &H0}
    PrintBytesToRTF(encryptBytesToBytes_AES(bZeroKey, bZeroKey, bZeroKey))
End Sub

However, I get an exception thrown on swEncrypt.Write(plainText) stating that the 'Length of the data to encrypt is invalid.'

However, I know that the size of my key, iv, and plaintext are 16 bytes == 128 bits == aesAlg.BlockSize. Why is it throwing this exception? Is it because the StreamWriter is trying to make a String (ostensibly with some encoding) and it doesn't like &H0 as a value?


EDIT: I think I need to come up with a new way to encrypt the byte array, other than using the StreamWriter. A gander on the MSDN page shows that this will do some sort of string conversion first, which I don't want. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放手` 2024-09-10 21:50:26

在这种情况下,您不能也不需要使用 StreamWriter。
StreamWriter 不接受 Byte() 作为参数。

您可以将加密函数修改为以下内容:

Function encryptBytesToBytes_AES(ByVal plainText As Byte(), ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
    ' Check arguments.'
    If plainText Is Nothing OrElse plainText.Length <= 0 Then
        Throw New ArgumentNullException("plainText")
    End If
    If Key Is Nothing OrElse Key.Length <= 0 Then
        Throw New ArgumentNullException("Key")
    End If
    If IV Is Nothing OrElse IV.Length <= 0 Then
        Throw New ArgumentNullException("IV")
    End If

    ' Declare the RijndaelManaged object'
    ' used to encrypt the data.'
    Dim aesAlg As RijndaelManaged = Nothing

    Dim encryptedData As Byte()

    Try
        ' Create a RijndaelManaged object'
        ' with the specified key and IV.'
        aesAlg = New RijndaelManaged()
        aesAlg.BlockSize = 128
        aesAlg.KeySize = 128
        aesAlg.Mode = CipherMode.ECB
        aesAlg.Padding = PaddingMode.None
        aesAlg.Key = Key
        aesAlg.IV = IV

        ' Create a decrytor to perform the stream transform.'
        Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor()

        ' Create the streams used for encryption.'
        Using msEncrypt As New MemoryStream()

            Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                csEncrypt.Write(plainText, 0, plainText.Length)
                csEncrypt.FlushFinalBlock()
            End Using
            encryptedData = msEncrypt.ToArray()
        End Using

    Finally
        ' Clear the RijndaelManaged object.'
        If Not (aesAlg Is Nothing) Then
            aesAlg.Clear()
        End If
    End Try
    ' Return the encrypted bytes from the memory stream.'
    Return encryptedData
End Function

You cannot and don't need to use a StreamWriter in this case.
StreamWriter doesn't accept Byte() as an argument.

You can modify your encryption function to the following:

Function encryptBytesToBytes_AES(ByVal plainText As Byte(), ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
    ' Check arguments.'
    If plainText Is Nothing OrElse plainText.Length <= 0 Then
        Throw New ArgumentNullException("plainText")
    End If
    If Key Is Nothing OrElse Key.Length <= 0 Then
        Throw New ArgumentNullException("Key")
    End If
    If IV Is Nothing OrElse IV.Length <= 0 Then
        Throw New ArgumentNullException("IV")
    End If

    ' Declare the RijndaelManaged object'
    ' used to encrypt the data.'
    Dim aesAlg As RijndaelManaged = Nothing

    Dim encryptedData As Byte()

    Try
        ' Create a RijndaelManaged object'
        ' with the specified key and IV.'
        aesAlg = New RijndaelManaged()
        aesAlg.BlockSize = 128
        aesAlg.KeySize = 128
        aesAlg.Mode = CipherMode.ECB
        aesAlg.Padding = PaddingMode.None
        aesAlg.Key = Key
        aesAlg.IV = IV

        ' Create a decrytor to perform the stream transform.'
        Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor()

        ' Create the streams used for encryption.'
        Using msEncrypt As New MemoryStream()

            Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                csEncrypt.Write(plainText, 0, plainText.Length)
                csEncrypt.FlushFinalBlock()
            End Using
            encryptedData = msEncrypt.ToArray()
        End Using

    Finally
        ' Clear the RijndaelManaged object.'
        If Not (aesAlg Is Nothing) Then
            aesAlg.Clear()
        End If
    End Try
    ' Return the encrypted bytes from the memory stream.'
    Return encryptedData
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文