如何用加密密钥字符串和公钥解密? [英] How to encrypt a string with private key and decrypt with public key?

查看:475
本文介绍了如何用加密密钥字符串和公钥解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来没有与加密库工作,但我想用加密密钥字符串和公钥解密。如何在C#中实现这一目标。请帮我用一小段代码。
感谢

i never work with encryption library but i want to encrypt a string with private key and decrypt by public key. how to achieve this in c#. please help me with small code snippet. thanks

推荐答案

据我所知,虽然是在一个公钥和私钥之间的数学技术上没有什么区别,你需要使用他们一贯出于安全原因。

AFAIK, Although there's technically no difference in the math between a public and private key, you need to use them consistently for security reasons.

你问用私钥加密和公钥解密。这通常是围绕着错误的方式。如果你想去这个方向,它通常被称为数字签名的操作。

You're asking to encrypt with the private key and decrypt with the public key. This is generally the wrong way around. If you want to go this direction, it's usually an operation called "digitally signing".

如果你用私钥签名,使得它是可逆的,通过的公共密钥的,那么它是不是一个真正的秘密。我以为你只是想将消息从发送方是合法验证。你需要的是数字签名 - 用公钥,私钥(或不对称),关键还是执行。

If you sign with the private key such that it is reversible by the public key, then it's not really a secret. I assume you're just trying to authenticate the message as being legitimately from the sender. What you need is digital signatures - still performed with the public-key-private-key (or "asymmetric") key.

通过数字签名,消息本身已经不是秘密了(有没有必要,因为任何人与公共密钥可以反正解密),但伴随着其他数据的基础上,消息,可验证使用公共密钥和可能只被某人匹配的私钥来计算。

With digital signatures, the message itself is not secret (there's no need since anyone with the public key could decrypt it anyway) but is accompanied by additional data, based on the message, that is verifiable using the public key and could have only been computed by someone with matching private key.

它看起来像下面这样。现在,你只需要找出你会得到钥匙。

It would look something like the following. Now you just have to figure out where you'll get the key.

static byte[] GenerateDigitalSignature(byte[] data, RSAParameters asymmetricKey)
{
  using (var rcsp = new RSACryptoServiceProvider())
  using (var cp = new SHA1CryptoServiceProvider())
  {
    rcsp.ImportParameters(asymmetricKey);

    return rcsp.SignData(data, cp);
  }
}

这篇关于如何用加密密钥字符串和公钥解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆