c#encrypt xml文件

告诉我加密XML文件的最简单方法。 它是一个用于某些配置的文件,不希望人们乱用它。 安全性不是问题,因为它是一种私有工具。

如果您不关心安全性,只需将文件与哈希一起保存即可。 即:

your.xml和your.xml.hash

例如,您可以使用System.Security.Cryptography.MD5Managed。 它只是保存xml文件,然后保存文件本身的哈希值。 阅读时,只需计算哈希值,与保存的值进行比较,然后将xml文件作为常规文件使用。

当然,xml文件中的信息未加密,可以读取,但如果您编辑该文件,则哈希将不正确,您的程序将发现该尝试。 把事情简单化 :)

如果您只是想让它更难修改,请通过DeflateStream发送它。 作为额外的好处,文件将更小。

DPAPI是保护Windows系统中的东西的最简单方法 – 请参阅ProtectedData.Protect作为初学者。

我可能只是在读取/写入之前通过这个包装DPAPI的类运行整个文件。 生成的输出被编码,因此可以将其写为文本文件:

using System; using System.Security.Cryptography; using System.Text; ///  /// used for encryption and decryption ///  public static class DataProtector { private const string EntropyValue = "secret"; ///  /// Encrypts a string using the DPAPI. ///  /// The string to encrypt. /// encrypt data public static string EncryptData(string stringToEncrypt) { byte[] encryptedData = ProtectedData.Protect(Encoding.Unicode.GetBytes(stringToEncrypt), Encoding.Unicode.GetBytes(EntropyValue), DataProtectionScope.LocalMachine); return Convert.ToBase64String(encryptedData); } ///  /// Decrypts a string using the DPAPI. ///  /// The string to decrypt. /// decrypted data public static string DecryptData(string stringToDecrypt) { byte[] decryptedData = ProtectedData.Unprotect(Convert.FromBase64String(stringToDecrypt), Encoding.Unicode.GetBytes(EntropyValue), DataProtectionScope.LocalMachine); return Encoding.Unicode.GetString(decryptedData); } }