无废话,直接上工具类
using System.Security.Cryptography; using System.Text; namespace DZCloudServer.Core.Util { public class EncryptUtil { /* RSA 加密算法 */ /// <summary> /// 获取RSA 密钥 /// 下标 0 为 私钥 /// 下标 1 为 公钥 /// </summary> /// <returns></returns> public static string[] GetRSAKey() { RSACryptoServiceProvider provider = new(); string privatekey = provider.ToXmlString(true); string publickey = provider.ToXmlString(false); return [privatekey, publickey]; } /// <summary> /// 私钥加密 /// </summary> /// <param name="plaintext"></param> /// <param name="privateKey"></param> /// <returns></returns> public static string RSAEncrypt(string plaintext, string privateKey) { using RSACryptoServiceProvider provider = new(); provider.FromXmlString(privateKey); byte[] PlaintextData = Encoding.UTF8.GetBytes(plaintext); int MaxBlockSize = provider.KeySize / 8 - 11; //加密块最大长度限制 if (PlaintextData.Length <= MaxBlockSize) return Convert.ToBase64String(provider.Encrypt(PlaintextData, false)); using MemoryStream PlaiStream = new(PlaintextData); using MemoryStream CrypStream = new(); byte[] Buffer = new byte[MaxBlockSize]; int BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize); while (BlockSize > 0) { byte[] ToEncrypt = new byte[BlockSize]; Array.Copy(Buffer, 0, ToEncrypt, 0, BlockSize); byte[] Cryptograph = provider.Encrypt(ToEncrypt, false); CrypStream.Write(Cryptograph, 0, Cryptograph.Length); BlockSize = PlaiStream.Read(Buffer, 0, MaxBlockSize); } return Convert.ToBase64String(CrypStream.ToArray(), Base64FormattingOptions.None); } /// <summary> /// 公钥解密 /// </summary> /// <param name="ciphertext"></param> /// <param name="publicKey"></param> /// <returns></returns> public static string RSADecrypt(string ciphertext, string publicKey) { using RSACryptoServiceProvider provider = new(); provider.FromXmlString(publicKey); byte[] CiphertextData = Convert.FromBase64String(ciphertext); int MaxBlockSize = provider.KeySize / 8; //解密块最大长度限制 if (CiphertextData.Length <= MaxBlockSize) return Encoding.UTF8.GetString(provider.Decrypt(CiphertextData, false)); using MemoryStream CrypStream = new(CiphertextData); using MemoryStream PlaiStream = new(); byte[] Buffer = new byte[MaxBlockSize]; int BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize); while (BlockSize > 0) { byte[] ToDecrypt = new byte[BlockSize]; Array.Copy(Buffer, 0, ToDecrypt, 0, BlockSize); byte[] Plaintext = provider.Decrypt(ToDecrypt, false); PlaiStream.Write(Plaintext, 0, Plaintext.Length); BlockSize = CrypStream.Read(Buffer, 0, MaxBlockSize); } return Encoding.UTF8.GetString(PlaiStream.ToArray()); } /// <summary> /// SHA256 提取字符串Hash /// </summary> /// <returns></returns> public static string SHA256Encryptor(string message) { byte[] bytes = Encoding.UTF8.GetBytes(message); byte[] hash = SHA256.HashData(bytes); var builder = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { builder.Append(hash[i].ToString("x2")); } return builder.ToString(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)