Trong .NET, Nếu bạn nhận cert X509 của bạn từ một file pfx, như thế này:
X509Certificate2 certificate = new X509Certificate2(certFile, pfxPassword);
RSACryptoServiceProvider rsaCsp = (RSACryptoServiceProvider) certificate.PrivateKey;
Sau đó, bạn có thể xuất khẩu phần khóa công khai như vậy:
rsaCsp.ToXmlString(false);
Các " giả "phần nói, chỉ xuất khẩu các mảnh công cộng, không xuất khẩu phần tư nhân. (Doc cho RSA.ToXmlString)
Và sau đó trong việc áp dụng kiểm định, sử dụng
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
csp.FromXmlString(PublicKeyXml);
bool isValid = VerifyXml(xmlDoc, rsa2);
Và VerifyXml gọi CheckSignature()
. Nó trông giống như sau:
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);
}
Cảm ơn bạn, đó là chính xác những gì tôi không rõ ràng. Bây giờ tôi biết rằng tôi phải sử dụng chứng chỉ từ X509Store để lấy chứng chỉ "người ký" và sử dụng tệp .cer làm "người xác minh". – willvv