I want to encrypt and decrypt different elements of xml file with different RSACryptoServiceProvider keys
RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);
string publicKey = rsaKey.ToXmlString(false);
RSACryptoServiceProvider rsaKey2 = new RSACryptoServiceProvider(cspParams2);
string publicKey2 = rsaKey2.ToXmlString(false);
ex: I encrypt <creditcard> element with publicKey and I encrypt <personal> element with
publicKey2.
Here is my data file:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<creditcard>
<number>19834209</number>
<expiry>02/02/1890</expiry>
</creditcard>
<name>
<first>abc</first>
<mi>v</mi>
<last>xyz</last>
</name>
<personal>
<dob>01011790</dob>
<gender>female</gender>
</personal>
</root>
I am able to successfully encrypt two elements with two different keys.
However the problem comes in Decrypt
public static void Decrypt(XmlDocument Doc, RSA Alg, string KeyName)
{
EncryptedXml exml = new EncryptedXml(Doc);
exml.AddKeyNameMapping(KeyName, Alg);
exml.DecryptDocument();
}
I call the above code like this:
Decrypt(xmlDoc, rsaKey, publicKey);
Decrypt(xmlDoc, rsaKey, publicKey2);
I get Exception: "Unable to retrieve the decryption key."
However if I Encrypt both elements with same publicKey then Decrypt works.
This means If I encrypt elements of a document with single key Decrypt works. However If I encrypt elements of a document with multiple keys Decrypt fails. I can see why - because the Decrypt is working on the whole document. I want to be able to Decrypt each data element seperately with corresponding Encrypted key. MSDN article says it is possible. But I cannot locate examples.
Can someone please help?