public void EncryptAndDecryptStreamTest() { // Make a provider IBlobCryptoProvider asymmetricProvider = new AsymmetricBlobCryptoProvider(); // In all cases we are READING from streams // (read from original, read from encrypted, read from decrypted). var encryptedStream = asymmetricProvider.EncryptedStream(streamSample); var decryptedStream = asymmetricProvider.DecryptedStream(encryptedStream); byte[] result = new byte[sampleStreamSize]; decryptedStream.Read(result, 0, result.Length); Assert.IsTrue( result.SequenceEqual(streamSample.ToArray()), "Decrypted data does not match original data"); }
public void ToKeyFileAndBackTest() { IBlobCryptoProvider asymmetricProvider = new AsymmetricBlobCryptoProvider(); asymmetricProvider.WriteKeyFile("keyfile.txt"); IBlobCryptoProvider clonedProvider = ProviderFactory.CreateProviderFromKeyFile("keyfile.txt"); var encryptedStream = asymmetricProvider.EncryptedStream(streamSample); var decryptedStream = clonedProvider.DecryptedStream(encryptedStream); byte[] result = new byte[sampleStreamSize]; decryptedStream.Read(result, 0, result.Length); Assert.IsTrue( result.SequenceEqual(streamSample.ToArray()), "Decrypted data does not match original data"); }
public void IsActuallyEncryptedTest() { // Make a provider IBlobCryptoProvider asymmetricProvider = new AsymmetricBlobCryptoProvider(); var encryptedStream = asymmetricProvider.EncryptedStream(streamSample); byte[] result = new byte[sampleStreamSize + (4096 + 256) / 8]; encryptedStream.Read(result, 0, result.Length); Assert.IsFalse( result.SequenceEqual(streamSample.ToArray()), "Encrypted stream is not encrypted"); Assert.IsFalse( result.Take(5).SequenceEqual(streamSample.ToArray().Take(5)), "Encrypted stream is not encrypted"); }
public void DecryptFailsWithX509IfPrivateKeyNotLoadedTest() { // Load Certificate X509Certificate2 cert = new X509Certificate2("4096.pfx", string.Empty, X509KeyStorageFlags.Exportable); // Make a provider IBlobCryptoProvider asymmetricProvider = new AsymmetricBlobCryptoProvider(cert, false); // In all cases we are READING from streams // (read from original, read from encrypted, read from decrypted). var encryptedStream = asymmetricProvider.EncryptedStream(streamSample); var decryptedStream = asymmetricProvider.DecryptedStream(encryptedStream); byte[] result = new byte[sampleStreamSize]; decryptedStream.Read(result, 0, result.Length); Assert.IsTrue( result.SequenceEqual(streamSample.ToArray()), "Decrypted data does not match original data"); }
public void ToKeyFileStringCertificateTest() { // Load Certificate X509Certificate2 cert = new X509Certificate2("4096.pfx", string.Empty, X509KeyStorageFlags.Exportable); // Make a provider IBlobCryptoProvider asymmetricProvider = new AsymmetricBlobCryptoProvider(cert, true); string keyString = asymmetricProvider.ToKeyFileString(); // Clone a new provider from exported keyfile IBlobCryptoProvider clonedProvider = ProviderFactory.CreateProviderFromKeyFileString(keyString); // Run an encryption loop using the two providers var encryptedStream = asymmetricProvider.EncryptedStream(streamSample); var decryptedStream = clonedProvider.DecryptedStream(encryptedStream); byte[] result = new byte[sampleStreamSize]; decryptedStream.Read(result, 0, result.Length); Assert.IsTrue( result.SequenceEqual(streamSample.ToArray()), "Decrypted data does not match original data"); }