public void EncryptUsingPublicKey_DecryptUsingPrivateKey() { // Create Keys, export, only import public _sut.CreateAsymmetricKey(); _sut.ExportPublicKey(PublicKeyFile); _sut.ImportPublicKey(PublicKeyFile); Assert.That(_sut.CryptoServiceProviderIsPublicOnly, Is.True); // Encrypt _sut.EncryptFile(OriginalFile, EncryptedFile); // Decrypt should fail (no private key) try { _sut.DecryptFile(EncryptedFile, DecryptedFile); Assert.Fail(); } catch (Exception exception) { Assert.That(exception.Message, Is.EqualTo("Key does not exist.")); } // Get private key, decryption now works _sut.GetPrivateKey(); Assert.That(_sut.CryptoServiceProviderIsPublicOnly, Is.False); _sut.DecryptFile(EncryptedFile, DecryptedFile); Assert.That(File.ReadAllText(DecryptedFile), Is.EqualTo(Constants.PlainText)); }