
前提・実現したいこと
C#でzip圧縮を行ったプロジェクトをaes暗号化したいと考えています。
AesManaged クラスを用いた暗号化関数を作成し、zip圧縮後に呼び出してaes暗号化したいと考えております。
暗号化にあたり参考にさせていただいた記事は主に下記です。
リンク
試したこと
現在扱っているデータがJsonであり、Jsonデータをzip化まではできております。
その後、aes暗号化を記述した関数を呼び出す際に
System.IO.IOException: '別のプロセスで使用されているため、プロセスはファイル 'C:~' にアクセスできません。
と表示され、暗号化ができていない状態です。
(暗号化部分の入力ファイルパスと出力ファイルパスは、SaveFileDialogで保存した際のパスにしています)
他のプロセスが読み書きロックしているようなのですが、現状どの部分が干渉しているかが分かりかねる状態です。
zip化した後のaes暗号化手順に問題がないかご教授いただけますと幸いです。
該当のソースコード
↓暗号化関数
C#
1//aes暗号化 2 /// <summary> 3 /// 共有キー 4 /// </summary> 5 private const string AES_KEY = "-AESKEY-"; 6 7 /// <summary> 8 /// バッファサイズ 9 /// </summary> 10 private const int BUFFER_SIZE = 1024 * 4; 11 12 /// <summary> 13 /// 共有キービットサイズ 14 /// </summary> 15 private const int KEY_SIZE = 128; 16 17 /// <summary> 18 /// 暗号操作のビットサイズ 19 /// </summary> 20 private const int BLOCK_SIZE = 128; 21 22 /// <summary> 23 /// 暗号化 24 /// </summary> 25 /// <param name="src">入力ファイルパス</param> 26 /// <param name="dst">出力ファイルパス</param> 27 static public void Encode(string src, string dst) 28 { 29 //aes暗号化 30 using (AesManaged aes = new AesManaged()) 31 { 32 //AESインスタンスのパラメータ設定 33 aes.KeySize = KEY_SIZE; 34 aes.BlockSize = BLOCK_SIZE; 35 aes.Mode = CipherMode.CBC; 36 aes.Key = Encoding.UTF8.GetBytes(AES_KEY); 37 38 //初期ベクターの生成 39 aes.GenerateIV(); 40 41 //暗号化オブジェクトの生成 42 ICryptoTransform ct = aes.CreateEncryptor(aes.Key, aes.IV); 43 44 //出力ファイルストリーム 45 using (FileStream outFs = new FileStream(dst, FileMode.Create, FileAccess.Write)) 46 { 47 //初期ベクター書き込み 48 outFs.Write(aes.IV, 0, aes.IV.Length); 49 50 //暗号変換のストリーム(暗号化) 51 using (CryptoStream cs = new CryptoStream(outFs, ct , CryptoStreamMode.Write)) 52 { 53 //Deflateアルゴリズムを使用したストリーム(圧縮) 54 using (DeflateStream ds = new DeflateStream(cs, CompressionMode.Compress)) 55 { 56 //入力ファイルストリーム 57 using (FileStream inFs = new FileStream(src, FileMode.Open, FileAccess.Read)) 58 { 59 byte[] buf = new byte[BUFFER_SIZE]; 60 for(int size = inFs.Read(buf, 0, buf.Length); size>0; size = inFs.Read(buf, 0, buf.Length)) 61 { 62 //出力ファイルへの書き込み 63 ds.Write(buf, 0, size); 64 } 65 } 66 } 67 } 68 } 69 } 70 }
↓複合化関数
C#
1/// <summary> 2 /// 複合化 3 /// </summary> 4 /// <param name="src">入力ファイルパス</param> 5 /// <param name="dst">出力ファイルパス</param> 6 static public void Decode(string src, string dst) 7 { 8 // 高度暗号化標準(AES) 9 using (AesManaged aes = new AesManaged()) 10 { 11 // AESインスタンスのパラメータ設定 12 aes.KeySize = KEY_SIZE; 13 aes.BlockSize = BLOCK_SIZE; 14 aes.Mode = CipherMode.CBC; 15 aes.Key = Encoding.UTF8.GetBytes(AES_KEY); 16 17 // 入力ファイルストリーム 18 using (FileStream inFs = new FileStream(src, FileMode.Open, FileAccess.Read)) 19 { 20 // 初期化ベクター(IV)読込 21 byte[] iv = new byte[aes.IV.Length]; 22 inFs.Read(iv, 0, iv.Length); 23 aes.IV = iv; 24 25 // 複合化オブジェクト生成 26 ICryptoTransform ct = aes.CreateDecryptor(aes.Key, aes.IV); 27 28 // 暗号変換のストリーム(複合化) 29 using (CryptoStream cs = new CryptoStream(inFs, ct, CryptoStreamMode.Read)) 30 { 31 // Deflateアルゴリズムを使用したストリーム(圧縮解除) 32 using (DeflateStream ds = new DeflateStream(cs, CompressionMode.Decompress)) 33 { 34 // 出力ファイルストリーム 35 using (FileStream outFs = new FileStream(dst, FileMode.Create, FileAccess.Write)) 36 { 37 // 複合化した結果を書き込む 38 byte[] buf = new byte[BUFFER_SIZE]; 39 for (int size = ds.Read(buf, 0, buf.Length); size > 0; size = ds.Read(buf, 0, buf.Length)) 40 { 41 outFs.Write(buf, 0, size); 42 } 43 } 44 } 45 } 46 } 47 } 48 }
↓zip化、暗号化関数呼び出し
C#
1private void AesSaveAsButton_Click(object sender, RoutedEventArgs e) 2 { 3 var dialog = new SaveFileDialog(); 4 dialog.Filter = "zip|*.zip"; 5 if (dialog.ShowDialog() == true) 6 { 7 var filename = System.IO.Path.GetFileName(dialog.FileName); 8 var entryName = filename.Replace("zip", "json"); 9 10 var json = JsonConvert.SerializeObject(_data, Formatting.Indented); 11 12 // JSON 文字列からエンコーディング UTF-8 でバイト列を作成 13 var bytes = Encoding.UTF8.GetBytes(json); 14 15 using (var ms = new MemoryStream()) 16 { 17 // メモリストリーム上にZipArchiveを作成する 18 using (var zipArchive = new ZipArchive(ms, ZipArchiveMode.Create, true)) 19 { 20 var entry = zipArchive.CreateEntry(entryName); 21 using (var es = entry.Open()) 22 { 23 // エントリにバイナリを書き込む 24 es.Write(bytes, 0, bytes.Length); 25 } 26 } 27 28 // MemoryStream からバイト列を取得してファイルを作成 29 byte[] zipBytes = ms.ToArray(); 30 File.WriteAllBytes(dialog.FileName, zipBytes); 31 32 //暗号化関数の呼び出し 33 Encode(dialog.FileName, C:~(pass)"); 34 } 35 }
↓複合化関数呼び出し
C#
1//AES複合化 2 private void AesOpenAsButton_Click(object sender, RoutedEventArgs e) 3 { 4 var dialog = new OpenFileDialog(); 5 6 if (dialog.ShowDialog().Value) 7 { 8 Decode(dialog.FileName, "C:~(pass)"); 9 var file = dialog.FileName; 10 11 try 12 { 13 var reader = new StreamReader(file); 14 var json = File.ReadAllText(dialog.FileName); 15 reader.Close(); 16 _data = JsonConvert.DeserializeObject<ObservableCollection<ManagementUnitData>>(json); 17 } 18 catch (Exception error) 19 { 20 if (error.Message != null) 21 Debug.WriteLine(error.Message); 22 } 23 24 if (_data != null) 25 { 26 DataContext = _data; 27 } 28 } 29 }
doing
出力先を別のパスに指定した結果、ファイルというものが生成。
こちらがaes暗号化されたものである可能性のため、複合化を行って検証予定。
メモ帳で開く限り、何かしらのデータは入っているようです。
こちらの取り扱い方を捜査中




回答1件
あなたの回答
tips
プレビュー