我正在使用 SignedXML 类进行 rsa-sha256 xml 签名。但问题是我需要更改 CSP 以支持 sha256。
这就是我选择证书的方式,
public X509Certificate2 GetCertificateFromStore()
{
X509Store st = new X509Store(StoreName.My, StoreLocation.CurrentUser);
st.Open(OpenFlags.ReadOnly);
X509Certificate2Collection col = st.Certificates.Find(X509FindType.FindByTimeValid, (object)DateTime.Now, false);
X509Certificate2 x509Certificate =null;
X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(col, "Certificate", "Select single certificate to sign", X509SelectionFlag.SingleSelection);
if (sel.Count > 0)
{
X509Certificate2Enumerator en = sel.GetEnumerator();
en.MoveNext();
x509Certificate = en.Current;
}
st.Close();
//x509Certificate.s
return x509Certificate;
}
这就是我尝试更改 CSP 参数的方式。
byte[] privateKeyBlob;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa = cert.PrivateKey as RSACryptoServiceProvider;
try
{
privateKeyBlob = rsa.ExportCspBlob(true);
}
catch
{
throw new ApplicationException("Private key fails to export");
}
// To use the RSA-SHA256 the CryptoAPI needs to select a special CSP: Microsoft Enhanced RSA and AES Cryptographic Provider
// By reinstantiating a CSP of type 24 we ensure that we get the right CSP.
CspParameters cp = new CspParameters(24);
rsa = new RSACryptoServiceProvider(cp);
rsa.ImportCspBlob(privateKeyBlob);
signer.SigningKey = rsa;
signer.KeyInfo = getKeyInfo(signer, cert);
问题是我正在使用 USB 设备令牌,我怀疑私钥不可导出。在导出它时抛出一个错误'密钥在指定状态下无效。'。
任何人都可以帮助如何做到这一点?