使用RSA C#签名和validation签名

我最近发布了有关使用RSAencryption大数据的问题,我终于完成了这个工作,现在我正在使用用户的私钥进行签名,并使用相应的公钥进行validation。 但是,每当我比较签名的数据和原来的消息,我基本上只是得到错误返回。 我希望你的一些人能看到我做错了什么。

这里是代码:

public static string SignData(string message, RSAParameters privateKey) { //// The array to store the signed message in bytes byte[] signedBytes; using (var rsa = new RSACryptoServiceProvider()) { //// Write the message to a byte array using UTF8 as the encoding. var encoder = new UTF8Encoding(); byte[] originalData = encoder.GetBytes(message); try { //// Import the private key used for signing the message rsa.ImportParameters(privateKey); //// Sign the data, using SHA512 as the hashing algorithm signedBytes = rsa.SignData(originalData, CryptoConfig.MapNameToOID("SHA512")); } catch (CryptographicException e) { Console.WriteLine(e.Message); return null; } finally { //// Set the keycontainer to be cleared when rsa is garbage collected. rsa.PersistKeyInCsp = false; } } //// Convert the a base64 string before returning return Convert.ToBase64String(signedBytes); } 

所以这是签署数据的第一步,接下来我继续validation数据:

 public static bool VerifyData(string originalMessage, string signedMessage, RSAParameters publicKey) { bool success = false; using (var rsa = new RSACryptoServiceProvider()) { byte[] bytesToVerify = Convert.FromBase64String(originalMessage); byte[] signedBytes = Convert.FromBase64String(signedMessage); try { rsa.ImportParameters(publicKey); SHA512Managed Hash = new SHA512Managed(); byte[] hashedData = Hash.ComputeHash(signedBytes); success = rsa.VerifyData(bytesToVerify, CryptoConfig.MapNameToOID("SHA512"), signedBytes); } catch (CryptographicException e) { Console.WriteLine(e.Message); } finally { rsa.PersistKeyInCsp = false; } } return success; } 

这里是testing客户端:

 public static void Main(string[] args) { PublicKeyInfrastructure pki = new PublicKeyInfrastructure(); Cryptograph crypto = new Cryptograph(); RSAParameters privateKey = crypto.GenerateKeys("email@email.com"); const string PlainText = "This is really sent by me, really!"; RSAParameters publicKey = crypto.GetPublicKey("email@email.com"); string encryptedText = Cryptograph.Encrypt(PlainText, publicKey); Console.WriteLine("This is the encrypted Text:" + "\n " + encryptedText); string decryptedText = Cryptograph.Decrypt(encryptedText, privateKey); Console.WriteLine("This is the decrypted text: " + decryptedText); string messageToSign = encryptedText; string signedMessage = Cryptograph.SignData(messageToSign, privateKey); //// Is this message really, really, REALLY sent by me? bool success = Cryptograph.VerifyData(messageToSign, signedMessage, publicKey); Console.WriteLine("Is this message really, really, REALLY sent by me? " + success); } 

我在这里错过了一步吗? 根据Cryptography API和那里的例子,我不应该手动计算任何散列,因为我在方法调用本身中提供algorithm。

任何帮助将不胜感激。

你的问题是在VerifyData方法的开始处:

 public static bool VerifyData(string originalMessage, string signedMessage, RSAParameters publicKey) { bool success = false; using (var rsa = new RSACryptoServiceProvider()) { //Don't do this, do the same as you did in SignData: //byte[] bytesToVerify = Convert.FromBase64String(originalMessage); var encoder = new UTF8Encoding(); byte[] bytesToVerify = encoder.GetBytes(originalMessage); byte[] signedBytes = Convert.FromBase64String(signedMessage); try ... 

出于某种原因,您切换到FromBase64String而不是UTF8Encoding.GetBytes