Here I get private and public key pair out of rsa object for more understandability of the code.
//Get rsaKeys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048); | |
string publicKey = RSA.ToXmlString(false); | |
string privateKey = RSA.ToXmlString(true); |
The following method will explain how to sign a message. You need to provide a private key and a message need to be signed. This method will be returned signed message.
//Sign message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Signs the message. | |
/// </summary> | |
/// <param name="message">The message.</param> | |
/// <param name="privateKey">The private key.</param> | |
/// <returns>signed message</returns> | |
private static string SignMessage(string message, string privateKey) | |
{ | |
string signedMessage; | |
try | |
{ | |
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048); | |
//Initiate a new instanse with 2048 bit key size | |
rsa.FromXmlString(privateKey); | |
// Load private key | |
signedMessage = Convert.ToBase64String(rsa.SignData(Encoding.UTF8.GetBytes(message), CryptoConfig.MapNameToOID("SHA512"))); | |
//rsa.SignData( buffer, hash algorithm) - For signed data. Here I used SHA512 for hash. | |
//Encoding.UTF8.GetBytes(string) - convert string to byte messafe | |
//Convert.ToBase64String(string) - convert back to a string. | |
} | |
catch (Exception) | |
{ | |
signedMessage = string.Empty; | |
} | |
return signedMessage; | |
} |
Next step is verify the message that you signed using your private key. You need to pass original message, signed message and public key to method. This method will be returned boolean value verified or not.
//Verify message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Verifies the message. | |
/// </summary> | |
/// <param name="originalMessage">The original message.</param> | |
/// <param name="signedMessage">The signed message.</param> | |
/// <param name="publicKey">The public key.</param> | |
/// <returns>verify status</returns> | |
private static bool VerifyMessage(string originalMessage, string signedMessage, string publicKey) | |
{ | |
bool verified; | |
try | |
{ | |
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048); | |
rsa.FromXmlString(publicKey); | |
// load public key | |
verified = rsa.VerifyData(Encoding.UTF8.GetBytes(originalMessage), CryptoConfig.MapNameToOID("SHA512"), Convert.FromBase64String(signedMessage)); | |
} | |
catch (Exception) | |
{ | |
verified = false; | |
} | |
return verified; | |
} |
To this point we are done with methods we need to sign and verify. Lets see how to use them. I wrote it in main method.
//RSA main
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
bool verifyState = false; | |
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048); | |
string publicKey = RSA.ToXmlString(false); | |
string privateKey = RSA.ToXmlString(true); | |
string plainText = "originalMessage"; | |
string tamperMessage = "origiinalMessage"; | |
string signedMessage = SignMessage(plainText, privateKey); | |
verifyState = VerifyMessage(plainText, signedMessage, publicKey); // return true | |
verifyState = VerifyMessage(tamperMessage, signedMessage, publicKey); // return false | |
} |