我如何用c#加密整个xml文件

时间:2012-07-29 11:13:56

标签: c# xml encryption

在我的项目中,我编写了tow方法来加密和解密我的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;
          }

2 个答案:

答案 0 :(得分:3)

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

对于大型文件,您可以逐行阅读和处理。 e.g。

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

答案 1 :(得分:1)

非常简单,将XML转换为字符串并将其传递给加密/解密函数。 您使用哪些类来创建XML? iF您正在使用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);