2010-05-27 3 views
3

Je passe du temps à apprendre comment utiliser la bibliothèque RijndaelManaged dans .NET, et a développé la fonction suivante pour tester le texte chiffrer avec de légères modifications de la bibliothèque MSDN:Rijndael géré: longueur plaintext detction

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 

Voilà le code réel que je fais appel encryptBytesToBytes_AES() avec:

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 

Cependant, je reçois une exception levée sur swEncrypt.Write(plainText) indiquant que la « longueur des données à chiffrer est invalide. »

Cependant, je sais que la taille de ma clé, iv et de 16 octets en texte brut == 128 bits == aesAlg.BlockSize. Pourquoi lance-t-il cette exception? Est-ce parce que le StreamWriter tente de faire une chaîne (ostensiblement avec un codage) et il n'aime pas & H0 comme valeur?


EDIT: Je pense que je dois trouver une nouvelle façon de chiffrer le tableau d'octets, autre que l'utilisation du StreamWriter. Un coup d'oeil sur le MSDN page montre que cela va faire une sorte de conversion de chaîne d'abord, ce que je ne veux pas. Des idées?

+2

Peut-être est une objection parce que vous demandez pour le mode de la BCE, mais dont une IV (qui ne sert pas à la BCE)? –

+0

@Jerry - Essayera, comme vous avez raison à propos de la IV. –

+0

@Jerry - Nope, arrive encore, même lorsque je change le code pour ne pas définir le IV. Mais bonne observation. –

Répondre

1

Vous ne pouvez pas et ne pas avoir besoin d'utiliser un StreamWriter dans ce cas. StreamWriter n'accepte pas Byte() comme argument.

Vous pouvez modifier votre fonction de chiffrement à ce qui suit:

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 
+0

Cela a résolu mon problème. Merci beaucoup. –

Questions connexes