Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        private static int DecryptFile(DecryptFileOptions opts)
        {
            string          pkcs11LibPath = opts.LibPath ?? FindEidLibrary();
            IBokPinProvider pinProvider   = CreatePinpProvider(opts.UseConsolePin);

            using EidRsaCryptoAccessor eidRsaCryptoAccessor = new EidRsaCryptoAccessor(pkcs11LibPath, pinProvider);

            using FileStream inputFiletream = new FileStream(opts.EncryptedFile, FileMode.Open, FileAccess.Read);

            using ContainerReader reader = new ContainerReader(inputFiletream, eidRsaCryptoAccessor);

            string fileName       = reader.ReadFileName().GetAwaiter().GetResult();
            string outputFilePath = Path.Combine(Path.GetDirectoryName(opts.EncryptedFile), fileName);

            using FileStream outputFiletream = new FileStream(outputFilePath, FileMode.Create, FileAccess.ReadWrite);

            using Stream contentSrream = reader.GetContentStream().GetAwaiter().GetResult();
            contentSrream.CopyTo(outputFiletream);

            return(0);
        }