public void TestEncrypt_Decrypt_WrongDecryptionKey() { PlainMessageStream.Seek(0, SeekOrigin.Begin); PublicKey1.Seek(0, SeekOrigin.Begin); PrivateKey1.Seek(0, SeekOrigin.Begin); var encryptionTask = new PgpEncryptionBuilder() .Encrypt(PlainMessageStream) .WithArmor() .WithCompression() .WithIntegrityCheck() .WithPublicKey(PublicKey1) .WithSigning(PrivateKey1, PassPhrase1) .Build(); var encryptedStream = encryptionTask.Run().GetEncryptedStream(); var encryptedText = new StreamReader(encryptedStream).ReadToEnd(); encryptedStream.Seek(0, SeekOrigin.Begin); PlainMessageStream.Seek(0, SeekOrigin.Begin); var decryptionTask = new PgpDecryptionBuilder() .Decrypt(encryptedStream) .WithPrivateKey(PrivateKey2, PassPhrase2) .VerifySignatureUsingKey(PublicKey2) .Build(); Assert.Throws <SecretKeyNotFound>(() => decryptionTask.Run()); }
public void TestEncrypt_Decrypt_2KeysAnd2Signatures() { PlainMessageStream.Seek(0, SeekOrigin.Begin); PublicKey1.Seek(0, SeekOrigin.Begin); PrivateKey1.Seek(0, SeekOrigin.Begin); PublicKey2.Seek(0, SeekOrigin.Begin); PrivateKey2.Seek(0, SeekOrigin.Begin); var encryptionTask = new PgpEncryptionBuilder() .Encrypt(PlainMessageStream) .WithArmor() .WithCompression() .WithIntegrityCheck() .WithPublicKey(PublicKey1) .WithPublicKey(PublicKey2) .WithSigning(PrivateKey1, PassPhrase1) .WithSigning(PrivateKey2, PassPhrase2) .Build(); var encryptedStream = encryptionTask.Run().GetEncryptedStream(); var encryptedText = new StreamReader(encryptedStream).ReadToEnd(); encryptedStream.Seek(0, SeekOrigin.Begin); PlainMessageStream.Seek(0, SeekOrigin.Begin); PublicKey1.Seek(0, SeekOrigin.Begin); PrivateKey1.Seek(0, SeekOrigin.Begin); PublicKey2.Seek(0, SeekOrigin.Begin); PrivateKey2.Seek(0, SeekOrigin.Begin); var decryptionTask = new PgpDecryptionBuilder() .Decrypt(encryptedStream) .WithPrivateKey(PrivateKey1, PassPhrase1) .WithPrivateKey(PrivateKey2, PassPhrase2) .VerifySignatureUsingKey(PublicKey1) .VerifySignatureUsingKey(PublicKey2) .Build(); var decryptedStream = decryptionTask.Run().GetDecryptedStream(); var decryptedText = new StreamReader(decryptedStream).ReadToEnd(); Assert.Equal(PlainMessage, decryptedText); }
public void TestEncrypt_Decrypt_AESAlgorithm() { PlainMessageStream.Seek(0, SeekOrigin.Begin); PublicKey1.Seek(0, SeekOrigin.Begin); PrivateKey1.Seek(0, SeekOrigin.Begin); PublicKey2.Seek(0, SeekOrigin.Begin); PrivateKey2.Seek(0, SeekOrigin.Begin); var encryptionTask = new PgpEncryptionBuilder() .Encrypt(PlainMessageStream) .WithArmor() .WithCompression() .WithIntegrityCheck() .WithSymmetricKeyAlgorithm(Org.BouncyCastle.Bcpg.SymmetricKeyAlgorithmTag.Aes256) .WithPublicKey(PublicKey1) .Build(); var encryptedStream = encryptionTask.Run().GetEncryptedStream(); var encryptedText = new StreamReader(encryptedStream).ReadToEnd(); encryptedStream.Seek(0, SeekOrigin.Begin); PlainMessageStream.Seek(0, SeekOrigin.Begin); PublicKey1.Seek(0, SeekOrigin.Begin); PrivateKey1.Seek(0, SeekOrigin.Begin); PublicKey2.Seek(0, SeekOrigin.Begin); PrivateKey2.Seek(0, SeekOrigin.Begin); var decryptionTask = new PgpDecryptionBuilder() .Decrypt(encryptedStream) .WithPrivateKey(PrivateKey1, PassPhrase1) .Build(); var decryptedStream = decryptionTask.Run().GetDecryptedStream(); var decryptedText = new StreamReader(decryptedStream).ReadToEnd(); Assert.Equal(PlainMessage, decryptedText); }