简体   繁体   English

如何在 c# RSA 中使用私钥加密和使用公钥解密

[英]How to encrypt with private key and decrypt with public key in c# RSA

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.我找到了几种解决方案,我可以使用 .Net RSA Provider 使用公钥加密消息并使用私钥解密。

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.我想将公钥存储在我的应用程序中,并使用私钥在我的开发机器上加密许可证,将其发送到应用程序并让信息用公钥解密。

How can I achieve that?我怎样才能做到这一点?

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!");
}

you can take further help from HERE你可以从这里获得进一步的帮助

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

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