Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用私钥c解密XML#_C#_Xml_Encryption - Fatal编程技术网

C# 用私钥c解密XML#

C# 用私钥c解密XML#,c#,xml,encryption,C#,Xml,Encryption,用私钥c解密XML# 我得到了需要解密的XML。xml使用公钥加密,发送方从我的证书存储中的p12证书获得公钥。它基于W3c的XML加密语法和处理标准 如何解密我使用的是C#4.5。我查看了msdn并尝试了其他示例,但没有成功。 我必须从证书中提取私钥吗?在XML下面有一个Java示例,它应该可以工作,但我不知道XML是如何获得私钥的 <?xml version="1.0" encoding="UTF-8" standalone="no" ?> <urn:PersonSamt

用私钥c解密XML#

我得到了需要解密的XML。xml使用公钥加密,发送方从我的证书存储中的p12证书获得公钥。它基于W3c的XML加密语法和处理标准

如何解密我使用的是C#4.5。我查看了msdn并尝试了其他示例,但没有成功。 我必须从证书中提取私钥吗?在XML下面有一个Java示例,它应该可以工作,但我不知道XML是如何获得私钥的

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<urn:PersonSamtykkeOpret_O xmlns:urn="urn:oio:skat:hentselv:ws:1.0.1">
<xenc:EncryptedData xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" Type="http://www.w3.org/2001/04/xmlenc#Content">
  <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /> 
 <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<xenc:EncryptedKey>
  <xenc:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /> 
<xenc:CipherData>
  <xenc:CipherValue>cdvDSYkcUTu59nEqHg9GhMxS6yGK7ZLVdghluipkUT7PxwwJ2+N9HZcIg4XI26y5Q0mzNg6IixEB….. xenc:CipherValue> 
  </xenc:CipherData>
  </xenc:EncryptedKey>
  </ds:KeyInfo>
<xenc:CipherData>
  <xenc:CipherValue>D/aMUyUAzuaEJSLfiOLEEOshXoUQ5yNRUxdVFq6Jlg1+p6emUephz86THWXiy00EC+zgD9olqsm6…. </xenc:CipherValue> 
  </xenc:CipherData>
  </xenc:EncryptedData>
  </urn:PersonSamtykkeOpret_O>

我找到了解决办法。对称密钥是用我的私钥加密的。因此,在对对称密钥进行解密后,我可以解密xml的其余部分
public void decryptXMLDocument(Document xmlDocToDecrypt, 
PrivateKey decryptionPrivateKey) throws Exception { 
try { 
// Find Encryption Element i XML Document 
Element _encryptedDataElement = 
(Element) xmlDocToDecrypt.getElementsByTagNameNS( 
EncryptionConstants.EncryptionSpecNS, EncryptionConstants._TAG_ENCRYPTEDDATA).item(0); 
System.out.print("EcryptedData element found in Document"); 
// Create XMLCipher to do the decryption 
XMLCipher _xmlCipher = XMLCipher.getInstance(); 
_xmlCipher.init(XMLCipher.DECRYPT_MODE, null); 
_xmlCipher.setKEK(decryptionPrivateKey); 
System.out.print("XMLChiper initeret"); 
/* 
* The following doFinal call replaces the encrypted data with 
* decrypted contents in the document. 
*/ 
_xmlCipher.doFinal(xmlDocToDecrypt, _encryptedDataElement); 
System.out.print("Document decrypted"); 

} catch (Exception e) { 
System.out.print("Failed to decrypt document :: " + e); 
throw new Exception("Failed to decrypt document"); 
} 
}