private void Form1_DragDrop(object s, DragEventArgs e) { string[] fileList = (string[])e.Data.GetData(DataFormats.FileDrop); // 複数ファイルまたはフォルダのドロップは無視 if (fileList.Length != 1) { return; } bool encrypted = false; // 暗号化されたファイルかどうかチェック if (!Directory.Exists(fileList[0])) { using (FileStream outfs = new FileStream(fileList[0], FileMode.Open)) { var locker = new AttacheCase.Unlocker(); encrypted = (locker.Open(outfs) != AttacheCase.Result.ERR_UNENCRYPTED_FILE); } } if (encrypted) { Decrypt(fileList[0]); } else { Encrypt(fileList[0]); } }
private void Unencrypted_File_Test(String filename) { string key = "cosmos"; using (FileStream infs = new FileStream("../../../../test/" + filename, FileMode.Open)) { AttacheCase.Unlocker unlocker = new AttacheCase.Unlocker(); Assert.AreEqual(unlocker.Open(infs, key), AttacheCase.Result.ERR_UNENCRYPTED_FILE); } }
private void Decryption_Test(String filename) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); string key = "cosmos"; if (test_md5.Length == 0) { using (FileStream infs = new FileStream("../../../../test/cosmos.jpg", FileMode.Open)) { byte[] buffer = new byte[infs.Length]; infs.Read(buffer, 0, buffer.Length); md5.ComputeHash(buffer); test_md5 = md5.Hash; md5.Initialize(); } } using (FileStream infs = new FileStream("../../../../test/" + filename, FileMode.Open)) { AttacheCase.Unlocker unlocker = new AttacheCase.Unlocker(); Assert.AreEqual(unlocker.Open(infs, key), AttacheCase.Result.OK); Assert.AreEqual(unlocker.Entries.Length, 1); AttacheCase.FileEntry entry = unlocker.Entries[0]; MemoryStream extracted = new MemoryStream(); Assert.AreEqual(unlocker.ExtractFileData(extracted, infs, (uint)entry.Size), AttacheCase.Result.OK); byte[] buffer = new byte[entry.Size]; extracted.Position = 0; extracted.Read(buffer, 0, buffer.Length); md5.ComputeHash(buffer); Assert.IsTrue(test_md5.SequenceEqual(md5.Hash)); } }
private void Decrypt(string fileName) { // パスワードの入力を要求 PasswordInput form = new PasswordInput(); form.ShowDialog(); string key = form.GetPassword(); if (key.Length > 0) { using (FileStream infs = new FileStream(fileName, FileMode.Open)) { var unlocker = new AttacheCase.Unlocker(); // 指定したキーで復号を試みる AttacheCase.Result result = unlocker.Open(infs, key); if (result == AttacheCase.Result.OK) { // *** ヘッダの復号に成功 foreach (AttacheCase.FileEntry entry in unlocker.Entries) { string dirPath = Path.GetDirectoryName(fileName); string outName = dirPath + "\\" + entry.NameSJIS; if (entry.Size < 0) { // *** ディレクトリ // 無かったら作る if (!Directory.Exists(outName)) { Directory.CreateDirectory(outName); } // 作成日時、更新日時を設定 Directory.SetCreationTime(outName, entry.CreateDateTime); Directory.SetLastWriteTime(outName, entry.ChangeDateTime); } else { // *** ファイル using (FileStream outfs = new FileStream(outName, FileMode.Create)) { unlocker.ExtractFileData(outfs, infs, entry.Size); } // 作成日時、更新日時を設定 File.SetCreationTime(outName, entry.CreateDateTime); File.SetLastWriteTime(outName, entry.ChangeDateTime); } } MessageBox.Show("Decryption completed"); } else if (result == AttacheCase.Result.ERR_WRONG_KEY) { // *** パスワードが違う MessageBox.Show("Wrong password"); } else { // *** 読み込み失敗 MessageBox.Show("Failed"); } } } }
public void SelfEncryptionAndDecryption() { string key = "This is a pen."; String atc_filename = "test_.atc"; String test_data_str = "The quick brown fox jumps over the lazy dog"; String test_data2_str = "Quo usque tandem abutere, Catilina, patientia nostra?"; MemoryStream test_data = new MemoryStream(Encoding.ASCII.GetBytes(test_data_str)); MemoryStream test_data2 = new MemoryStream(Encoding.ASCII.GetBytes(test_data2_str)); // UNIX時間の精度に合わせるため切り捨てる DateTime time_stamp = (new DateTime()).AddSeconds(DateTime.Now.Ticks / 10000000); using (FileStream outfs = new FileStream(atc_filename, FileMode.Create)) { var locker = new AttacheCase.Locker(); locker.PasswdTryLimit = 5; locker.SelfDestruction = true; Assert.AreEqual(locker.Open(outfs, key), AttacheCase.Result.OK); { var entry = new AttacheCase.FileEntry(); entry.Attribute = 16; entry.Size = -1; entry.NameSJIS = "out\\"; entry.NameUTF8 = "out\\"; entry.ChangeDateTime = time_stamp; entry.CreateDateTime = time_stamp; Assert.AreEqual(locker.AddFileEntry(entry), AttacheCase.Result.OK); } { var entry = new AttacheCase.FileEntry(); entry.Attribute = 0; entry.Size = test_data.Length; entry.NameSJIS = "out\\test.txt"; entry.NameUTF8 = "out\\test.txt"; entry.ChangeDateTime = time_stamp; entry.CreateDateTime = time_stamp; Assert.AreEqual(locker.AddFileEntry(entry), AttacheCase.Result.OK); } { var entry = new AttacheCase.FileEntry(); entry.Attribute = 0; entry.Size = test_data2.Length; entry.NameSJIS = "out\\test2.txt"; entry.NameUTF8 = "out\\test2.txt"; entry.ChangeDateTime = time_stamp; entry.CreateDateTime = time_stamp; Assert.AreEqual(locker.AddFileEntry(entry), AttacheCase.Result.OK); } Assert.AreEqual(locker.WriteEncryptedHeader(outfs), AttacheCase.Result.OK); Assert.AreEqual(locker.WriteFileData(outfs, test_data, (uint)test_data.Length), AttacheCase.Result.OK); Assert.AreEqual(locker.WriteFileData(outfs, test_data2, (uint)test_data2.Length), AttacheCase.Result.OK); Assert.AreEqual(locker.Close(), AttacheCase.Result.OK); } using (FileStream infs = new FileStream(atc_filename, FileMode.Open)) { var unlocker = new AttacheCase.Unlocker(); Assert.AreEqual(unlocker.Open(infs, key), AttacheCase.Result.OK); Assert.AreEqual(unlocker.PasswdTryLimit, 5); Assert.IsTrue(unlocker.SelfDestruction); Assert.AreEqual(unlocker.Entries.Length, 3); { var entry = unlocker.Entries[1]; Assert.AreEqual(entry.ChangeDateTime, time_stamp); Assert.AreEqual(entry.CreateDateTime, time_stamp); MemoryStream buf = new MemoryStream(); Assert.AreEqual(unlocker.ExtractFileData(buf, infs, (uint)entry.Size), AttacheCase.Result.OK); buf.Position = 0; Assert.AreEqual((new StreamReader(buf)).ReadToEnd(), test_data_str); } { var entry = unlocker.Entries[2]; Assert.AreEqual(entry.ChangeDateTime, time_stamp); Assert.AreEqual(entry.CreateDateTime, time_stamp); MemoryStream buf = new MemoryStream(); Assert.AreEqual(unlocker.ExtractFileData(buf, infs, (uint)entry.Size), AttacheCase.Result.OK); buf.Position = 0; Assert.AreEqual((new StreamReader(buf)).ReadToEnd(), test_data2_str); } } }