SignedXML.CheckSignature 개인 키로 확인하는 경우에만 TRUE를 반환합니다.
-
20-12-2019 - |
문제
나는 몇 가지 유사한 질문을 보았지만 내가 묻는 것을 정확히 주소하지는 않았지만 아무도 없었습니다.
XML에 서명 한 다음 C #을 사용하여 공개 키로 확인하려고합니다.
키가있는 XML에 서명 한 다음 키를 XML로 내보내십시오. 그런 다음 키와 서명 된 XML을 다른 컴퓨터로 가져오고, rsa.FromXmlString(doc.InnerXml)
를 사용하여 키를 가져 와서 XML 서명을 확인하십시오.
Public 및 Private 키를 rsa.ToXmlString(True)
를 사용하여 XML로 내보낼 경우 작동합니다. 그러나 rsa.ToXmlString(False)
를 사용하여 공개 키 만 내보내고 싶습니다. 공개 키 만 내보내고 두 번째 컴퓨터에서 가져오고 XML 서명의 유효성을 검사하려고하면 서명이 유효하지 않습니다.
먼저 공개 키만으로 서명 된 XML을 확인할 수 있어야합니까?
두 번째, 그것이 사실이라면, 내 검증 XML 함수 만 PUB / PRUB 키 쌍으로 만 작동하며 공개 키 만 사용하지 않는 이유는 무엇입니까?
이 문제를 디버깅하는 방법에 대한 지혜가 있습니까? signedXml.CheckSignature(Key);
가 실패한 이유에 대한 정보를 제공하지 않기 때문에 다른 할 일이 확실하지 않습니다.
내 가져 오기 키, 내보내기 키, 서명 XML 및 검증 XML 기능이 아래에 있습니다. 더 많은 정보가 필요하면 알려주세요.
public static void ImportKeyFromFile(string ContainerName, string inputFile)
{
try
{
// Create new XmlDocument.
XmlDocument doc = new XmlDocument();
// Load XML Document.
doc.Load(inputFile);
// Create the CspParameters object and set the key container
// name used to store the RSA key pair.
CspParameters cp = new CspParameters();
cp.KeyContainerName = ContainerName;
// Create a new instance of RSACryptoServiceProvider that accesses
// the key container MyKeyContainerName.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
// Get RSA Parameters from xml document.
rsa.FromXmlString(doc.InnerXml);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static void ExportKeyToFile(string ContainerName, string outputPath, bool private_key)
{
try
{
// Create the CspParameters object and set the key container
// name used to store the RSA key pair.
CspParameters cp = new CspParameters();
cp.KeyContainerName = ContainerName;
// Create a new instance of RSACryptoServiceProvider that accesses
// the key container MyKeyContainerName.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp);
// Create new XmlDocument.
XmlDocument doc = new XmlDocument();
// Store rsa key.
doc.InnerXml = rsa.ToXmlString(private_key);
// Save Document.
doc.Save(outputPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static Boolean VerifyXml(XmlDocument Doc, RSA Key)
{
// Check arguments.
if (Doc == null)
throw new ArgumentException("Doc");
if (Key == null)
throw new ArgumentException("Key");
// Create a new SignedXml object and pass it
// the XML document class.
SignedXml signedXml = new 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.");
}
// Load the first <signature> node.
signedXml.LoadXml((XmlElement)nodeList[0]);
// Check the signature and return the result.
return signedXml.CheckSignature(Key);
}
public static void SignXml(XmlDocument xmlDoc, RSA Key)
{
// Check arguments.
if (xmlDoc == null)
throw new ArgumentException("xmlDoc");
if (Key == null)
throw new ArgumentException("Key");
// Create a SignedXml object.
SignedXml signedXml = new SignedXml(xmlDoc);
// Add the key to the SignedXml document.
signedXml.SigningKey = Key;
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "";
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Add the reference to the SignedXml object.
signedXml.AddReference(reference);
// Compute the signature.
signedXml.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document.
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
}
. 해결책
문제는 키 컨테이너에 공개 키를 저장할 수 없기 때문입니다.공개 키로 서명을 확인하려면 rsa.FromXmlString
를 사용하여 XML에서 키를 가져 와서 RSA를 확인 서명 기능에 직접 전달해야합니다.키 컨테이너에 공개 키를 저장하고 나중에 검색을 시도하면 새 키를 만드는 것만으로 만듭니다.
공개 키를 저장하는 방법기계 수준의 RSA 키 컨테이너