0

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.

ikey
  • 321
  • 4
  • 19
  • 1
    Does this answer your question? [How to use public and private key encryption technique in C#](https://stackoverflow.com/questions/18485715/how-to-use-public-and-private-key-encryption-technique-in-c-sharp) – gunr2171 Dec 23 '20 at 04:33
  • I'm not sure I understand the problem. You say you want to avoid writing the content to a file, but all you do is write it to a file and then read it back into memory in order to pass it to MimeKit. Why not just write it to memory in the first place and then use it with MimeKit the same as you are doing now? – jstedfast Dec 23 '20 at 17:24

1 Answers1

1
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;
    }
}
jstedfast
  • 35,744
  • 5
  • 97
  • 110