2015-10-19 77 views
1

下面是我用來測試文件加密和解密的簡單片段。發生了什麼是加密工作正常 - 但是當我解密文件是空白的,我不完全確定爲什麼。加密和解密文件導致解密時出現空白文件

我也越來越填充是無效的,不能被刪除的問題,但我可以解決這與Padding.None,但我不明白爲什麼解密文件時爲null。我正在使用一個簡單的文本文件來測試。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Security.Cryptography; 
using System.IO; 

namespace EncryptionTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var Mode = ""; 
      bool Loop = true; 
      while (Loop) 
      { 
       Console.WriteLine("Encrypt or Decrypt? (E/D)"); 
       Mode = Console.ReadLine(); 
       if (Mode.ToUpper() == "E" || Mode.ToUpper() == "D") 
        switch (Mode.ToUpper()) 
        { 
         case "E": 
          Mode = "Encrypt"; 
          Loop = false; 
          continue; 
         case "D": 
          Mode = "Decrpt"; 
          Loop = false; 
          continue; 

        } 
       else 
        Console.WriteLine("Must be E OR D"); 

      } 
      Console.WriteLine("What directory do you want to encrypt?"); 

      var fileDirectory = Console.ReadLine(); 

      Console.WriteLine("What password do you want to use?"); 
      var passWord = Console.ReadLine(); 

      string[] files = Directory.GetFiles(fileDirectory); 
      for (int i = 0; i < files.Length; i++) 
      { 
       byte[] bytesToBeEncrypted = File.ReadAllBytes(files[i]); 
       byte[] passwordBytes = Encoding.UTF8.GetBytes(passWord); 

       // Hash the password with SHA256 
       passwordBytes = SHA256.Create().ComputeHash(passwordBytes); 
       byte[] bytesCyrpt = new byte[0]; 
       if (Mode == "Encrypt") 
       { 
        bytesCyrpt = AES_Encrypt(bytesCyrpt, passwordBytes); 
        File.WriteAllBytes(encryptedFileName(files[i]), bytesCyrpt); 
        File.Delete(files[i]); 
       } 
       else 
       { 
        bytesCyrpt = AES_Decrypt(bytesCyrpt, passwordBytes); 
        File.WriteAllBytes(decryptFileName(files[i]), bytesCyrpt); 
       } 




      } 
     } 

     public static byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes) 
     { 
      byte[] encryptedBytes = null; 

      // Set your salt here, change it to meet your flavor: 
      // The salt bytes must be at least 8 bytes. 
      byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; 

      using (MemoryStream ms = new MemoryStream()) 
      { 
       using (RijndaelManaged AES = new RijndaelManaged()) 
       { 
        AES.KeySize = 256; 
        AES.BlockSize = 128; 

        var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000); 
        AES.Key = key.GetBytes(AES.KeySize/8); 
        AES.IV = key.GetBytes(AES.BlockSize/8); 

        AES.Mode = CipherMode.CBC; 

        using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write)) 
        { 
         cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length); 
         cs.Close(); 
        } 
        encryptedBytes = ms.ToArray(); 
       } 
      } 

      return encryptedBytes; 
     } 
     public static byte[] AES_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes) 
     { 
      byte[] decryptedBytes = null; 

      // Set your salt here, change it to meet your flavor: 
      // The salt bytes must be at least 8 bytes. 
      byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; 

      using (MemoryStream ms = new MemoryStream()) 
      { 
       using (RijndaelManaged AES = new RijndaelManaged()) 
       { 
        AES.KeySize = 256; 
        AES.BlockSize = 128; 

        var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000); 
        AES.Key = key.GetBytes(AES.KeySize/8); 
        AES.IV = key.GetBytes(AES.BlockSize/8); 

        AES.Mode = CipherMode.CBC; 

        using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write)) 
        { 
         cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length); 
         cs.Close(); 
        } 
        decryptedBytes = ms.ToArray(); 
       } 
      } 
      return decryptedBytes; 
     } 

     private static string encryptedFileName(string fileName) 
     { 
      string new_file_name = string.Empty; 
      string extension = fileName.Split('.').Last(); 
      new_file_name = fileName.Replace(extension, ""); 
      new_file_name = new_file_name + "_encrypt." + extension; 



      return new_file_name; 


     } 
     private static string decryptFileName(string fileName) 
     { 
      string new_file_name = fileName; 
      new_file_name = new_file_name.Replace("_encrypt", ""); 
      return new_file_name; 
     } 
    } 
} 

回答

1

您沒有將任何字節傳遞到您的方法中。你需要傳遞bytesToBeEncrypted數組。你正在傳遞其中有0字節的bytesCrypt數組。

byte[] bytesToBeEncrypted = File.ReadAllBytes(files[i]); 
byte[] passwordBytes = Encoding.UTF8.GetBytes(passWord); 

// Hash the password with SHA256 
passwordBytes = SHA256.Create().ComputeHash(passwordBytes); 
byte[] bytesCyrpt = new byte[0]; 
if (Mode == "Encrypt") 
{ 
    bytesCyrpt = AES_Encrypt(bytesToBeEncrypted, passwordBytes); 
    File.WriteAllBytes(encryptedFileName(files[i]), bytesCyrpt); 
    File.Delete(files[i]); 
} 
else 
{ 
    bytesCyrpt = AES_Decrypt(bytesToBeEncrypted, passwordBytes); 
    File.WriteAllBytes(decryptFileName(files[i]), bytesCyrpt); 
} 
+0

哇 - 我覺得好笑。 – JL1

+2

不用擔心的人,它發生在我們最好的。看到它的最難的地方是在你面前的時候;) – bhmahler