Learner
Learner

Reputation: 353

how to decrypt xml elements stored after encryption

I am trying to encrypt XML elements before saving.

Below is the code to get values and save.

 XmlDocument xmlDoc = new XmlDocument();
 xmlDoc.Load("../../Info.xml");
 XmlElement ParentElement = xmlDoc.CreateElement("Details");
 XmlElement userID = xmlDoc.CreateElement("ID");
 userID.InnerText = strName;
 XmlElement userPwd = xmlDoc.CreateElement("Pwd");
 userPwd.InnerText = strPwd;
 XmlElement startDate = xmlDoc.CreateElement("Start");
 startDate.InnerText = dtStart.ToString();
 XmlElement expiryDate = xmlDoc.CreateElement("Expiry");
 expiryDate.InnerText = dtExpiry.ToString();
 ParentElement.AppendChild(userID);
 ParentElement.AppendChild(userPwd);
 ParentElement.AppendChild(startDate);
 ParentElement.AppendChild(expiryDate);
 xmlDoc.DocumentElement.AppendChild(ParentElement);
 xmlDoc.Save("../../Info.xml");

Now I have a private string (code shown below) which can encrypt the inner xml contents before save

 private string Encrypt(string clearText)
    {
        string EncryptionKey = "MAKV2SPBNI99212";
        byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                clearText = Convert.ToBase64String(ms.ToArray());
            }
        }
        return clearText;
    }

I am passing the innerxml contents(shown below) and clearText string encrypts the contents.

string str = Encrypt(xmlUserDoc.InnerXml);
File.WriteAllText("../../Info.xml", str);

Now I am able to save the encrypted contents.

Now how can I decrypt and load the xml doc into readable format?

Upvotes: 0

Views: 395

Answers (2)

A3006
A3006

Reputation: 1079

You can use the same code as encrypt with two changes

  1. while creating byte array
  2. Instead of CreateEncryptor use CreateDecryptor

Try following

public static string Decrypt(string sEncryptedText)
     {
         string EncryptionKey = "MAKV2SPBNI99212";
         byte[] cipherBytes = Convert.FromBase64String(sEncryptedText);
         using (Aes encryptor = Aes.Create())
         {
             Rfc2898DeriveBytes pdb = new
                 Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
             encryptor.Key = pdb.GetBytes(32);
             encryptor.IV = pdb.GetBytes(16);
             using (MemoryStream ms = new MemoryStream())
             {
                 using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                 {
                     cs.Write(cipherBytes, 0, cipherBytes.Length);
                     cs.Close();
                 }
                 sEncryptedText = Encoding.Unicode.GetString(ms.ToArray());
             }
         }
         return sEncryptedText;
     }

Upvotes: 0

Manfred Radlwimmer
Manfred Radlwimmer

Reputation: 13394

You currently encrypt your string with the following steps:

  1. Encode string to Unicode
  2. Encrypt Unicode Byte array
  3. Convert Encrypted Byte array to Base64

To decrypt it, you simply have to reverse the order:

  1. Convert Base64 to Byte array
  2. Decrypt Byte array
  3. Decode Unicode Byte array to string
private string Decrypt(string encrypted)
{
    byte[] encryptedBytes = Convert.FromBase64String(encrypted);

    string EncryptionKey = "MAKV2SPBNI99212";

    using (Aes encryptor = Aes.Create())
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, 
          new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        encryptor.Key = pdb.GetBytes(32);
        encryptor.IV = pdb.GetBytes(16);
        using (MemoryStream ms = new MemoryStream(encryptedBytes))
        {
            using (CryptoStream cs = new CryptoStream(ms, 
              encryptor.CreateDecryptor(), CryptoStreamMode.Read))
            {
                MemoryStream buffer = new MemoryStream();
                cs.CopyTo(buffer);

                return Encoding.Unicode.GetString(buffer.ToArray());
            }
        }
    }
}

Upvotes: 1

Related Questions