I'm trying to write an app that stores that will sign xml files using certificates stored in a database (public and private keys).
The issue I'm having is that whenever I load the pfx file from a file (not from the database), and try to compute the signature of the xml using:
var cert = new X509Certificate2(@"c:\temp\cert.pfx");
var signedXml = new SignedXml(doc) {SigningKey = cert};
signedXml.ComputeSignature();
I get the following exception:
An exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll but was not handled in user code
Additional information: The system cannot find the file specified.
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash, Int32 cbHash, ObjectHandleOnStack retSignature)
at System.Security.Cryptography.Utils.SignValue(SafeKeyHandle hKey, Int32 keyNumber, Int32 calgKey, Int32 calgHash, Byte[] hash)
at System.Security.Cryptography.RSACryptoServiceProvider.SignHash(Byte[] rgbHash, Int32 calgHash)
at System.Security.Cryptography.RSAPKCS1SignatureFormatter.CreateSignature(Byte[] rgbHash)
at System.Security.Cryptography.Xml.SignedXml.ComputeSignature()
<snip>
... my code
</snip>
I created the pfx using the following:
makecert -r -pe -n "CN=CompanyXYZ Server" -b 01/01/2007 -e 01/01/2010 -sky exchange Server.cer -sv Server.pvk
pvk2pfx.exe -pvk Server.pvk -spc Server.cer -pfx Server.pfx
Just to clarify this further. It is being used in an ASP.NET website, and I can't set "Load User Profile" to true.