Try the following:
class Program { static byte[] Sign(string message, RSAParameters key) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(key); byte[] toSign = Encoding.Unicode.GetBytes(message); return rsa.SignData(toSign, "SHA1"); } static bool Verify(string message, byte[] signature, RSAParameters key) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(key); byte[] toVerify = Encoding.Unicode.GetBytes(message); return rsa.VerifyData(toVerify, "SHA1", signature); } static void Main(string[] args) { string message = "Let sign this message."; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
It is important to note that when creating this program, a new public / private key pair is created. To do what you want, you want to keep the public / private key pair before using it from both ends. Your public key is the only thing you need to verify, so your private key will not be published to the client.
You might want to take a look at ExportParameters or ExportCspBlob to save / load a public / private key pair.
Dave van den eynde
source share