Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
The signature value getting generated for a file is getting changed each time on applying signature using RSA 256 algorithm.

What I have tried:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); // Creates a new RANDOM key.
     RSAParameters privatekey = rsa.ExportParameters(true);
     RSAParameters publickey = rsa.ExportParameters(false);







     string sign = SignData1(content, privatekey);


public static string SignData1(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("SHA256"));
            }
            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 signedBytes;
        return Convert.ToBase64String(signedBytes);
    }
Posted
Updated 1-May-17 20:48pm
Comments
Richard MacCutchan 1-May-17 10:49am    
I would guess it has something to do with the random key.
Dave Kreskowiak 1-May-17 12:53pm    
What part of "// Creates a new RANDOM key." do you not understand?

1 solution

If it didn't change every time you generated it, breaking it would be vastly simpler. Good cryptographic ciphers, whether used for signatures or to encrypt whole documents, start with a "salt" value that is expected to change every time the cipher is used, even with the same key. The degree to which the salt is truly random is called it entropy, and a great deal of research has focused on ways to get more entropy (randomness) into the process.

For example, a good salt generator might find a way to combine several independent sources of entropy in such a way that whole is at least equal to the sum of its parts.

For example, an early AES (Rijndael) implementation published by Dr. Brian Gladman used values from the CPU clock as a source of entropy.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900