如何解密加密后存储的XML元素

本文关键字:存储 XML 元素 加密 何解密 解密 | 更新日期: 2023-09-27 18:14:41

我试图在保存之前加密XML元素。

下面是获取值并保存的代码。

 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");
现在我有一个私有字符串(代码如下所示),它可以在保存 之前加密内部xml内容。
 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;
    }

我传递innerxml内容(如下所示),clearText字符串加密内容。

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

现在我可以保存加密的内容了。

现在我如何解密和加载xml文档成可读的格式?

如何解密加密后存储的XML元素

您当前使用以下步骤加密字符串:

  1. 将字符串编码为Unicode
  2. 加密Unicode字节数组
  3. 将加密字节数组转换为Base64

解密,您只需颠倒顺序:

  1. 将Base64转换为字节数组
  2. 解密字节数组
  3. 解码Unicode字节数组为字符串
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());
            }
        }
    }
}

您可以使用与encrypt相同的代码,只需更改两次

  1. 创建字节数组
  2. 用CreateDecryptor
  3. 代替CreateEncryptor

试试下面的

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;
     }