4

私は .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 ページをざっと見ると、これは最初にある種の文字列変換を行うことが示されていますが、これは望ましくありません。何か案は?

4

1 に答える 1

2

この場合、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
于 2010-05-27T16:23:11.083 に答える