【问题标题】:how i can encrypt a whole xml file by c#我如何通过 c# 加密整个 xml 文件
【发布时间】:2012-07-29 11:13:56
【问题描述】:

在我的项目中,我编写了两个方法来加密和解密我的 xml 文件中的一些字符串 但知道我想加密整个 xml 文件

我想编辑方法来加密和解密整个文件而不是字符串

  public static string Encrypt(string plainText)
          {
                byte[] initVectorBytes = Encoding.ASCII.GetBytes("teto1620@#$%asdf");
                byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
                byte[] keyBytes = Encoding.Unicode.GetBytes("_+)&qwer9512popo");
                RijndaelManaged symmetricKey = new RijndaelManaged();
                symmetricKey.Mode = CipherMode.CBC;
                ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
                cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                cryptoStream.FlushFinalBlock();
                byte[] cipherTextBytes = memoryStream.ToArray();
                memoryStream.Close();
                cryptoStream.Close();
                string cipherText = Convert.ToBase64String(cipherTextBytes);
                return cipherText;
          }
          public static string Decrypt(string cipherText)
          {
                byte[] initVectorBytes = Encoding.ASCII.GetBytes("teto1620@#$%asdf");
                byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
                byte[] keyBytes = Encoding.Unicode.GetBytes("_+)&qwer9512popo");
                RijndaelManaged symmetricKey = new RijndaelManaged();
                symmetricKey.Mode = CipherMode.CBC;
                ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
                MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
                CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
                byte[] plainTextBytes = new byte[cipherTextBytes.Length];
                int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                memoryStream.Close();
                cryptoStream.Close();
                string plainText = Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
                return plainText;
          }

【问题讨论】:

标签: c# xml encryption


【解决方案1】:

File.ReadAllText() 返回一个字符串,因此您可以使用普通字符串方法对ReadAllText() 进行加密,然后使用File.WriteAllText() 将加密版本写回文件。

但是对于大文件,您可以改为逐行读取和处理。例如

using (StreamReader sr = new StreamReader("xmlfile.txt")) 
{
    string line;
    while ((line = sr.ReadLine()) != null) 
    {
         File.WriteAllText("mynewxmfile.xml", MyEncryptMethod(line));
    }
}

【讨论】:

  • 如果您的 XML 文件很大,请不要这样做。
【解决方案2】:

非常简单,将您的 XML 转换为字符串并将其传递给加密/解密函数。 您使用什么类来创建 XML?如果您使用的是 XMLDocument,那么可以使用以下函数将您的 XML 转换为字符串

public string ConvertToString(XmlDocument xml)
{
    StringWriter sw = new StringWriter();
    XmlTextWriter tx = new XmlTextWriter(sw);
    xml.WriteTo(tx);
    string str = sw.ToString(); 
    return str;
}

同样,您可以在解密时将解密后的字符串转换为 XML,如下所示

XmlDocument doc = new XmlDocument();
doc.LoadXml(decruptString);

【讨论】:

  • 如果已知 XML 文件很小,这将是一个选项。请注意,您必须注意生成的 string 的字符编码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 2010-12-26
  • 1970-01-01
  • 2015-08-09
相关资源
最近更新 更多