/// <summary> /// Attempt to sign then encrypt a message using PGP with the specified private and public keys. /// </summary> /// <param name="senderPublicKey">The BouncyCastle public key associated with the signature.</param> /// <param name="senderPrivateKey">The BouncyCastle private key to be used for signing.</param> /// <param name="recipientPublicKeys">Collection of BouncyCastle public keys to be used for encryption.</param> /// <param name="hashAlgorithmTag">The hash algorithm tag to use for signing.</param> /// <param name="symmetricKeyAlgorithmTag">The symmetric key algorithm tag to use for encryption.</param> /// <returns>Whether the encryption completed successfully.</returns> public bool PgpSignAndEncrypt(PgpPublicKey senderPublicKey, PgpPrivateKey senderPrivateKey, IEnumerable <PgpPublicKey> recipientPublicKeys, HashAlgorithmTag hashAlgorithmTag = HashAlgorithmTag.Sha256, SymmetricKeyAlgorithmTag symmetricKeyAlgorithmTag = SymmetricKeyAlgorithmTag.TripleDes) { // Ensure a valid encoding. if (BodyEncoding == null) { BodyEncoding = Encoding.UTF8; } // Attempt to sign. bool signedAndEncrypted = false; using (MemoryStream signedAndEncryptedMessageStream = new MemoryStream()) { // Attempt to encrypt the message. signedAndEncrypted = Pgp.SignAndEncrypt(BodyEncoding.GetBytes(Body), "", signedAndEncryptedMessageStream, senderPublicKey, senderPrivateKey, recipientPublicKeys, hashAlgorithmTag, symmetricKeyAlgorithmTag, true); if (signedAndEncrypted) { signedAndEncrypted = true; rawBody = BodyEncoding.GetString(signedAndEncryptedMessageStream.ToArray()); } } return(signedAndEncrypted); }
/// <summary> /// Attempt to sign then encrypt a message using PGP with the specified private and public keys. /// </summary> /// <param name="senderPublicKey">The BouncyCastle public key associated with the signature.</param> /// <param name="senderPrivateKey">The BouncyCastle private key to be used for signing.</param> /// <param name="recipientPublicKeys">Collection of BouncyCastle public keys to be used for encryption.</param> /// <param name="hashAlgorithmTag">The hash algorithm tag to use for signing.</param> /// <param name="symmetricKeyAlgorithmTag">The symmetric key algorithm tag to use for encryption.</param> /// <returns>Whether the encryption completed successfully.</returns> public bool PgpSignAndEncrypt(PgpPublicKey senderPublicKey, PgpPrivateKey senderPrivateKey, IEnumerable <PgpPublicKey> recipientPublicKeys, HashAlgorithmTag hashAlgorithmTag = HashAlgorithmTag.Sha256, SymmetricKeyAlgorithmTag symmetricKeyAlgorithmTag = SymmetricKeyAlgorithmTag.TripleDes) { // Ensure a valid encoding. if (BodyEncoding == null) { BodyEncoding = Encoding.UTF8; } // Attempt to sign. bool signedAndEncrypted = false; using (MemoryStream signedAndEncryptedMessageStream = new MemoryStream()) { // Attempt to encrypt the message. // OpaqueMail optional setting for protecting the subject. if (SubjectEncryption && !Body.StartsWith("Subject: ")) { signedAndEncrypted = Pgp.SignAndEncrypt(BodyEncoding.GetBytes("Subject: " + Subject + "\r\n" + Body), "", signedAndEncryptedMessageStream, senderPublicKey, senderPrivateKey, recipientPublicKeys, hashAlgorithmTag, symmetricKeyAlgorithmTag, true); } else { signedAndEncrypted = Pgp.SignAndEncrypt(BodyEncoding.GetBytes(Body), "", signedAndEncryptedMessageStream, senderPublicKey, senderPrivateKey, recipientPublicKeys, hashAlgorithmTag, symmetricKeyAlgorithmTag, true); } if (signedAndEncrypted) { // OpaqueMail optional setting for protecting the subject. if (SubjectEncryption) { Subject = "PGP Encrypted Message"; } signedAndEncrypted = true; RawBody = BodyEncoding.GetString(signedAndEncryptedMessageStream.ToArray()); } } return(signedAndEncrypted); }