如何在C#RSA中使用私钥加密和使用公钥解密 [英] How to encrypt with private key and decrypt with public key in c# RSA

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

问题描述

我找到了几种解决方案,可以在其中使用.Net RSA Provider来使用公钥对消息加密,并使用私钥对消息进行解密。

I found several solutions where I can use the .Net RSA Provider to Encrypt a message with the public key and Decrypt it with the private one.

我想拥有的是使用私钥加密和使用公钥解密。

But what I want to have is to Encrypt with the private key and Decrypt with the public key.

我不想将公钥存储在我的应用中并例如对我的许可证进行加密具有私钥的开发机,将其发送到应用程序,然后使用公钥对信息进行解密。

I want t store the public key in my app and encrypt a license for example on my dev machine with the private key, send it to the app and let the information decrypt with a public key.

我该如何实现?

推荐答案

您可以使用签名生成,其中私钥用于生成签名以验证您的邮件是真实的。

You can use signature generation, In which the private key is used to generate a signature that verifies that your message is authentic.

// Create message and signature on your end
string message = "Here is the license message";

var converter = new ASCIIEncoding();
byte[] plainText = converter.GetBytes(message);

var rsaWrite = new RSACryptoServiceProvider();
var privateParams = rsaWrite.ExportParameters(true);

// Generate the public key / these can be sent to the user.
var publicParams = rsaWrite.ExportParameters(false);

byte[] signature =
    rsaWrite.SignData(plainText, new SHA1CryptoServiceProvider());

// Verify from the user's side. Note that only the public parameters
// are needed.
var rsaRead = new RSACryptoServiceProvider();
rsaRead.ImportParameters(publicParams);
if (rsaRead.VerifyData(plainText,
                       new SHA1CryptoServiceProvider(),
                       signature))
{
    Console.WriteLine("Verified!");
}
else
{
    Console.WriteLine("NOT verified!");
}

您可以从这里

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

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