C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C#配置文件加密解密

C#配置文件加密解密的实现方案

作者:猫猫头不加班

在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": "Server=localhost;Database=MyDB;User=sa;Password=MyPass123;"
  },
  "ApiKey": "abc123def456"
}

appsettings.json (加密后)

{
  "ConnectionStrings": {
    "DefaultConnection": "AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAAA0IAAAAAAA... (加密文本)"
  },
  "ApiKey": "AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAAA0IAAAAAAA..."
}

配置读取类

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

方案优势与注意事项

优点

注意事项

DataProtectionScope选项

部署限制

加密提示:首次运行时可自动加密配置文件

// 启动时检查并加密配置
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);
    }
}

最佳实践建议

  1. 敏感配置项:仅加密真正敏感的数据(密码、密钥),普通配置保持明文
  2. 开发/生产分离:开发环境可不加密,生产环境强制加密
  3. 备份原文:加密前备份原始配置,防止加密失败
  4. 使用配置中心:大型系统建议使用Azure App Configuration/AWS Parameter Store

总结

对于大多数C#应用,DPAPI方案是最简单有效的配置保护方案:

只需将敏感配置项替换为加密文本,在读取时自动解密,即可实现"防改写、防窥视"的安全目标。

以上就是C#配置文件加密解密的实现方案的详细内容,更多关于C#配置文件加密解密的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文