예제 #1
0
        private Multipart Sign(Multipart input, GpgPrivateKeyInfo senderKey)
        {
            var gpgMultipart = new Multipart("signed");

            gpgMultipart.ContentType.Parameters.Add("micalg", "pgp-sha256");
            gpgMultipart.ContentType.Parameters.Add("protocol", "application/pgp-signature");
            gpgMultipart.Add(input);

            var multipartStream = new MemoryStream();

            input.WriteTo(multipartStream);
            var signedText = Encoding.UTF8.GetString(multipartStream.ToArray()).Replace("\n", "\r\n");

            _gpg.Sign(signedText, out string signatureText, senderKey.Id, SignatureType.DetachSign, true, senderKey.Passphrase);
            var signaturePart = new TextPart("pgp-signature");

            signaturePart.ContentType.MediaType       = "application";
            signaturePart.ContentType.Name            = "signature.asc";
            signaturePart.ContentDisposition          = new ContentDisposition("attachment");
            signaturePart.ContentDisposition.FileName = "signature.asc";
            signaturePart.Text = signatureText;
            signaturePart.ContentTransferEncoding = ContentEncoding.SevenBit;
            signaturePart.Headers.Remove(HeaderId.ContentTransferEncoding);

            gpgMultipart.Add(signaturePart);

            return(gpgMultipart);
        }
예제 #2
0
        private Multipart EncryptAndSign(Multipart input, GpgPrivateKeyInfo senderKey, GpgPublicKeyInfo recipientKey)
        {
            var gpgMultipart = new Multipart("encrypted");

            gpgMultipart.ContentType.Parameters.Add("protocol", "application/pgp-encrypted");

            var versionPart = new TextPart("pgp-encrypted");

            versionPart.ContentType.MediaType = "application";
            versionPart.Headers.Add(new Header("Content-Description", "PGP/MIME version identification"));
            versionPart.Text = "Version: 1";
            gpgMultipart.Add(versionPart);

            var multipartStream = new MemoryStream();

            input.WriteTo(multipartStream);
            multipartStream.Position = 0;
            var plainText = Encoding.UTF8.GetString(multipartStream.ToArray()).Replace("\n", "\r\n");

            _gpg.EncryptAndSign(plainText, out string cipherText, recipientKey.Id, senderKey.Id, true, senderKey.Passphrase);
            var encryptedPart = new TextPart("octet-stream");

            encryptedPart.ContentType.MediaType       = "application";
            encryptedPart.ContentType.Name            = "encrypted.asc";
            encryptedPart.ContentDisposition          = new ContentDisposition("inline");
            encryptedPart.ContentDisposition.FileName = "encrypted.asc";
            encryptedPart.Text = cipherText;
            encryptedPart.ContentTransferEncoding = ContentEncoding.SevenBit;
            encryptedPart.Headers.Remove(HeaderId.ContentTransferEncoding);
            gpgMultipart.Add(encryptedPart);

            return(gpgMultipart);
        }
예제 #3
0
        public MimeMessage Create(InternetAddress from, InternetAddress to, GpgPrivateKeyInfo senderKey, GpgPublicKeyInfo recipientKey, string subject, Multipart content)
        {
            if (senderKey != null && recipientKey != null)
            {
                content = EncryptAndSign(content, senderKey, recipientKey);
            }
            else if (recipientKey != null)
            {
                content = Encrypt(content, recipientKey);
            }
            else if (senderKey != null)
            {
                content = Sign(content, senderKey);
            }

            var message = new MimeMessage();

            message.From.Add(from);
            message.To.Add(to);
            message.Subject = subject;
            message.Body    = content;

            return(message);
        }
예제 #4
0
 public void Send(InternetAddress from, InternetAddress to, GpgPrivateKeyInfo senderKey, GpgPublicKeyInfo recipientKey, string subject, Multipart content)
 {
     Send(Create(from, to, senderKey, recipientKey, subject, content));
 }
예제 #5
0
 public void Send(InternetAddress to, GpgPrivateKeyInfo senderKey, GpgPublicKeyInfo recipientKey, string subject, Multipart content)
 {
     Send(MailboxAddress.Parse(_config.SystemMailAddress), to, senderKey, recipientKey, subject, content);
 }