Reputation: 11
I'm working with a third party attempting to establish digital signature matching in both directions. On my side we are using C# while the third party is using Java. So far I am able to generate a key pairing within OpenSSL and use the private key to generate a signature which they are able to match in their Java environment (C# code below). Going the opposite direction, how do I use the generated public key to create a matching signature?
I've attempted using the same code below simply changing the CngKeyBlobFormat when importing the public key but all other types produce an error.
On both sides the following OpenSSL commands were used to generate the public and private keys:
openssl genrsa -des3 -out my_rsa_key_pair 2048
openssl pkcs8 -topk8 -inform PEM -in my_rsa_key_pair -outform PEM -out private_key.pem -nocrypt
openssl rsa -in my_rsa_key_pair -outform PEM -pubout -out public_key.pem
This is the C# code used to generate a signature from the private key:
byte[] keyBytes = Convert.FromBase64String(privateKey);
byte[] valueBytes = Encoding.UTF8.GetBytes(stringToSign);
byte[] signedBytes;
using (CngKey key = CngKey.Import(keyBytes, CngKeyBlobFormat.Pkcs8PrivateBlob))
using (RSA rsa = new RSACng(key))
{
signedBytes = rsa.SignData(valueBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
return Convert.ToBase64String(signedBytes);
}
Using the public key I need to generate a signature within C# that matches what was generated with the private key. What is the proper way to do this?
Upvotes: 0
Views: 1919
Reputation: 11
There are a lot of posts surrounding signature generation / verification and after hitting on the right key words I found the answer in the following post. Given the above code and method of generating public/private key pairings the answer in this post will successfully validate the signature generated with a private key using only the public key. Leaving this open for anyone searching for both sides of the solution: Verify signature generated with RSA 2048-bit key, SHA256 algorithm and PKCSv1.5 padding
Upvotes: 1