I am setting up AS2 communication for signing files to a customer. As I understand signing to work through AS2 communication, we will sign the data we send with our private key, and they will verify it with our public key.
My problem: My IT department has given me a .cer and a .key file. The .cer file has no private key in it, and obviously the .key file is the private key. The customer will add the .cer file to their trusted root to verify our messages. I am having trouble understanding how to sign my data with the .key file. It's not something I can add to a Personal certificate store, so I can't simply get the certificate and do this:
//Open my Personal cert store.
X509Store my = new X509Store(StoreName.My, StoreLocation.LocalMachine);
my.Open(OpenFlags.ReadOnly);
RSACryptoServiceProvider csp = null;
foreach (X509Certificate2 cert in my.Certificates)
{
if (cert.Subject.Contains("My certificate subject"))
{
// We found it.
// Get its associated CSP and private key
csp = (RSACryptoServiceProvider)cert.PrivateKey;
}
}
// Hash the data
SHA256Managed sha256 = new SHA256Managed();
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] hash = sha256.ComputeHash(arMessage);
// Sign the hash
return csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA256"));
How do I get my private key as a RSACryptoServiceProvider directly using the .key file, since a .key file can't be stored in the certificate store?