2012-08-27 7 views
1

Je crypte mon fichier avec l'algorithme Blowfish mais il semble que je n'en sache rien. Chaque fois que j'essaie de Encipher(), une exception "Invalid Length" apparaît. Je me suis dit que la longueur doit être nulle quand il est mod avec 8 et je pense que cela signifie qu'il devrait y avoir 8 par 8 blocs de flux pour commencer à chiffrer. Que devrais-je faire?Blowfish Invalid Length Exception

méthode Chiffrer Blowfish:

public void Encipher(byte[] data, int length) 
{ 
    uint xl, xr; 
    if ((length % 8) != 0) <-- Exception Line 
     throw new Exception("Invalid Length"); 
    for (int i = 0; i < length; i += 8) 
    { 
     // Encode the data in 8 byte blocks. 
     xl = (uint)((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]); 
     xr = (uint)((data[i + 4] << 24) | (data[i + 5] << 16) | (data[i + 6] << 8) | data[i + 7]); 
     Encipher(ref xl, ref xr); 
     // Now Replace the data. 
     data[i] = (byte)(xl >> 24); 
     data[i + 1] = (byte)(xl >> 16); 
     data[i + 2] = (byte)(xl >> 8); 
     data[i + 3] = (byte)(xl); 
     data[i + 4] = (byte)(xr >> 24); 
     data[i + 5] = (byte)(xr >> 16); 
     data[i + 6] = (byte)(xr >> 8); 
     data[i + 7] = (byte)(xr); 
    } 
} 

Ma méthode de cryptage:

private void EncryptFile(string szFilePath, string szInfoFile, string szKey, string szEncryptedFile = "") 
     { 
      // Blowfish 
      Blowfish alg = new Blowfish(Encoding.Unicode.GetBytes(szKey)); 

      // Open file 
      System.IO.FileStream originalStream = System.IO.File.OpenRead(szFilePath); 

      // Store original file length 
      long originalLength = originalStream.Length; 
      System.IO.File.WriteAllText(szInfoFile, originalLength.ToString()); 

      Byte[] buffer = new byte[originalStream.Length + (originalStream.Length % 8)]; 
      originalStream.Read(buffer, 0, buffer.Length); 
      originalStream.Close(); 

      // Encrypt 
      alg.Encipher(buffer, buffer.Length); 

      string szEncFile; 

      if (szEncryptedFile != string.Empty) szEncFile = szEncryptedFile; else szEncFile = szFilePath; 

      System.IO.FileStream stream = new System.IO.FileStream(szEncFile, System.IO.FileMode.Create); 
      stream.Write(buffer, 0, buffer.Length); 
      stream.Close(); 
     } 

Merci.

+5

Veuillez indiquer le code qui provoque ce scénario si vous voulez un bon diagnostic du problème. –

+0

Je suggère au lieu d'essayer d'implémenter votre propre méthode de chiffrement que vous utilisez http://www.bouncycastle.org/csharp/ à la place. –

+0

Je me déplace avec Blowfish donc, merci pour l'offre. – MahanGM

Répondre

3

Si ce que vous essayez de faire est autour d'une valeur à la valeur suivante divisible par 8, alors vous devriez le faire à la place:

Byte[] buffer = new byte[originalStream.Length + (8-(originalStream.Length % 8))]; 
+0

Merci pour la correction. Ça marche. – MahanGM

2

Peter Ritchieanswered il. Cependant, il y a quelques pièces idiomatiques que vous devriez considérer ci-dessous. On enveloppe des classes IDisposable -implémentées (telles que FileStream s) en blocs using afin d'assurer l'élimination des ressources dans le cas de conditions exceptionnelles pendant le traitement. Un autre est le if..then que vous mettez dans une ligne. C'est vraiment ... étrange. Je l'ai remplacé par l'opérateur ternaire, qui semble correspondre à l'usage que vous utilisez. Bonne chance.

private void EncryptFile(string szFilePath, string szInfoFile, string szKey, string szEncryptedFile = "") 
    { 
     // Blowfish 
     Blowfish alg = new Blowfish(Encoding.Unicode.GetBytes(szKey)); 

     // Open file 
     using (System.IO.FileStream originalStream = System.IO.File.OpenRead(szFilePath)) 
     { 
      // Store original file length 
      long originalLength = originalStream.Length; 
      System.IO.File.WriteAllText(szInfoFile, originalLength.ToString()); 

      Byte[] buffer = new byte[originalStream.Length + (originalStream.Length % 8)]; 
      originalStream.Read(buffer, 0, buffer.Length); 
     } 

     // Encrypt 
     alg.Encipher(buffer, buffer.Length); 

     string szEncFile; 

     szEncFile = string.IsNullOrEmpty(szEncryptedFile) ? szFilePath : szEncryptedFile; 

     using (System.IO.FileStream stream = new System.IO.FileStream(szEncFile, System.IO.FileMode.Create)) 
     { 
      stream.Write(buffer, 0, buffer.Length); 
     } 
    } 
+0

Merci. Le bloc 'FileStream's et' if..then' était juste un test de rien de sérieux. – MahanGM