/// <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 encrypt a message using PGP with the specified private key. /// </summary> /// <param name="recipientPublicKeys">Collection of BouncyCastle public keys to be used for encryption.</param> /// <param name="symmetricKeyAlgorithmTag">The symmetric key algorithm tag to use for encryption.</param> /// <returns>Whether the encryption completed successfully.</returns> public bool PgpEncrypt(IEnumerable <PgpPublicKey> recipientPublicKeys, SymmetricKeyAlgorithmTag symmetricKeyAlgorithmTag = SymmetricKeyAlgorithmTag.TripleDes) { // Ensure a valid encoding. if (BodyEncoding == null) { BodyEncoding = Encoding.UTF8; } // Attempt to encrypt. bool encrypted; using (MemoryStream encryptedMessageStream = new MemoryStream()) { // Attempt to encrypt the message. // OpaqueMail optional setting for protecting the subject. if (SubjectEncryption && !Body.StartsWith("Subject: ")) { encrypted = Pgp.Encrypt(BodyEncoding.GetBytes("Subject: " + Subject + "\r\n" + Body), encryptedMessageStream, "", recipientPublicKeys, symmetricKeyAlgorithmTag, true); } else { encrypted = Pgp.Encrypt(BodyEncoding.GetBytes(Body), encryptedMessageStream, "", recipientPublicKeys, symmetricKeyAlgorithmTag, true); } if (encrypted) { RawBody = BodyEncoding.GetString(encryptedMessageStream.ToArray()); } } // If the body was successfully encrypted, attempt to encrypt attachments. if (encrypted) { // OpaqueMail optional setting for protecting the subject. if (SubjectEncryption) { Subject = "PGP Encrypted Message"; } foreach (Attachment attachment in Attachments) { // Don't process attachments with names ending in ".pgp". if (!attachment.Name.ToLower().EndsWith(".pgp")) { using (MemoryStream attachmentStream = new MemoryStream()) { encrypted = Pgp.Encrypt(attachment.ContentStream, attachmentStream, "", recipientPublicKeys); if (encrypted) { attachment.ContentStream = attachmentStream; attachment.Name += ".pgp"; } } } } } return(encrypted); }
/// <summary> /// Attempt to encrypt a message using PGP with the specified private key. /// </summary> /// <param name="recipientPublicKeys">Collection of BouncyCastle public keys to be used for encryption.</param> /// <param name="symmetricKeyAlgorithmTag">The symmetric key algorithm tag to use for encryption.</param> /// <returns>Whether the encryption completed successfully.</returns> public bool PgpEncrypt(IEnumerable <PgpPublicKey> recipientPublicKeys, SymmetricKeyAlgorithmTag symmetricKeyAlgorithmTag = SymmetricKeyAlgorithmTag.TripleDes) { // Ensure a valid encoding. if (BodyEncoding == null) { BodyEncoding = Encoding.UTF8; } // Attempt to encrypt. bool encrypted; using (MemoryStream encryptedMessageStream = new MemoryStream()) { // Attempt to encrypt the message. encrypted = Pgp.Encrypt(BodyEncoding.GetBytes(Body), encryptedMessageStream, "", recipientPublicKeys, symmetricKeyAlgorithmTag, true); if (encrypted) { rawBody = BodyEncoding.GetString(encryptedMessageStream.ToArray()); } } // If the body was successfully encrypted, attempt to encrypt attachments. if (encrypted) { foreach (Attachment attachment in Attachments) { // Don't process attachments with names ending in ".pgp". if (!attachment.Name.ToLower().EndsWith(".pgp")) { using (MemoryStream attachmentStream = new MemoryStream()) { encrypted = Pgp.Encrypt(attachment.ContentStream, attachmentStream, "", recipientPublicKeys); if (encrypted) { attachment.ContentStream = attachmentStream; attachment.Name += ".pgp"; } } } } } return(encrypted); }
/// <summary> /// Attempt to sign a PGP message using the specific private key. /// </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> /// <returns>Whether the signature completed successfully.</returns> public bool PgpSign(PgpPublicKey senderPublicKey, PgpPrivateKey senderPrivateKey, HashAlgorithmTag hashAlgorithmTag = HashAlgorithmTag.Sha256) { // Ensure a valid encoding. if (BodyEncoding == null) { BodyEncoding = Encoding.UTF8; } byte[] signatureBytes; if (Pgp.Sign(BodyEncoding.GetBytes(Body), out signatureBytes, senderPublicKey, senderPrivateKey, hashAlgorithmTag)) { // Fix up a formatting bug in BouncyCastle. rawBody = Encoding.UTF8.GetString(signatureBytes).Replace("-----BEGIN PGP SIGNATURE-----", "\r\n-----BEGIN PGP SIGNATURE-----"); return(true); } return(false); }
/// <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); }