Decrypt Xml file with asymetric key windows forms and windows service

bongopete

New Member
I'm developping a winforms application using .NET3.5 (C#). And for the protection of the application, I used an Xml file, whitch should should be crypted using an asymetric key.Now, My application has two part, the windows forms part, and the windows service part (who check the authenticity of the application). So, my file will be crypted/decrypted by the two parts. And this is where the problem appear. When the win forms part try to decrypt the file already creapted by the service, an exception occur (Incorrect Data). Here's my code :\[code\]private readonly static string containerName = "ENC_XML_ASY_KEY";private readonly static string keyName = "keyName";public static void EncryptXmlFile(this XmlDocument doc, string elemToEncryptName) { if (doc == null) return; var cspParams = new CspParameters() { KeyContainerName = containerName }; var rsaKey = new RSACryptoServiceProvider(cspParams); var elementToEncrypt = doc.GetElementsByTagName(elemToEncryptName)[0] as XmlElement; if (elementToEncrypt == null) return; // Create a 256 bit Rijndael key. var sessionKey = new RijndaelManaged() { KeySize = 256 }; var eXml = new EncryptedXml(); byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, sessionKey, false); var edElement = new EncryptedData() { Type = EncryptedXml.XmlEncElementUrl, Id = "XmlID", EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url) }; var ek = new EncryptedKey(); var encryptedKey = EncryptedXml.EncryptKey(sessionKey.Key, rsaKey, false); ek.CipherData = http://stackoverflow.com/questions/11260031/new CipherData(encryptedKey); ek.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url); edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek)); // Create a new KeyInfoName element. var kin = new KeyInfoName() { Value = keyName }; // Add the KeyInfoName element to the encryptedKey object. ek.KeyInfo.AddClause(kin); edElement.CipherData.CipherValue = encryptedElement; EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false); }public static void DecryptXmlFile(this XmlDocument doc) { if (doc == null) return; var cspParams = new CspParameters() { KeyContainerName = containerName }; var rsaKey = new RSACryptoServiceProvider(cspParams); var exml = new EncryptedXml(doc); exml.AddKeyNameMapping(keyName, rsaKey); exml.DecryptDocument(); }\[/code\]Thank you in advance
 
Back
Top