2016-11-27 38 views
-2

C#でAESを使用してファイルを暗号化および復号化するコードがあるかどうかを知りたかったのですが、C#でテキストを暗号化および復号化するコードをいくつか見てきましたが、 Cでファイルを暗号化および復号化しました。それをよく理解するための完全なコードはありません。誰かが私を助けてくれたら?テキスト C#のエースでファイルを暗号化して解読する?

  • 暗号
  • をファイルに保存し、テキストとしてファイルの内容は
  • は、あなたがする必要がある

    • 負荷:

    +0

    [C#でAES暗号化を使用する](http://stackoverflow.com/questions/273452/using-aes-encryption-in-c-sharp) – devRicher

    +0

    の可能な複製@devRicherリンクされた質問は質問に答えませんが、 OPはsp구체的に**ファイル**の暗号化を探しています。 – zaph

    答えて

    -3

    あなたは、暗号化のために次の操作を実行する必要があります解読のために以下を実行する:

    • loa Dテキスト
    • としてファイルの内容は、一般的にはファイル
    +0

    はい、ファイルの内容をテキストとして読み込まずに暗号化する方法はありますか? – stones

    +0

    ファイルへのストリーミングインターフェイスを使用します。 – zaph

    0

    にテキスト保存

  • を復号化するには、ファイルを暗号化する必要はありません。つまり、ファイルを書き込んだ後で暗号化する必要はありません。データはおそらくストレージデバイスの異なるセクタにあり、おそらく回復する可能性があります。 (もちろん、あなたがransomwareを書くことを試みているなら、それは貧弱な言い方で書いてください)。あなたが代わりにやりたいのは、ディスクにする前にコンテンツを暗号化することです。あなたが本当にその代わりFileStreamを経由して、元のファイルの書き込みの

    を欲しい

    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); 
    } 
    

    を求め何

    、ファイルを開いて、ヘッダおよびIVを書き、CryptoStreamを作成し、使用しますすべてのCryptoStream。暗号化されていないフォームをディスク上に置く必要はありません。

  • 関連する問題