使用私钥c#解密XML#
我得到了我需要解密的XML。 xml使用公钥加密,发件人从我的证书存储区中的p12证书中获取该公钥。它基于W3c的XML加密语法和处理标准。
我如何解密这个我使用C#4.5。我已经查看了msdn并尝试了其他没有运气的例子。 我是否必须从证书中提取私钥?在XML下面有一个Java应该可行的例子,但我不知道如何获得私钥。
<?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>
这是一个适用于Java的示例:
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");
}
}