En .NET, Si obtiene su certificado X509 desde un archivo .pfx, así:
X509Certificate2 certificate = new X509Certificate2(certFile, pfxPassword);
RSACryptoServiceProvider rsaCsp = (RSACryptoServiceProvider) certificate.PrivateKey;
A continuación, puede exportar la parte de clave pública de este modo:
rsaCsp.ToXmlString(false);
El " falso ", dice la parte, solo exporta la pieza pública, no exporta la pieza privada. (Doc para RSA.ToXmlString)
Y luego, en la aplicación verificación, utilice
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
csp.FromXmlString(PublicKeyXml);
bool isValid = VerifyXml(xmlDoc, rsa2);
Y el VerifyXml llama CheckSignature()
. Se ve algo como esto:
private Boolean VerifyXml(XmlDocument Doc, RSA Key)
{
// Create a new SignedXml object and pass it
// the XML document class.
var signedXml = new System.Security.Cryptography.Xml.SignedXml(Doc);
// Find the "Signature" node and create a new XmlNodeList object.
XmlNodeList nodeList = Doc.GetElementsByTagName("Signature");
// Throw an exception if no signature was found.
if (nodeList.Count <= 0)
{
throw new CryptographicException("Verification failed: No Signature was found in the document.");
}
// Though it is possible to have multiple signatures on
// an XML document, this app only supports one signature for
// the entire XML document. Throw an exception
// if more than one signature was found.
if (nodeList.Count >= 2)
{
throw new CryptographicException("Verification failed: More that one signature was found for the document.");
}
// Load the first <signature> node.
signedXml.LoadXml((XmlElement)nodeList[0]);
// Check the signature and return the result.
return signedXml.CheckSignature(Key);
}
Gracias, eso es exactamente lo que no tenía claro. Ahora sé que tengo que usar un certificado de X509Store para obtener el certificado "firmante" y utilizar un archivo .cer como "verificador". – willvv