2016-11-27 189 views
-2

我想知道是否有代碼在C#中使用AES加密和解密文件?我已經看到一些關於在c#中使用aes加密和解密文本的代碼,但是在c#中加密和解密文件時使用了 ..沒有完整的代碼來理解它..如果有人可以幫助我嗎?在c#中用aes加密和解密文件?

+0

可能的重複[在C#中使用AES加密](http://stackoverflow.com/questions/273452/using-aes-encryption-in-c-sharp) – devRicher

+0

@devRicher鏈接的問題沒有回答這個問題, OP實際上正在尋找**文件**加密。 – zaph

回答

-3

你需要做的加密如下:

  • 負載爲一個文本
  • 加密文本
  • 保存到一個文件中的文件的內容

你需要做的以下爲解密:

  • loa d爲文本
  • 文件的內容解密文本
  • 保存到一個文件
+0

是的,但是如何在不加載文件內容的情況下進行加密? – stones

+0

對文件使用流式接口。 – zaph

0

一般情況下,你不想加密文件。也就是說,你不想寫一個文件,然後加密它。數據可能位於存儲設備的不同部門,並​​且很可能會被恢復。 (當然,如果你想編寫勒索軟件,通過一切手段寫它不好)。你想要做的是在內容加載到磁盤之前加密內容。

你問什麼

public static void EncryptFile(string filePath, byte[] key) 
{ 
    string tempFileName = Path.GetTempFileName(); 

    using (SymmetricAlgorithm cipher = Aes.Create()) 
    using (FileStream fileStream = File.OpenRead(filePath)) 
    using (FileStream tempFile = File.Create(tempFileName)) 
    { 
     cipher.Key = key; 
     // aes.IV will be automatically populated with a secure random value 
     byte[] iv = cipher.IV; 

     // Write a marker header so we can identify how to read this file in the future 
     tempFile.WriteByte(69); 
     tempFile.WriteByte(74); 
     tempFile.WriteByte(66); 
     tempFile.WriteByte(65); 
     tempFile.WriteByte(69); 
     tempFile.WriteByte(83); 

     tempFile.Write(iv, 0, iv.Length); 

     using (var cryptoStream = 
      new CryptoStream(tempFile, cipher.CreateEncryptor(), CryptoStreamMode.Write)) 
     { 
      fileStream.CopyTo(cryptoStream); 
     } 
    } 

    File.Delete(filePath); 
    File.Move(tempFileName, filePath); 
} 

public static void DecryptFile(string filePath, byte[] key) 
{ 
    string tempFileName = Path.GetTempFileName(); 

    using (SymmetricAlgorithm cipher = Aes.Create()) 
    using (FileStream fileStream = File.OpenRead(filePath)) 
    using (FileStream tempFile = File.Create(tempFileName)) 
    { 
     cipher.Key = key; 
     byte[] iv = new byte[cipher.BlockSize/8]; 
     byte[] headerBytes = new byte[6]; 
     int remain = headerBytes.Length; 

     while (remain != 0) 
     { 
      int read = fileStream.Read(headerBytes, headerBytes.Length - remain, remain); 

      if (read == 0) 
      { 
       throw new EndOfStreamException(); 
      } 

      remain -= read; 
     } 

     if (headerBytes[0] != 69 || 
      headerBytes[1] != 74 || 
      headerBytes[2] != 66 || 
      headerBytes[3] != 65 || 
      headerBytes[4] != 69 || 
      headerBytes[5] != 83) 
     { 
      throw new InvalidOperationException(); 
     } 

     remain = iv.Length; 

     while (remain != 0) 
     { 
      int read = fileStream.Read(iv, iv.Length - remain, remain); 

      if (read == 0) 
      { 
       throw new EndOfStreamException(); 
      } 

      remain -= read; 
     } 

     cipher.IV = iv; 

     using (var cryptoStream = 
      new CryptoStream(tempFile, cipher.CreateDecryptor(), CryptoStreamMode.Write)) 
     { 
      fileStream.CopyTo(cryptoStream); 
     } 
    } 

    File.Delete(filePath); 
    File.Move(tempFileName, filePath); 
} 

你真正想要的是什麼

而是通過一個FileStream寫原始文件,打開文件,寫入頭和IV,創建CryptoStream的,並使用CryptoStream的一切。沒有理由永遠讓未加密的表單在磁盤上。