Search code examples
c#encryptionmimepkcs#7mimekit

How to encrypt string with public key and decrypt using private key using MimeKit?


I'm having a hard time looking for a solution on how to encrypt a string with public key certificate and decrypt it with private key certificate using Mimekit. This is my code for encrypting a text file with a public key certificate :

public string encryptFile(string filename)
{
    var certificate2 = new X509Certificate2(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, /Sample.crt));
    MimeEntity body;

    using (var content = new MemoryStream(File.ReadAllBytes(filename)))
    {
        var part = new MimePart(MimeTypes.GetMimeType(filename))
        {
            ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
            ContentTransferEncoding = ContentEncoding.Binary,
            FileName = Path.GetFileName(filename),
            Content = new MimeContent(content)
        };


        var recipient = new CmsRecipient(certificate2)
        {
            EncryptionAlgorithms = new EncryptionAlgorithm[] { EncryptionAlgorithm.TripleDes }
        };
        var recipients = new CmsRecipientCollection();
        recipients.Add(recipient);

        using (var ctx = new TemporarySecureMimeContext())
            body = ApplicationPkcs7Mime.Encrypt(ctx, recipients, part);
    }

    string response = body.ToString();
    return response;
}

But using this way I'm writing the string I wanted to encrypt to a file before encrypting it. What I wanted to do is to directly encrypt the string using MimeKit. I'm only new to using MimeKit. If anyone knows how can I do this it will be a great help.


Solution

  • public string EncryptString(string value)
    {
        var certificate2 = new X509Certificate2(Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, /Sample.crt));
        MimeEntity body;
    
        using (var content = new MemoryStream(Encoding.UTF8.GetBytes (value)))
        {
            var part = new MimePart(MimeTypes.GetMimeType(filename))
            {
                ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
                ContentTransferEncoding = ContentEncoding.Binary,
                FileName = Path.GetFileName(filename),
                Content = new MimeContent(content)
            };
    
    
            var recipient = new CmsRecipient(certificate2)
            {
                EncryptionAlgorithms = new EncryptionAlgorithm[] { EncryptionAlgorithm.TripleDes }
            };
            var recipients = new CmsRecipientCollection();
            recipients.Add(recipient);
    
            using (var ctx = new TemporarySecureMimeContext())
                body = ApplicationPkcs7Mime.Encrypt(ctx, recipients, part);
        }
    
        using (var memory = new MemoryStream ()) {
            body.WriteTo (memory);
    
            string response = Encoding.UTF8.GetString (memory.ToArray ());
            return response;
        }
    }