简体   繁体   English

如何使用公钥加密字符串并使用 MimeKit 使用私钥解密?

[英]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.我很难寻找有关如何使用公钥证书加密字符串并使用 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.我想做的是使用 MimeKit 直接加密字符串。 I'm only new to using MimeKit.我只是使用 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用公共 RSA 密钥加密数据? 并用私钥解密? - How to encrypt data using a public RSA key? And decrypt with the private key? 如何使用私钥加密字符串并使用公钥解密? - How to encrypt a string with private key and decrypt with public key? RSACryptoServiceProvider使用自己的公钥和私钥加密和解密 - RSACryptoServiceProvider encrypt and decrypt using own public and private key 在C#中使用公钥加密数据,在php中使用私钥解密数据 - Encrypt data by using a public key in c# and decrypt data by using a private key in php 使用代码签名私钥在PHP中加密,需要使用公钥在C#中解密 - Using code signing private key to encrypt in PHP, need to decrypt in C# using public key 尝试使用 .net 中的 bouncycastle 使用公钥(非私钥)解密字符串 - Trying to decrypt a string with public key(not private) using bouncycastle in .net 如何使用公钥密码术加密字符串 - How to encrypt a string using public key cryptography 使用RSA公钥解密使用RSA私钥加密的字符串 - Using an RSA Public key to decrypt a string that was encrypted using an RSA private key 使用RSA公钥解密使用RSA私钥加密的字符串 - Using an RSA Public Key to decrypt a string that was encrypted using RSA Private Key 使用公钥在java中加密并使用私钥RSA在C#中解密 - Encrypt in java with public key and decrypt in C# with private key RSA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM