欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > C#中的加密和解密类设计

C#中的加密和解密类设计

2025/5/9 8:02:32 来源:https://blog.csdn.net/zkmrobot/article/details/145728325  浏览:    关键词:C#中的加密和解密类设计

下面是一个基于 ProtectedData 类和 DataProtectionScope 的静态类 Cypher 的设计,用于加密和解密数据。该类封装了加密和解密的逻辑,并提供了简单易用的接口。

设计目标
封装加密和解密逻辑:

提供静态方法 Encrypt 和 Decrypt,分别用于加密和解密数据。

支持字符串和字节数组的加密和解密。

支持自定义熵:

允许用户提供可选的熵(optionalEntropy)以增加安全性。

异常处理:

捕获并处理加密和解密过程中可能抛出的异常。

灵活性:

支持 DataProtectionScope.CurrentUser 和 DataProtectionScope.LocalMachine。

实现代码

using System;
using System.Security.Cryptography;
using System.Text;public static class Cypher
{/// <summary>/// 加密字符串。/// </summary>/// <param name="data">要加密的字符串。</param>/// <param name="scope">数据保护范围(默认当前用户)。</param>/// <param name="optionalEntropy">可选的熵(增加安全性)。</param>/// <returns>加密后的 Base64 字符串。</returns>public static string Encrypt(string data, DataProtectionScope scope = DataProtectionScope.CurrentUser, byte[] optionalEntropy = null){if (string.IsNullOrEmpty(data))throw new ArgumentNullException(nameof(data));try{byte[] dataBytes = Encoding.UTF8.GetBytes(data);byte[] encryptedBytes = ProtectedData.Protect(dataBytes, optionalEntropy, scope);return Convert.ToBase64String(encryptedBytes);}catch (Exception ex){throw new CryptographicException("加密失败。", ex);}}/// <summary>/// 解密字符串。/// </summary>/// <param name="encryptedData">加密后的 Base64 字符串。</param>/// <param name="scope">数据保护范围(默认当前用户)。</param>/// <param name="optionalEntropy">可选的熵(必须与加密时一致)。</param>/// <returns>解密后的原始字符串。</returns>public static string Decrypt(string encryptedData, DataProtectionScope scope = DataProtectionScope.CurrentUser, byte[] optionalEntropy = null){if (string.IsNullOrEmpty(encryptedData))throw new ArgumentNullException(nameof(encryptedData));try{byte[] encryptedBytes = Convert.FromBase64String(encryptedData);byte[] decryptedBytes = ProtectedData.Unprotect(encryptedBytes, optionalEntropy, scope);return Encoding.UTF8.GetString(decryptedBytes);}catch (Exception ex){throw new CryptographicException("解密失败。", ex);}}/// <summary>/// 加密字节数组。/// </summary>/// <param name="data">要加密的字节数组。</param>/// <param name="scope">数据保护范围(默认当前用户)。</param>/// <param name="optionalEntropy">可选的熵(增加安全性)。</param>/// <returns>加密后的字节数组。</returns>public static byte[] Encrypt(byte[] data, DataProtectionScope scope = DataProtectionScope.CurrentUser, byte[] optionalEntropy = null){if (data == null || data.Length == 0)throw new ArgumentNullException(nameof(data));try{return ProtectedData.Protect(data, optionalEntropy, scope);}catch (Exception ex){throw new CryptographicException("加密失败。", ex);}}/// <summary>/// 解密字节数组。/// </summary>/// <param name="encryptedData">加密后的字节数组。</param>/// <param name="scope">数据保护范围(默认当前用户)。</param>/// <param name="optionalEntropy">可选的熵(必须与加密时一致)。</param>/// <returns>解密后的原始字节数组。</returns>public static byte[] Decrypt(byte[] encryptedData, DataProtectionScope scope = DataProtectionScope.CurrentUser, byte[] optionalEntropy = null){if (encryptedData == null || encryptedData.Length == 0)throw new ArgumentNullException(nameof(encryptedData));try{return ProtectedData.Unprotect(encryptedData, optionalEntropy, scope);}catch (Exception ex){throw new CryptographicException("解密失败。", ex);}}
}

使用示例
1. 加密和解密字符串

class Program
{static void Main(){try{string originalText = "这是一个秘密消息!";Console.WriteLine("原始数据: " + originalText);// 加密string encryptedText = Cypher.Encrypt(originalText);Console.WriteLine("加密后的数据: " + encryptedText);// 解密string decryptedText = Cypher.Decrypt(encryptedText);Console.WriteLine("解密后的数据: " + decryptedText);}catch (Exception ex){Console.WriteLine("错误: " + ex.Message);}}
}

2. 使用自定义熵

class Program
{static void Main(){try{string originalText = "这是一个秘密消息!";byte[] entropy = Encoding.UTF8.GetBytes("MySecretEntropy");// 加密string encryptedText = Cypher.Encrypt(originalText, optionalEntropy: entropy);Console.WriteLine("加密后的数据: " + encryptedText);// 解密string decryptedText = Cypher.Decrypt(encryptedText, optionalEntropy: entropy);Console.WriteLine("解密后的数据: " + decryptedText);}catch (Exception ex){Console.WriteLine("错误: " + ex.Message);}}
}

3. 加密和解密字节数组

class Program
{static void Main(){try{byte[] originalData = Encoding.UTF8.GetBytes("这是一个秘密消息!");// 加密byte[] encryptedData = Cypher.Encrypt(originalData);Console.WriteLine("加密后的数据: " + Convert.ToBase64String(encryptedData));// 解密byte[] decryptedData = Cypher.Decrypt(encryptedData);Console.WriteLine("解密后的数据: " + Encoding.UTF8.GetString(decryptedData));}catch (Exception ex){Console.WriteLine("错误: " + ex.Message);}}
}

总结
Cypher 类 封装了加密和解密的逻辑,提供了简单易用的接口。

支持字符串和字节数组的加密和解密。

允许使用自定义熵以增加安全性。

通过异常处理确保代码的健壮性。

这个设计可以满足大多数基于 ProtectedData 的加密和解密需求,同时保持了代码的简洁性和可维护性。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词