I have 2 Projects in the same solution, in one project I encrypt a message using SignData
and in the other project I am recreating the message that was encrypted in the first project and I compare the results to see if they are the same. But the encryption from the first project doesn't return the same value like the encryption from the second project. How can I make so that if i give the same byte array in both the projects I get the same value returned. I think that RSA
generates 2 different keys but I use the same container
param.KeyContainerName = "SignatureContainer101";
Is there any way that I can do this without passing any Key? I need it to run locally on the same machine. Or any other suggestions in what I can do to get the same result? This is the code I am using:
public static byte[] SignData(byte[] message)
{
//convert data to be signed in a byte array
byte[] byteSignature2;
CspParameters param = new CspParameters();
//Giving container a name, so we can use the same keys for verifying data.
param.KeyContainerName = "SignatureContainer101";
//Initializing RSACryptoServiceProvider
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
{
//We want RSACryptoServiceProvider to save keys in keystore
//because we want to keep signature generation routine seperate
//from verifying data against signature.
rsa.PersistKeyInCsp = true;
byte[] byteSignature = rsa.SignData(message, "SHA256");
byteSignature2 = new byte[byteSignature.Length];
byteSignature2 = byteSignature;
}
return byteSignature2;
}
public static bool VerifyData(byte[] originalMessage, byte[] signedMessage)
{
bool isSuccess = false;
CspParameters param = new CspParameters();
//Utilizing the same key container which was created during
//signature generation process i.e. in GetDataSignature(string data) function.
//If we don't utilize the same keycontainer, VerifyData will always return false
//because RSACryptoSeriveProvider initialization will generate new keys.
param.KeyContainerName = "SignatureContainer101";
//Initializing RSACryptoServiceProvider
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
{
//verifying data against signature
isSuccess = rsa.VerifyData(originalMessage, "SHA256", signedMessage);
}
return isSuccess;
}
}
I found no problem in you code. It works! This is what I've done: