2012-07-04 66 views
0

我不知道我做錯了,但我一直在試圖讓這件事約4小時的工作,現在我只是無法得到它的工作...這只是給了我錯誤:當我嘗試解密時,請提供一個正確的密碼。 雖然加密似乎工作正常。Rijndael加密發出

有什麼建議嗎? :<

using System; 
using System.Text; 
using System.Security.Cryptography; 
using System.IO; 
using System.Security; 
using AesApp.Rijndael; 
using System.Linq; 

    internal class FileEncryption 
     { 
      private static string password = pw; 

      internal static void Encrypt(string inputfile, string outputfile) 
      { 
       byte[] encryptedPassword; 

       // Create a new instance of the RijndaelManaged 
       // class. This generates a new key and initialization 
       // vector (IV). 
       using (var algorithm = new RijndaelManaged()) 
       { 
        algorithm.KeySize = 256; 
        algorithm.BlockSize = 128; 

        // Encrypt the string to an array of bytes. 
        encryptedPassword = Cryptology.EncryptStringToBytes(
         password, algorithm.Key, algorithm.IV); 
       } 

       string chars = encryptedPassword.Aggregate(string.Empty, (current, b) => current + b.ToString()); 
       Cryptology.EncryptFile(@inputfile, @outputfile, chars); 
      } 

      internal static void Decrypt(string @inputfile, string @outputfile) 
      { 
       byte[] encryptedPassword; 

       // Create a new instance of the RijndaelManaged 
       // class. This generates a new key and initialization 
       // vector (IV). 
       using (var algorithm = new RijndaelManaged()) 
       { 
        algorithm.KeySize = 256; 
        algorithm.BlockSize = 128; 

        // Encrypt the string to an array of bytes. 
        encryptedPassword = Cryptology.EncryptStringToBytes(
         password, algorithm.Key, algorithm.IV); 
       } 

       string chars = encryptedPassword.Aggregate(string.Empty, (current, b) => current + b.ToString()); 
       Cryptology.DecryptFile(@inputfile, @outputfile, chars); 
      } 
     } 

Reindael.cs

using System; 
using System.IO; 
using System.Security.Cryptography; 
using System.Text; 

    namespace AesApp.Rijndael 
    { 
     internal sealed class Cryptology 
     { 
      private const string Salt = "d5fg4df5sg4ds5fg45sdfg4"; 
      private const int SizeOfBuffer = 1024 * 8; 

      internal static byte[] EncryptStringToBytes(string plainText, byte[] key, byte[] iv) 
      { 
       // Check arguments. 
       if (plainText == null || plainText.Length <= 0) 
       { 
        throw new ArgumentNullException("plainText"); 
       } 
       if (key == null || key.Length <= 0) 
       { 
        throw new ArgumentNullException("key"); 
       } 
       if (iv == null || iv.Length <= 0) 
       { 
        throw new ArgumentNullException("key"); 
       } 

       byte[] encrypted; 
       // Create an RijndaelManaged object 
       // with the specified key and IV. 
       using (var rijAlg = new RijndaelManaged()) 
       { 
        rijAlg.Key = key; 
        rijAlg.IV = iv; 

        // Create a decrytor to perform the stream transform. 
        ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV); 

        // Create the streams used for encryption. 
        using (var msEncrypt = new MemoryStream()) 
        { 
         using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) 
         { 
          using (var swEncrypt = new StreamWriter(csEncrypt)) 
          { 
           //Write all data to the stream. 
           swEncrypt.Write(plainText); 
          } 
          encrypted = msEncrypt.ToArray(); 
         } 
        } 
       } 


       // Return the encrypted bytes from the memory stream. 
       return encrypted; 

      } 

      internal static string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] iv) 
      { 
       // Check arguments. 
       if (cipherText == null || cipherText.Length <= 0) 
        throw new ArgumentNullException("cipherText"); 
       if (key == null || key.Length <= 0) 
        throw new ArgumentNullException("key"); 
       if (iv == null || iv.Length <= 0) 
        throw new ArgumentNullException("key"); 

       // Declare the string used to hold 
       // the decrypted text. 
       string plaintext; 

       // Create an RijndaelManaged object 
       // with the specified key and IV. 
       using (var rijAlg = new RijndaelManaged()) 
       { 
        rijAlg.Key = key; 
        rijAlg.IV = iv; 

        // Create a decrytor to perform the stream transform. 
        ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV); 

        // Create the streams used for decryption. 
        using (var msDecrypt = new MemoryStream(cipherText)) 
        { 
         using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) 
         { 
          using (var srDecrypt = new StreamReader(csDecrypt)) 
          { 
           // Read the decrypted bytes from the decrypting stream 
           // and place them in a string. 
           plaintext = srDecrypt.ReadToEnd(); 
          } 
         } 
        } 

       } 
       return plaintext; 
      } 

      internal static void EncryptFile(string inputPath, string outputPath, string password) 
      { 
       var input = new FileStream(inputPath, FileMode.Open, FileAccess.Read); 
       var output = new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write); 

       // Essentially, if you want to use RijndaelManaged as AES you need to make sure that: 
       // 1.The block size is set to 128 bits 
       // 2.You are not using CFB mode, or if you are the feedback size is also 128 bits 

       var algorithm = new RijndaelManaged { KeySize = 256, BlockSize = 128 }; 
       var key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(Salt)); 

       algorithm.Key = key.GetBytes(algorithm.KeySize/8); 
       algorithm.IV = key.GetBytes(algorithm.BlockSize/8); 

       using (var encryptedStream = new CryptoStream(output, algorithm.CreateEncryptor(), CryptoStreamMode.Write)) 
       { 
        CopyStream(input, encryptedStream); 
       } 
      } 

      internal static void DecryptFile(string inputPath, string outputPath, string password) 
      { 
       var input = new FileStream(inputPath, FileMode.Open, FileAccess.Read); 
       var output = new FileStream(outputPath, FileMode.OpenOrCreate, FileAccess.Write); 

       // Essentially, if you want to use RijndaelManaged as AES you need to make sure that: 
       // 1.The block size is set to 128 bits 
       // 2.You are not using CFB mode, or if you are the feedback size is also 128 bits 
       var algorithm = new RijndaelManaged { KeySize = 256, BlockSize = 128 }; 
       var key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(Salt)); 

       algorithm.Key = key.GetBytes(algorithm.KeySize/8); 
       algorithm.IV = key.GetBytes(algorithm.BlockSize/8); 

       try 
       { 
        using (var decryptedStream = new CryptoStream(output, algorithm.CreateDecryptor(), CryptoStreamMode.Write)) 
        { 
         CopyStream(input, decryptedStream); 
        } 
       } 
       catch (CryptographicException) 
       { 
        throw new InvalidDataException("Please suppy a correct password"); 
       } 
       catch (Exception ex) 
       { 
        throw new Exception(ex.Message); 
       } 
      } 

      private static void CopyStream(Stream input, Stream output) 
      { 
       using (output) 
       using (input) 
       { 
        byte[] buffer = new byte[SizeOfBuffer]; 
        int read; 
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
        { 
         output.Write(buffer, 0, read); 
        } 
       } 
      } 
     } 
    } 
+0

嗨...只是葉蘭時間:你在哪裏得到一個錯誤?什麼是工作,什麼不是?文件加密?字節加密?這樣的代碼難以閱讀,請縮小您的請求範圍。 – Kek

+0

對不起沒有提到的是,加密似乎工作正常,產生適當的前瞻性的文件,但對解密我得到這個錯誤 – user1071461

+1

你爲什麼前綴'inputfile'和'outputfile'與'@'這樣呢? –

回答

1

不能完全確定,但我想我還記得,當加密2連續調用了2個不同結果的時間。 因此,對EncryptStringToBytes的兩次連續調用可能會給出2個不同的密碼:一個用於加密,另一個用於解密......這會導致失敗。

我不知道這些加密是必要的......如果你有一個硬編碼密碼,總是有可能對任何人產生依賴沒有別的其他字符串。您應該直接使用此密碼,而不是crypting它第一次:

internal static void Encrypt(string inputfile, string outputfile) 
{ 
    Cryptology.EncryptFile(inputfile, outputfile, password); 
} 

internal static void Decrypt(string inputfile, string outputfile) 
{ 
    Cryptology.DecryptFile(inputfile, outputfile, password); 
} 
+0

好吧,它適用於此^^謝謝!對不起,要能理解,我沒有帶睡在年齡和我相當新的C# – user1071461

0
  1. 你的加密功能似乎正好等於你的解密功能。
  2. 此外,你爲什麼將所有字節字符串,將它們連接起來?這種轉變是不可逆轉的。
0
string chars = encryptedPassword.Aggregate(string.Empty, (current, b) => current + b.ToString()); 
      Cryptology.EncryptFile(@inputfile, @outputfile, chars); 

的骨料()函數是原因。每次運行應用程序時都會創建不同的值。