Мне удалось зашифровать XML-документ, зашифровав элемент, а затем заменив элемент зашифрованными данными.A показано в примере кода ниже.
Public Shared Sub Encrypt(ByVal textReader As TextReader, ByVal textWriter As TextWriter, ByVal certificateName As String)
Dim xmlDoc As New XmlDocument()
xmlDoc.Load(textReader)
' Add the schema from Resources
AddSchema(xmlDoc)
' Get all elements to encrypt
Dim elementsToEncrypt As List(Of XmlElement) = FindElementsToEncrypt(xmlDoc.DocumentElement)
' Get the certificate
Dim certificate As X509Certificate2 = FindTrustedCertificate(certificateName)
If certificate Is Nothing Then
Throw New ArgumentException(String.Format("Certificate {0} not found", certificateName), "certificateName")
End If
Dim xmlEncrypter As New EncryptedXml(xmlDoc)
' Itterate all elelemts to encrypt
For Each elementToEncrypt As XmlElement In elementsToEncrypt
' Encrypt the elements with the given certificate
Dim encryptedData As EncryptedData = xmlEncrypter.Encrypt(elementToEncrypt, certificate)
EncryptedXml.ReplaceElement(elementToEncrypt, encryptedData, False)
Next
' Return the encrypted XmlDocument
xmlDoc.Save(textWriter)
End Sub
Это приводит к xml, где элемент имеет EncryptedData, содержащий сертификат X509, например (я удалил массовые данные):
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<X509Data>
<X509Certificate>MIIFU......</X509Certificate>
</X509Data>
</KeyInfo>
<CipherData>
<CipherValue>dQOzeY81I9XAz......</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>qfmuwmyrpMOK.....</CipherValue>
</CipherData>
</EncryptedData>
Если я зашифрую 2 из этих элементов, один и тот же сертификат X509 будет включен дважды.
Кто-нибудь знает решение, в котором, например, указан сертификат?
Спасибо,
Берт Хисбин