Md5加密解密方法
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace Siia.Veima.Host.Utils { public class CommonFunction { /// <summary> /// 32位MD5加密 /// </summary> /// <param name="password"></param> /// <returns></returns> public static string MD5Encrypt32(string password) { //string cl = password; //string pwd = ""; //MD5 md5 = MD5.Create(); //实例化一个md5对像 // // 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 //byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl)); //// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得 //for (int i = 0; i < s.Length; i++) //{ // // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符 // pwd = pwd + s[i].ToString("X"); //} //return pwd; MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(password))); t2 = t2.Replace("-", ""); return t2; } /// <summary> /// 加密 /// </summary> /// <param name="characters">要加密的明文</param> /// <returns>加密后的密文</returns> public static string EncryptStr(string characters) { string key = "330ead0b-4951-46a7-8db8-0e2569472fd1"; //位移加密 byte[] bStr = (new UnicodeEncoding()).GetBytes(characters); for (int i = 0; i < bStr.Length; i++) { byte b = (byte)(bStr[i] + 255); bStr[i] = b; } byte[] bKey = (new UnicodeEncoding()).GetBytes(key);//加密锁 //异或加密 for (int i = 0; i < bStr.Length; i += 2) { for (int j = 0; j < bKey.Length; j += 3) { bStr[i] = Convert.ToByte(bStr[i] ^ bKey[j]); } } return (new UnicodeEncoding()).GetString(bStr).TrimEnd('\0'); } /// <summary> /// 解密 /// </summary> /// <param name="ciphertext">要解密的密文</param> /// <returns>解密后的明文</returns> public static string DecryptStr(string ciphertext) { string key = "330ead0b-4951-46a7-8db8-0e2569472fd1"; byte[] bStr = (new UnicodeEncoding()).GetBytes(ciphertext); byte[] bKey = (new UnicodeEncoding()).GetBytes(key);//加密锁 //异或解密 for (int i = 0; i < bStr.Length; i += 2) { for (int j = 0; j < bKey.Length; j += 3) { bStr[i] = Convert.ToByte(bStr[i] ^ bKey[j]); } } //位移解密 for (int i = 0; i < bStr.Length; i++) { byte b = (byte)(bStr[i] - 255); bStr[i] = b; } return (new UnicodeEncoding()).GetString(bStr).TrimEnd('\0'); } //加密 public static string Encryption(string express) { CspParameters param = new CspParameters(); param.KeyContainerName = "siia";//密匙容器的名称,保持加密解密一致才能解密成功 using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param)) { byte[] plaindata = Encoding.Default.GetBytes(express);//将要加密的字符串转换为字节数组 byte[] encryptdata = rsa.Encrypt(plaindata, false);//将加密后的字节数据转换为新的加密字节数组 return Convert.ToBase64String(encryptdata);//将加密后的字节数组转换为字符串 } } //解密 public static string Decrypt(string ciphertext) { CspParameters param = new CspParameters(); param.KeyContainerName = "330ead0b-4951-46a7-8db8-0e2569472fd1"; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param)) { byte[] encryptdata = Convert.FromBase64String(ciphertext); byte[] decryptdata = rsa.Decrypt(encryptdata, false); return Encoding.Default.GetString(decryptdata); } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)