Cómo cambiar el parámetro CSP de RsacryptoserviceProvider
-
13-12-2019 - |
Pregunta
Estoy haciendo la firma de RSA-SHA256 XML utilizando la clase SignedXML.Pero el problema es que necesito cambiar CSP para admitir SHA256.
Así es como estoy seleccionando Certificado,
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;
}
Así es como estoy tratando de cambiar el parámetro 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);
El problema es que estoy usando un token de dispositivo USB y dudo que la clave privada no es exportable.Al exportar su lanzamiento de un error " clave, no válido para su uso en estado específico. '.
¿Puede alguien ayudar a hacer esto?
Solución
Si alguien interesado aquí es mi solución, terminé usando otra nueva versión de mi CSP de 3RD Party.La versión CSP que estaba usando era una vieja y yo cambiamos a una nueva versión.Ahora la firma está funcionando.Gracias por tu ayuda.