public void HelixFileDecryptor_UnflushedEncryptThrowsException() { try { using (MemoryStream stream = new MemoryStream()) { FileEntry headerSaved; using (HelixFileEncryptor encryptor = new HelixFileEncryptor(stream, DerivedBytesProvider.FromPassword("password"), HelixFileVersion.UnitTest)) { headerSaved = new FileEntry(); headerSaved.FileName = "testfile.txt"; encryptor.WriteHeader(headerSaved); encryptor.WriteContent("example content"); Assert.IsTrue(stream.Length > 10); using (var readStream = new MemoryStream(stream.ToArray(), false)) using (HelixFileDecryptor decryptor = new HelixFileDecryptor(readStream)) { decryptor.Initialize(DerivedBytesProvider.FromPassword("password")); var header2 = decryptor.ReadHeader(); Assert.AreEqual(headerSaved.FileName, header2.FileName); var content = decryptor.GetContentString(); Assert.AreEqual("example content", content); } } } Assert.IsTrue(false, "Expected a HelixException to be raised"); } catch (HelixException) { } }
public void HelixFileDecryptor_WrongPassword() { using (MemoryStream stream = new MemoryStream()) using (HelixFileEncryptor encryptor = new HelixFileEncryptor(stream, DerivedBytesProvider.FromPassword("password"), HelixFileVersion.UnitTest)) { var header = new FileEntry(); header.FileName = "testfile.txt"; encryptor.WriteHeader(header); encryptor.WriteContent("example content"); encryptor.FlushFinalBlock(); Assert.IsTrue(stream.Length > 10); stream.Position = 0; try { using (HelixFileDecryptor decryptor = new HelixFileDecryptor(stream)) { decryptor.Initialize(DerivedBytesProvider.FromPassword("wrongpassword")); var header2 = decryptor.ReadHeader(); Assert.AreEqual(header.FileName, header2.FileName); var content = decryptor.GetContentString(); Assert.AreEqual("example content", content); } Assert.IsTrue(false, "Expecting InvalidPasswordException"); } catch (InvalidPasswordException) { } } }
public void HelixFileDecryptor_EncryptThenDecrypt() { using (MemoryStream stream = new MemoryStream()) { FileEntry headerSaved; using (HelixFileEncryptor encryptor = new HelixFileEncryptor(stream, DerivedBytesProvider.FromPassword("password"), HelixFileVersion.UnitTest)) { headerSaved = new FileEntry(); headerSaved.FileName = "testfile.txt"; encryptor.WriteHeader(headerSaved); encryptor.WriteContent("example content"); Assert.IsTrue(stream.Length > 10); } //Assert.IsTrue(stream.CanRead, "stream.CanRead"); //Assert.IsTrue(stream.CanWrite, "stream.CanWrite"); using (var readStream = new MemoryStream(stream.ToArray(), false)) using (HelixFileDecryptor decryptor = new HelixFileDecryptor(readStream)) { decryptor.Initialize(DerivedBytesProvider.FromPassword("password")); var header2 = decryptor.ReadHeader(); Assert.AreEqual(headerSaved.FileName, header2.FileName); var content = decryptor.GetContentString(); Assert.AreEqual("example content", content); } } }