Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <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);
        }