博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 加密解密(DES,3DES,MD5,Base64) 类
阅读量:6119 次
发布时间:2019-06-21

本文共 5789 字,大约阅读时间需要 19 分钟。

原文:

保存!

public sealed class EncryptUtils    {        #region Base64加密解密        ///         /// Base64加密        ///         /// 需要加密的字符串        /// 
public static string Base64Encrypt(string input) { return Base64Encrypt(input, new UTF8Encoding()); } /// /// Base64加密 /// /// 需要加密的字符串 /// 字符编码 ///
public static string Base64Encrypt(string input, Encoding encode) { return Convert.ToBase64String(encode.GetBytes(input)); } /// /// Base64解密 /// /// 需要解密的字符串 ///
public static string Base64Decrypt(string input) { return Base64Decrypt(input, new UTF8Encoding()); } /// /// Base64解密 /// /// 需要解密的字符串 /// 字符的编码 ///
public static string Base64Decrypt(string input, Encoding encode) { return encode.GetString(Convert.FromBase64String(input)); } #endregion #region DES加密解密 /// /// DES加密 /// /// 加密数据 /// 8位字符的密钥字符串 /// 8位字符的初始化向量字符串 ///
public static string DESEncrypt(string data, string key, string iv) { byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv); DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); int i = cryptoProvider.KeySize; MemoryStream ms = new MemoryStream(); CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cst); sw.Write(data); sw.Flush(); cst.FlushFinalBlock(); sw.Flush(); return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); } /// /// DES解密 /// /// 解密数据 /// 8位字符的密钥字符串(需要和加密时相同) /// 8位字符的初始化向量字符串(需要和加密时相同) ///
public static string DESDecrypt(string data, string key, string iv) { byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(key); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(iv); byte[] byEnc; try { byEnc = Convert.FromBase64String(data); } catch { return null; } DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream(byEnc); CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read); StreamReader sr = new StreamReader(cst); return sr.ReadToEnd(); } #endregion #region MD5加密 /// /// MD5加密 /// /// 需要加密的字符串 ///
public static string MD5Encrypt(string input) { return MD5Encrypt(input, new UTF8Encoding()); } /// /// MD5加密 /// /// 需要加密的字符串 /// 字符的编码 ///
public static string MD5Encrypt(string input, Encoding encode) { MD5 md5 = new MD5CryptoServiceProvider(); byte[] t = md5.ComputeHash(encode.GetBytes(input)); StringBuilder sb = new StringBuilder(32); for (int i = 0; i < t.Length; i++) sb.Append(t[i].ToString("x").PadLeft(2, '0')); return sb.ToString(); } /// /// MD5对文件流加密 /// /// ///
public static string MD5Encrypt(Stream stream) { MD5 md5serv = MD5CryptoServiceProvider.Create(); byte[] buffer = md5serv.ComputeHash(stream); StringBuilder sb = new StringBuilder(); foreach (byte var in buffer) sb.Append(var.ToString("x2")); return sb.ToString(); } /// /// MD5加密(返回16位加密串) /// /// /// ///
public static string MD5Encrypt16(string input, Encoding encode) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); string result = BitConverter.ToString(md5.ComputeHash(encode.GetBytes(input)), 4, 8); result = result.Replace("-", ""); return result; } #endregion #region 3DES 加密解密 public static string DES3Encrypt(string data, string key) { TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(key); DES.Mode = CipherMode.CBC; DES.Padding = PaddingMode.PKCS7; ICryptoTransform DESEncrypt = DES.CreateEncryptor(); byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(data); return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)); } public static string DES3Decrypt(string data, string key) { TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); DES.Key = ASCIIEncoding.ASCII.GetBytes(key); DES.Mode = CipherMode.CBC; DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7; ICryptoTransform DESDecrypt = DES.CreateDecryptor(); string result = ""; try { byte[] Buffer = Convert.FromBase64String(data); result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)); } catch (Exception e) { } return result; } #endregion }

  

转载地址:http://bxlka.baihongyu.com/

你可能感兴趣的文章
分布式事务,EventBus 解决方案:CAP【中文文档】
查看>>
Linux下的CPU性能瓶颈分析案例
查看>>
spring mvc入门
查看>>
2012在数据库技术会议上的讲话PPT打包
查看>>
【Android】 TextView设置个别字体样式
查看>>
python svn
查看>>
raise语句
查看>>
sequence2(高精度dp)
查看>>
ABP实战--集成Ladp/AD认证
查看>>
存储过程
查看>>
phpcms v9栏目列表调用每一篇文章内容方法
查看>>
python 自定义信号处理器
查看>>
luov之SMTP报错详解
查看>>
软件概要设计做什么,怎么做
查看>>
dwr
查看>>
java的特殊符号
查看>>
word2010中去掉红色波浪线的方法
查看>>
fabric上下文管理器(context mangers)
查看>>
JQuery-EasyUI Datagrid数据行鼠标悬停/离开事件(onMouseOver/onMouseOut)
查看>>
并发和并行的区别
查看>>