static void decipher(string filename, XmlDocument xmlDoc, List <RSA> Keys) { // Create a new EncryptedXml object. EncryptedXml exml = new EncryptedXml(xmlDoc); // Add a key-name mapping. // This method can only decrypt documents // that present the specified key name. int keyid = 0; foreach (RSA Alg in Keys) { try { exml.ClearKeyNameMappings(); Trace.WriteLine("Trying to decrypt with keyid " + keyid++); exml.AddKeyNameMapping("rsaKey", Alg); // Decrypt the element. exml.DecryptDocument(); return; } catch (Exception ex) { Trace.WriteLine("When decoding the document - trying next key: " + ex.Message); } } Trace.WriteLine("The program tried to use " + keyid + " keys"); throw new PingCastleDataException(filename, "Unable to find a key in the configuration which can decrypt the document"); }