public async Task WriteReadData() { const string fileName = "hello.txt"; Random rand = new Random(156); byte[] data = new byte[1024 * 156]; rand.NextBytes(data); IRsaCryptoAccessor p12CryptoAccessor = this.CreateAccesor(); using MemoryStream writeMs = new MemoryStream(); using (ContainerWriter writer = new ContainerWriter(writeMs, fileName, await p12CryptoAccessor.ExtractPublicCertificate())) { await writer.Write(data, 0, data.Length); } using MemoryStream readMs = new MemoryStream(writeMs.ToArray()); using (ContainerReader reader = new ContainerReader(readMs, p12CryptoAccessor)) { string decodedFileName = await reader.ReadFileName(); Assert.AreEqual(fileName, decodedFileName, "Decoded file name"); using Stream contentStream = await reader.GetContentStream(); using MemoryStream buffer = new MemoryStream(data.Length + 10); await contentStream.CopyToAsync(buffer); byte[] decryptedData = buffer.ToArray(); Assert.AreEqual(data.Length, decryptedData.Length); CollectionAssert.AreEquivalent(data, decryptedData); } }
private static int EncryptFile(EncryptFileOptions opts) { string fileName = Path.GetFileName(opts.FileToEncrypt); X509Certificate2 certificate = new X509Certificate2(opts.CertificatePath); using FileStream outputFiletream = new FileStream(opts.EncryptedFile, FileMode.Create, FileAccess.ReadWrite); using FileStream inputFiletream = new FileStream(opts.FileToEncrypt, FileMode.Open, FileAccess.Read); using ContainerWriter writer = new ContainerWriter(outputFiletream, fileName, certificate); writer.AdditionalPadingSize = opts.ParseAdditionalPadingSize(); writer.Write(inputFiletream).GetAwaiter().GetResult(); return(0); }