title: How to RSA Encrypt XmlFile in one program and decrypt in another?
body: <p>I have a requirement to encrypt/decrypt a whole XML file. I am using RSA like in <a href="http://msdn.microsoft.com/en-us/library/ms229746.aspx" rel="nofollow">this MSDN post</a> but the issue is that I have to encrypt in one program, an Windows Form program and decrypt in a Windows Service. How will the Windows Service know the RSA key I generated in order to decrypt?</p>
  
  <p>The encrypt/decrypt code is:</p>
  
  <pre><code> public class EncryptDecrpt
      {
          public static void Encrypt(XmlDocument doc, string ElementToEncrypt, string EncryptionElementID, RSA Alg,
              string KeyName)
          {
              try
              {
                  //Check the arguments
                  if (doc == null)
                      throw new ArgumentNullException("doc");
                  if (ElementToEncrypt == null)
                      throw new ArgumentNullException("ElementToEncrypt");
                  if (EncryptionElementID == null)
                      throw new ArgumentNullException("EncryptionElementID");
                  if (Alg == null)
                      throw new ArgumentNullException("Alg");
                  if (KeyName == null)
                      throw new ArgumentNullException("KeyName");
  
  
                  // Find the specified element in the XmlDocument object
                  // and create a new XmlElement object
                  XmlElement elementToEncrypt = doc.GetElementsByTagName(ElementToEncrypt)[0] as XmlElement;
                  if (elementToEncrypt == null)
                      throw new XmlException("The specified element was not found");
  
                  RijndaelManaged sessionKey = null;
  
                  // Create a 256 bit Rijandel key
                  sessionKey = new RijndaelManaged();
                  sessionKey.KeySize = 256;
  
                  EncryptedXml eXml = new EncryptedXml();
  
                  byte[] encryptedElement = eXml.EncryptData(elementToEncrypt, sessionKey, false);
  
                  // Construct an EncryptedData object and populate
                  // it with the desired encryption information
  
                  EncryptedData edElement = new EncryptedData();
                  edElement.Type = EncryptedXml.XmlEncElementUrl;
                  edElement.Id = EncryptionElementID;
  
                  edElement.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncAES256Url);
                  // Encrypt the session key and add it to an EncryptedKey element
                  EncryptedKey ek = new EncryptedKey();
  
                  byte[] encryptedKey = EncryptedXml.EncryptKey(sessionKey.Key, Alg, false);
  
                  ek.CipherData = new CipherData(encryptedKey);
  
                  ek.EncryptionMethod = new EncryptionMethod(EncryptedXml.XmlEncRSA15Url);
  
                  DataReference dRef = new DataReference();
  
                  // Specify the EncryptedData URI
                  dRef.Uri = "#" + EncryptionElementID;
  
                  ek.AddReference(dRef);
  
                  edElement.KeyInfo.AddClause(new KeyInfoEncryptedKey(ek));
  
                  KeyInfoName kin = new KeyInfoName();
  
                  kin.Value = KeyName;
  
                  ek.KeyInfo.AddClause(kin);
  
                  edElement.CipherData.CipherValue = encryptedElement;
  
                  // Replace the element from the original XmlDocument
                  // object with the EncrytedData element
                  EncryptedXml.ReplaceElement(elementToEncrypt, edElement, false);
              }
              catch (Exception e)
              {
                  // rethrow the exception
                  throw e;
              }
  
          }
  
          public static void Decrypt(XmlDocument Doc, RSA Alg, string KeyName)
          {
              // Check the arguments.   
              if (Doc == null)
                  throw new ArgumentNullException("Doc");
              if (Alg == null)
                  throw new ArgumentNullException("Alg");
              if (KeyName == null)
                  throw new ArgumentNullException("KeyName");
  
              // Create a new EncryptedXml object.
              EncryptedXml exml = new EncryptedXml(Doc);
  
              // Add a key-name mapping. 
              // This method can only decrypt documents 
              // that present the specified key name.
              exml.AddKeyNameMapping(KeyName, Alg);
  
              // Decrypt the element.
              exml.DecryptDocument();
  
          }
  
      }
  </code></pre>
  
  <p>The code in the Windows Form program is:</p>
  
  <pre><code> private void SaveForm()
          {
              try
              {
                  string fileName = System.IO.Path.Combine(Application.StartupPath, "alphaService.xml");
                  XDocument doc = new XDocument();
                  XElement xml = new XElement("Info",
                      new XElement("DatabaseServerName", txtServerName.Text),
                      new XElement("DatabaseUserName", txtDatabaseUserName.Text),
                      new XElement("DatabasePassword", txtDatabasePassword.Text),
                      new XElement("ServiceAccount", txtAccount.Text),
                      new XElement("ServicePassword", txtServicePassword.Text),
                      new XElement("RegistrationCode", txtRegistrationCode.Text));
  
                  doc.Add(xml);
                  doc.Save(fileName);
  
                  // Encrypt
                  XmlDocument xmlDoc = new XmlDocument();
                  xmlDoc.Load(fileName);
                  // Create a new CspParameters object to specify
                  // a key container
                  CspParameters cspParams = new CspParameters();
                  cspParams.KeyContainerName = "XML_ENC_RSA_KEY";
  
                  RSACryptoServiceProvider rsaKey = new RSACryptoServiceProvider(cspParams);
  
                  EncryptDecrpt.Encrypt(xmlDoc, "info", "EncryptedElement1", rsaKey, "rsaKey");
  
                  xmlDoc.Save(fileName);
  
                  MessageBox.Show(xmlDoc.OuterXml);
  
                  EncryptDecrpt.Decrypt(xmlDoc, rsaKey, "rsaKey");
  </code></pre>
  
  <p>How will the Windows Service know the RSA key or how can I do this?</p>