I used EncryptedXml to decrypt my statements. Here is my code
EncryptedXmlWithPreconfiguredAsymmetricKey encXml = new EncryptedXmlWithPreconfiguredAsymmetricKey (_xmlDoc,_certificate); while (_xmlDoc.GetElementsByTagName("EncryptedData").Count > 0) { XmlElement encryptedDataElement = _xmlDoc.GetElementsByTagName("EncryptedData")[0] as XmlElement; EncryptedData encryptedData = new EncryptedData(); encryptedData.LoadXml(encryptedDataElement); SymmetricAlgorithm symmKey = encXml.GetDecryptionKey(encryptedData, encryptedData.EncryptionMethod.KeyAlgorithm); symmKey.IV = encXml.GetDecryptionIV(encryptedData, encryptedData.EncryptionMethod.KeyAlgorithm); symmKey.Padding = encXml.Padding; symmKey.Mode = encXml.Mode; byte[] decryptedData = encXml.DecryptData(encryptedData, symmKey); encXml.ReplaceData(encryptedDataElement, decryptedData); }
I also overridden the GetDecryptionKey () method to use the predefined certificate
public class EncryptedXmlWithPreconfiguredAsymmetricKey : EncryptedXml { public readonly X509Certificate2 _encryptionCert; public EncryptedXmlWithPreconfiguredAsymmetricKey(XmlDocument xmlDoc, X509Certificate2 encryptionCert) : base(xmlDoc) { _encryptionCert = encryptionCert; } public override SymmetricAlgorithm GetDecryptionKey(EncryptedData encryptedData, string symmetricAlgorithmUri) { if (encryptedData == null) throw new ArgumentNullException("encryptedData"); if (encryptedData.KeyInfo == null) return null; IEnumerator keyInfoEnum = encryptedData.KeyInfo.GetEnumerator(); KeyInfoRetrievalMethod kiRetrievalMethod; KeyInfoName kiName; KeyInfoEncryptedKey kiEncKey; EncryptedKey ek = null; while (keyInfoEnum.MoveNext()) { kiName = keyInfoEnum.Current as KeyInfoName; kiRetrievalMethod = keyInfoEnum.Current as KeyInfoRetrievalMethod; kiEncKey = keyInfoEnum.Current as KeyInfoEncryptedKey; if (kiEncKey != null) { ek = kiEncKey.EncryptedKey; break; } }
source share