在C#项目中保护敏感配置信息如数据库连接字符串、API密钥等至关重要。下面介绍一种最简便、最实用的配置文件保护方法无需复杂依赖直接利用.NET框架内置功能。方案核心使用Windows数据保护API (DPAPI) 或 托管封装1. 最简单实现使用ProtectedData类DPAPI封装DPAPI是Windows内置的数据保护接口无需管理密钥非常适合单机应用。using System.Security.Cryptography; using System.Text; public static class SimpleConfigProtector { // 加密字符串 public static string Encrypt(string plainText) { byte[] plainBytes Encoding.UTF8.GetBytes(plainText); byte[] encryptedBytes ProtectedData.Protect( plainBytes, entropy: null, // 可选的附加熵值增加安全性 scope: DataProtectionScope.CurrentUser // 或 LocalMachine ); return Convert.ToBase64String(encryptedBytes); } // 解密字符串 public static string Decrypt(string encryptedText) { byte[] encryptedBytes Convert.FromBase64String(encryptedText); byte[] plainBytes ProtectedData.Unprotect( encryptedBytes, entropy: null, scope: DataProtectionScope.CurrentUser ); return Encoding.UTF8.GetString(plainBytes); } }2. 配置文件集成方案appsettings.json (原始){ ConnectionStrings: { DefaultConnection: Serverlocalhost;DatabaseMyDB;Usersa;PasswordMyPass123; }, ApiKey: abc123def456 }appsettings.json (加密后){ ConnectionStrings: { DefaultConnection: AQAAANCMnd8BFdERjHoAwE/ClsBAAAAAA0IAAAAAAA... (加密文本) }, ApiKey: AQAAANCMnd8BFdERjHoAwE/ClsBAAAAAA0IAAAAAAA... }配置读取类public class AppSettings { private readonly IConfiguration _configuration; public AppSettings(IConfiguration configuration) { _configuration configuration; } public string GetConnectionString() { var encrypted _configuration.GetConnectionString(DefaultConnection); return SimpleConfigProtector.Decrypt(encrypted); } public string GetApiKey() { var encrypted _configuration[ApiKey]; return SimpleConfigProtector.Decrypt(encrypted); } }3. 一键加密工具控制台应用// ConfigEncryptor.cs class Program { static void Main(string[] args) { Console.WriteLine( 配置文件加密工具 ); Console.Write(输入要加密的文本: ); string plainText Console.ReadLine(); string encrypted SimpleConfigProtector.Encrypt(plainText); Console.WriteLine($加密结果: {encrypted}); Console.Write(是否测试解密? (y/n): ); if (Console.ReadKey().Key ConsoleKey.Y) { string decrypted SimpleConfigProtector.Decrypt(encrypted); Console.WriteLine($\n解密结果: {decrypted}); } } }方案优势与注意事项✅ 优点零密钥管理DPAPI自动处理密钥存储按用户/机器隔离CurrentUser范围防止其他用户解密无需外部依赖仅需System.Security.Cryptography防篡改加密后的Base64字符串无法直接解读⚠️ 注意事项DataProtectionScope选项CurrentUser只有当前登录用户可解密LocalMachine本机所有用户可解密安全性较低部署限制DPAPI加密的内容不能跨计算机解密如需多服务器部署考虑使用证书加密或Azure Key Vault加密提示首次运行时可自动加密配置文件// 启动时检查并加密配置 if (!IsEncrypted(configValue)) { string encrypted SimpleConfigProtector.Encrypt(configValue); // 更新配置文件谨慎操作 }备选方案如需跨平台/跨机器加密如果应用需部署到多台服务器可采用基于证书的加密public static class CertificateProtector { public static string EncryptWithCertificate(string plainText, X509Certificate2 cert) { using RSA rsa cert.GetRSAPublicKey(); byte[] plainBytes Encoding.UTF8.GetBytes(plainText); byte[] encryptedBytes rsa.Encrypt(plainBytes, RSAEncryptionPadding.OaepSHA256); return Convert.ToBase64String(encryptedBytes); } public static string DecryptWithCertificate(string encryptedText, X509Certificate2 cert) { using RSA rsa cert.GetRSAPrivateKey(); byte[] encryptedBytes Convert.FromBase64String(encryptedText); byte[] plainBytes rsa.Decrypt(encryptedBytes, RSAEncryptionPadding.OaepSHA256); return Encoding.UTF8.GetString(plainBytes); } }最佳实践建议敏感配置项仅加密真正敏感的数据密码、密钥普通配置保持明文开发/生产分离开发环境可不加密生产环境强制加密备份原文加密前备份原始配置防止加密失败使用配置中心大型系统建议使用Azure App Configuration/AWS Parameter Store总结对于大多数C#应用DPAPI方案是最简单有效的配置保护方案开发简单仅需10行核心代码维护方便无额外密钥管理负担安全性足满足大部分应用需求无缝集成与ASP.NET Core配置系统完全兼容只需将敏感配置项替换为加密文本在读取时自动解密即可实现防改写、防窥视的安全目标。