简体   繁体   中英

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.

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM