Esempio n. 1
0
        public async Task DecryptStreamAsync_DecryptEncryptedStream(KeyType keyType)
        {
            // Arrange
            TestFactory testFactory = new TestFactory();
            await testFactory.ArrangeAsync(keyType, FileType.Known);

            PGP pgp = new PGP();

            // Act
            using (FileStream inputFileStream = new FileStream(testFactory.ContentFilePath, FileMode.Open, FileAccess.Read))
                using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
                    using (Stream publicKeyStream = new FileStream(testFactory.PublicKeyFilePath, FileMode.Open, FileAccess.Read))
                        await pgp.EncryptStreamAsync(inputFileStream, outputFileStream, publicKeyStream);

            using (FileStream inputFileStream = new FileStream(testFactory.EncryptedContentFilePath, FileMode.Open, FileAccess.Read))
                using (Stream outputFileStream = File.Create(testFactory.DecryptedContentFilePath))
                    using (Stream privateKeyStream = new FileStream(testFactory.PrivateKeyFilePath, FileMode.Open, FileAccess.Read))
                        await pgp.DecryptStreamAsync(inputFileStream, outputFileStream, privateKeyStream, testFactory.Password);

            string decryptedContent = await File.ReadAllTextAsync(testFactory.DecryptedContentFilePath);

            // Assert
            Assert.True(File.Exists(testFactory.EncryptedContentFilePath));
            Assert.True(File.Exists(testFactory.DecryptedContentFilePath));
            Assert.Equal(testFactory.Content, decryptedContent.Trim());

            // Teardown
            testFactory.Teardown();
        }
Esempio n. 2
0
        internal static async Task <string> EncryptStringAsync(this PGP pgp, string data, byte[] key, bool armor = true, bool integrityCheck = true)
        {
            using (var ks = new MemoryStream(key))
                using (var ds = new MemoryStream(Encoding.UTF8.GetBytes(data)))
                    using (var os = new MemoryStream())
                    {
                        await pgp.EncryptStreamAsync(ds, os, ks, armor, integrityCheck);

                        return(Encoding.UTF8.GetString(os.ToArray()));
                    }
        }
Esempio n. 3
0
        private static async Task <Stream> EncryptAsync(Stream inputStream, string publicKey)
        {
            using (PGP pgp = new PGP())
            {
                Stream outputStream = new MemoryStream();

                using (inputStream)
                    using (Stream publicKeyStream = GenerateStreamFromString(publicKey))
                    {
                        await pgp.EncryptStreamAsync(inputStream, outputStream, publicKeyStream, true, true);

                        outputStream.Seek(0, SeekOrigin.Begin);
                        return(outputStream);
                    }
            }
        }
Esempio n. 4
0
        public async Task DecryptStreamAsync_DecryptSignedAndEncryptedStreamWithMultipleKeys(KeyType keyType)
        {
            // Arrange
            TestFactory testFactory  = new TestFactory();
            TestFactory testFactory2 = new TestFactory();
            await testFactory.ArrangeAsync(keyType, FileType.Known);

            await testFactory2.ArrangeAsync(KeyType.Generated, FileType.Known);

            PGP pgp = new PGP();

            // Act
            using (FileStream inputFileStream = new FileStream(testFactory.ContentFilePath, FileMode.Open, FileAccess.Read))
                using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
                    using (Stream publicKeyStream1 = new FileStream(testFactory.PublicKeyFilePath, FileMode.Open, FileAccess.Read))
                        using (Stream publicKeyStream2 = new FileStream(testFactory2.PublicKeyFilePath, FileMode.Open, FileAccess.Read))
                            await pgp.EncryptStreamAsync(inputFileStream, outputFileStream, new List <Stream>() { publicKeyStream1, publicKeyStream2 });

            using (FileStream inputFileStream = new FileStream(testFactory.EncryptedContentFilePath, FileMode.Open, FileAccess.Read))
                using (Stream outputFileStream = File.Create(testFactory.DecryptedContentFilePath))
                    using (Stream privateKeyStream = new FileStream(testFactory.PrivateKeyFilePath, FileMode.Open, FileAccess.Read))
                        await pgp.DecryptStreamAsync(inputFileStream, outputFileStream, privateKeyStream, testFactory.Password);

            using (FileStream inputFileStream = new FileStream(testFactory.EncryptedContentFilePath, FileMode.Open, FileAccess.Read))
                using (Stream outputFileStream = File.Create(testFactory2.DecryptedContentFilePath))
                    using (Stream privateKeyStream = new FileStream(testFactory2.PrivateKeyFilePath, FileMode.Open, FileAccess.Read))
                        await pgp.DecryptStreamAsync(inputFileStream, outputFileStream, privateKeyStream, testFactory2.Password);

            string decryptedContent1 = await File.ReadAllTextAsync(testFactory.DecryptedContentFilePath);

            string decryptedContent2 = await File.ReadAllTextAsync(testFactory2.DecryptedContentFilePath);

            bool verified = await pgp.VerifyFileAsync(testFactory.EncryptedContentFilePath, testFactory.PublicKeyFilePath);

            // Assert
            Assert.True(File.Exists(testFactory.EncryptedContentFilePath));
            Assert.True(File.Exists(testFactory.DecryptedContentFilePath));
            Assert.True(File.Exists(testFactory2.DecryptedContentFilePath));
            Assert.Equal(testFactory.Content, decryptedContent1.Trim());
            Assert.Equal(testFactory.Content, decryptedContent2.Trim());
            Assert.True(verified);

            // Teardown
            testFactory.Teardown();
        }
Esempio n. 5
0
        public async Task EncryptStreamAsync_CreateEncryptedFile(KeyType keyType)
        {
            // Arrange
            TestFactory testFactory = new TestFactory();
            await testFactory.ArrangeAsync(keyType, FileType.Known);

            PGP pgp = new PGP();

            // Act
            using (FileStream inputFileStream = new FileStream(testFactory.ContentFilePath, FileMode.Open, FileAccess.Read))
                using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
                    using (Stream publicKeyStream = new FileStream(testFactory.PublicKeyFilePath, FileMode.Open, FileAccess.Read))
                        await pgp.EncryptStreamAsync(inputFileStream, outputFileStream, publicKeyStream);

            // Assert
            Assert.True(File.Exists(testFactory.EncryptedContentFilePath));

            // Teardown
            testFactory.Teardown();
        }
Esempio n. 6
0
        public async Task <string> EncryptAsync(string data)
        {
            using (PGP pgp = new PGP())
            {
                var inputStream  = GenerateStreamFromString(data);
                var outputStream = new MemoryStream();
                var publicKey    = _config["PGP_PublicKey"];

                byte[] b = Convert.FromBase64String(publicKey);

                var strOriginal = System.Text.Encoding.UTF8.GetString(b);

                using (Stream publicKeyStream = GenerateStreamFromString(strOriginal))
                {
                    await pgp.EncryptStreamAsync(inputStream, outputStream, publicKeyStream, true, true);

                    outputStream.Seek(0, SeekOrigin.Begin);
                    return(GenerateStringFromStream(outputStream));
                }
            }
        }
Esempio n. 7
0
        public async Task EncryptStreamAsync_CreateEncryptedStreamWithMultipleKeys(KeyType keyType)
        {
            // Arrange
            TestFactory testFactory  = new TestFactory();
            TestFactory testFactory2 = new TestFactory();
            await testFactory.ArrangeAsync(keyType, FileType.Known);

            await testFactory2.ArrangeAsync(KeyType.Generated, FileType.Known);

            PGP pgp = new PGP();

            // Act
            using (FileStream inputFileStream = new FileStream(testFactory.ContentFilePath, FileMode.Open, FileAccess.Read))
                using (Stream outputFileStream = File.Create(testFactory.EncryptedContentFilePath))
                    using (Stream publicKeyStream1 = new FileStream(testFactory.PublicKeyFilePath, FileMode.Open, FileAccess.Read))
                        using (Stream publicKeyStream2 = new FileStream(testFactory2.PublicKeyFilePath, FileMode.Open, FileAccess.Read))
                            await pgp.EncryptStreamAsync(inputFileStream, outputFileStream, new List <Stream>() { publicKeyStream1, publicKeyStream2 });

            // Assert
            Assert.True(File.Exists(testFactory.EncryptedContentFilePath));

            // Teardown
            testFactory.Teardown();
        }
Esempio n. 8
0
 public async Task EncryptStreamAsync(Stream inputStream, Stream outputStream, PublicKey publicKey)
 {
     using var pgp             = new PGP();
     using var publicKeyStream = GetPublicKey(publicKey);
     await pgp.EncryptStreamAsync(inputStream, outputStream, publicKeyStream, true, true);
 }