示例#1
0
        /// <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);
        }
示例#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.
                // 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);
        }
示例#3
0
        public override string ToString()
        {
            string WrappedText = null;

            string base64Content = BodyEncoding.GetString(Body);

            WrappedText = ConvertBase64ToCleartext(base64Content, BodyEncoding);

            return(WrappedText);
        }
示例#4
0
        /// <summary>
        /// returns HTML part of Body
        /// </summary>
        /// <returns></returns>
        public string Get_Mail_html()
        {
            string body        = BodyEncoding.GetString(Body);
            string text        = "";
            string html        = "";
            int    html_type   = 0;
            int    index_doc   = body.ToLower().IndexOf("<!DOCTYPE".ToLower());
            int    index_style = body.ToLower().IndexOf("<style>".ToLower());
            int    index_html  = body.ToLower().IndexOf("<html>".ToLower());

            if (index_doc < 0)
            {
                index_doc = int.MaxValue;
            }
            if (index_style < 0)
            {
                index_style = int.MaxValue;
            }
            if (index_html < 0)
            {
                index_html = int.MaxValue;
            }
            html_type = (index_doc < index_style ? (index_doc < index_html ? 0 : 2) : (index_style < index_html ? 1 : 2));
            if ((index_doc == index_style) && (index_style == index_html))
            {
                text = body;
            }
            else
            {
                switch (html_type)
                {
                case 0:
                    index_doc = index_doc > 0 ? index_doc - 1 : 0;
                    text      = body.Substring(0, index_doc);
                    html      = body.Substring(index_doc > 0 ? index_doc : 0, body.Length - index_doc);
                    break;

                case 1:
                    index_style = index_style > 0 ? index_style - 1 : 0;
                    text        = body.Substring(0, index_style);
                    html        = body.Substring(index_style > 0 ? index_style : 0, body.Length - index_style);
                    break;

                case 2:
                    index_html = index_html > 0 ? index_html - 1 : 0;
                    text       = body.Substring(0, index_html);
                    html       = body.Substring(index_html > 0 ? index_html : 0, body.Length - index_html).Trim('\n');
                    break;
                }
            }
//            if (html == "") html = body;
            return(html);
        }
示例#5
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);
        }
示例#6
0
        /// <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);
        }
 /// <summary>
 /// Gets this MessagePart's <see cref="Body"/> as text.<br/>
 /// This is simply the <see cref="BodyEncoding"/> being used on the raw bytes of the <see cref="Body"/> property.<br/>
 /// This method is only valid to call if it is not a MultiPart message and therefore contains a body.<br/>
 /// </summary>
 /// <returns>The <see cref="Body"/> property as a string</returns>
 public string GetBodyAsText()
 {
     return(BodyEncoding.GetString(Body));
 }
示例#8
0
        /// <summary>
        /// Attempt to decrypt a PGP protected message using the matching private key.
        /// </summary>
        /// <param name="decryptedMessage">If successful, the decrypted message.</param>
        /// <returns>Whether the decryption completed successfully.</returns>
        public bool PgpDecrypt(PgpPrivateKey recipientPrivateKey, out byte[] decryptedMessage)
        {
            string encryptedBody = "";

            // Process each MIME part.
            if (MimeParts != null)
            {
                for (int i = 0; i < MimeParts.Count; i++)
                {
                    MimePart mimePart = MimeParts[i];

                    // Check if the MIME part is encrypted or signed using PGP.
                    if (mimePart.Body.StartsWith("-----BEGIN PGP MESSAGE-----"))
                    {
                        encryptedBody = Functions.ReturnBetween(mimePart.Body, "-----BEGIN PGP MESSAGE-----\r\n", "\r\n-----END PGP MESSAGE-----");
                        break;
                    }
                }
            }
            else
            {
                if (Body.StartsWith("-----BEGIN PGP MESSAGE-----"))
                {
                    encryptedBody = Functions.ReturnBetween(Body, "-----BEGIN PGP MESSAGE-----\r\n", "\r\n-----END PGP MESSAGE-----");
                }
            }

            // Process an encrypted body if found.
            if (!string.IsNullOrEmpty(encryptedBody))
            {
                // Ignore the PGP headers.
                int doubleLineBreak = encryptedBody.IndexOf("\r\n\r\n");
                if (doubleLineBreak > -1)
                {
                    encryptedBody = encryptedBody.Substring(doubleLineBreak + 4);
                }

                // Attempt to decrypt the message and set the body if successful.
                if (Pgp.Decrypt(Encoding.UTF8.GetBytes(encryptedBody), out decryptedMessage, recipientPrivateKey))
                {
                    // Ensure a valid encoding.
                    if (BodyEncoding == null)
                    {
                        BodyEncoding = Encoding.UTF8;
                    }

                    // Convert the byte array back to a string.
                    Body = BodyEncoding.GetString(decryptedMessage);

                    // If the body was successfully decrypted, attempt to decrypt attachments.
                    foreach (Attachment attachment in Attachments)
                    {
                        // Only process attachments with names ending in ".pgp".
                        if (attachment.Name.ToLower().EndsWith(".pgp"))
                        {
                            if (Pgp.Decrypt(attachment.ContentStream, out decryptedMessage, recipientPrivateKey))
                            {
                                attachment.ContentStream = new MemoryStream(decryptedMessage);
                                attachment.Name          = attachment.Name.Substring(0, attachment.Name.Length - 4);
                            }
                        }
                    }

                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                decryptedMessage = null;
                return(false);
            }
        }