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(); }
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())); } }
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); } } }
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(); }
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(); }
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)); } } }
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(); }
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); }