2013-06-26 2 views
0

OpenSSL에서 생성 한 개인 RSA 키로 C# .NET 4.0에서 XML 파일에 서명하려고합니다. 같은 내 소스 코드를 찾습니다 : 나는 그것을 호출에 대한 DLL (이름 DBTBeneficiariesCPSMS)과 코드로 내 응용 프로그램의 클래스 (CPSMSXmlGenerator)를 호출하고디지털 서명 인증서를 사용하여 XML 파일 생성

public static void SignXml(String filePath, String certificatePath) 
    { 
     CspParameters cspParams1 = new CspParameters(); 
     cspParams1.KeyContainerName = certificatePath; 
     RSACryptoServiceProvider rsakey = new RSACryptoServiceProvider(cspParams1); 


     XmlDocument xmlDoc = new XmlDocument(); 

     // Load an XML file into the XmlDocument object. 
     xmlDoc.PreserveWhitespace = true; 
     xmlDoc.Load(filePath); 
     SignedXml signedXml = new SignedXml(); 
     CspParameters cspParams = new CspParameters(); 
     cspParams.KeyContainerName = certificatePath; 

     // Create a new RSA signing key and save it in the container. 
     RSACryptoServiceProvider Key = new RSACryptoServiceProvider(cspParams); 

     // 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); 
     KeyInfo keyInfo = new KeyInfo(); 

     // Load the X509 certificate. 


     X509Certificate MSCert = X509Certificate.CreateFromCertFile(certificatePath); 


     // Load the certificate into a KeyInfoX509Data object 
     // and add it to the KeyInfo object. 
     keyInfo.AddClause(new KeyInfoX509Data(MSCert)); 
     keyInfo.AddClause(new RSAKeyValue((RSA)Key)); 

     // Add the KeyInfo object to the SignedXml object. 
     signedXml.KeyInfo = keyInfo; 
     // 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)); 


     xmlDoc.Save(filePath); 
    } 

것은 : 이제

Dim genXml As String = DBTBeneficiariesCPSMS.CPSMSXmlGenerator.getXmlFile1(xml) 

     'Dim appPath As String = Request.PhysicalApplicationPath 
     Dim fullPath As String = Server.MapPath("/XML/") + dataSource + ".xml" 
     lblMessage.Text = fullPath 
     Dim SwFromFile As StreamWriter = New StreamWriter(fullPath) 
     SwFromFile.Write(genXml) 
     SwFromFile.Flush() 
     SwFromFile.Close() 

     CPSMSXmlGenerator.SignXml(fullPath, Server.MapPath("/XML/aua.cer")) 

문제 내 응용 프로그램이 실행될 때마다 'Reference.Uri = ""'에서 중단되고 다음과 같은 오류가 발생합니다. - 오류 : 참조 Uri을 해결하려면 XmlDocument 컨텍스트가 필요합니다.

이 표시되고 디지털 서명 인증서가없는 XML 파일이 생성됩니다.

답변

1

xmDoc는 SignedXml로 전달되지 않습니다. 매개 변수로 전달하면 문제가 해결됩니다.

SignedXml signedXml = new SignedXml(xmlDoc);