Dans .NET, Si vous obtenez votre X509 cert à partir d'un fichier .pfx, comme ceci:
X509Certificate2 certificate = new X509Certificate2(certFile, pfxPassword);
RSACryptoServiceProvider rsaCsp = (RSACryptoServiceProvider) certificate.PrivateKey;
Ensuite, vous pouvez exporter la partie clé publique comme ceci:
rsaCsp.ToXmlString(false);
Le " fausse "partie dit, seulement exporter la pièce publique, ne pas exporter la pièce privée. (Doc pour RSA.ToXmlString)
Et puis dans l'application de vérification, utilisez
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
csp.FromXmlString(PublicKeyXml);
bool isValid = VerifyXml(xmlDoc, rsa2);
Et le VerifyXml appelle CheckSignature()
. Il ressemble à ceci:
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);
}
Merci, c'est exactement ce que je n'ai pas compris. Maintenant, je sais que je dois utiliser un certificat du X509Store pour obtenir le certificat "signataire", et utiliser un fichier .cer comme "vérificateur". – willvv