How to encrypt using a private key and decrypt using a public key in C # RSA

I found several solutions in which I can use the .Net RSA provider to encrypt a public key message and decrypt it using a private one.

But what I want to have is private key encryption and public key decryption.

I want to save the public key in my application and encrypt the license, for example, on my dev machine using the private key, send it to the application and allow the information to be decrypted using the public key.

How can i achieve this?

+6
source share
1 answer

You can use signature generation, which uses the private key to create a signature that authenticates your message.

// 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 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 get additional help HERE

+6
source

Source: https://habr.com/ru/post/979968/


All Articles