Signature et vérification des signatures avec RSA C #

J’ai récemment publié des articles sur le cryptage de données volumineuses avec RSA. J’en ai enfin terminé et je passe maintenant à la mise en œuvre de la signature avec la clé privée d’un utilisateur et à la vérification avec la clé publique correspondante. Cependant, chaque fois que je compare les données signées et le message original, je suis simplement renvoyé faux. J’espère que certains d’entre vous pourraient voir ce que je fais mal.

Voici le code:

public static ssortingng SignData(ssortingng 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 ssortingng before returning return Convert.ToBase64Ssortingng(signedBytes); } 

C’est donc la première étape, pour signer les données, ensuite je passe à la vérification des données:

 public static bool VerifyData(ssortingng originalMessage, ssortingng signedMessage, RSAParameters publicKey) { bool success = false; using (var rsa = new RSACryptoServiceProvider()) { byte[] bytesToVerify = Convert.FromBase64Ssortingng(originalMessage); byte[] signedBytes = Convert.FromBase64Ssortingng(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; } 

Et voici le client de test:

 public static void Main(ssortingng[] args) { PublicKeyInfrastructure pki = new PublicKeyInfrastructure(); Cryptograph crypto = new Cryptograph(); RSAParameters privateKey = crypto.GenerateKeys("[email protected]"); const ssortingng PlainText = "This is really sent by me, really!"; RSAParameters publicKey = crypto.GetPublicKey("[email protected]"); ssortingng encryptedText = Cryptograph.Encrypt(PlainText, publicKey); Console.WriteLine("This is the encrypted Text:" + "\n " + encryptedText); ssortingng decryptedText = Cryptograph.Decrypt(encryptedText, privateKey); Console.WriteLine("This is the decrypted text: " + decryptedText); ssortingng messageToSign = encryptedText; ssortingng 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); } 

Est-ce que je manque une étape ici? Selon l’API de cryptographie et les exemples présentés ici, je ne devrais pas calculer manuellement les hachages, car je fournis l’algorithme au sein même de l’appel de méthode.

Toute aide est la bienvenue.

Votre problème est au début de la méthode VerifyData :

 public static bool VerifyData(ssortingng originalMessage, ssortingng 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.FromBase64Ssortingng(originalMessage); var encoder = new UTF8Encoding(); byte[] bytesToVerify = encoder.GetBytes(originalMessage); byte[] signedBytes = Convert.FromBase64Ssortingng(signedMessage); try ... 

Pour une raison quelconque, vous êtes passé à FromBase64Ssortingng au lieu de UTF8Encoding.GetBytes .