博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AES加密 AESCrypt 类
阅读量:5295 次
发布时间:2019-06-14

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

///     /// AES加密    ///     public sealed class AESCrypt    {        ///         /// 加密        ///         ///         /// 
static public string Encode(string text) { byte[] key = new byte[] { 132, 149, 17, 104, 128, 101, 170, 180, 191, 28, 127, 149, 144, 121, 200, 130 }; byte[] iv = new byte[] { 198, 12, 49, 123, 101, 128, 103, 212, 112, 122, 133, 35, 150, 130, 210, 181 }; try { RijndaelManaged rijn = new RijndaelManaged(); ICryptoTransform trans = rijn.CreateEncryptor(key, iv); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, trans, CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cs); try { sw.Write(text); sw.Flush(); } finally { sw.Close(); } byte[] _text = ms.GetBuffer(); int k = 0; for (k = _text.Length - 1; k >= 0; k--) { if (_text[k] != 0) { break; } } int len = k + 1; if (len % 2 != 0) {
//必须保证取的长度为偶数 len += 1; } return Convert.ToBase64String(ms.GetBuffer(), 0, len); } catch { return string.Empty; } } /// /// 解密 /// /// ///
static public string Decode(string text) { byte[] key = new byte[] { 132, 149, 17, 104, 128, 101, 170, 180, 191, 28, 127, 149, 144, 121, 200, 130 }; byte[] iv = new byte[] { 198, 12, 49, 123, 101, 128, 103, 212, 112, 122, 133, 35, 150, 130, 210, 181 }; try { RijndaelManaged rijn = new RijndaelManaged(); ICryptoTransform trans = rijn.CreateDecryptor(key, iv); byte[] data = Convert.FromBase64String(text); MemoryStream ms = new MemoryStream(data); CryptoStream cs = new CryptoStream(ms, trans, CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); string r_data = string.Empty; try { r_data = sr.ReadToEnd(); } finally { sr.Close(); } return r_data; } catch (Exception ex) { return string.Empty; } } } /// /// SHA1摘要 /// public sealed class SHA1Crypt { /// /// Base64编码 /// /// ///
static public string MakeCode(string text) { UTF8Encoding utf8 = new UTF8Encoding(); SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider(); byte[] cb = utf8.GetBytes(text); byte[] sb = sha1.ComputeHash(cb); return Convert.ToBase64String(sb); } } /// /// MD5摘要 /// public sealed class MD5Crypt { /// /// 32位编码 /// /// ///
public string MakeCode(string text) { byte[] _bytes = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(text)); StringBuilder _result = new StringBuilder(); for (int i = 0; i < _bytes.Length; i++) { string _hex = _bytes[i].ToString("x"); if (_hex.Length == 1) { _result.Append("0"); } _result.Append(_hex); } return _result.ToString(); } }

 

转载于:https://www.cnblogs.com/dragon-L/p/3680428.html

你可能感兴趣的文章
深入理解css中的margin属性
查看>>
C++ 删除字符串的两种实现方式
查看>>
电容选型
查看>>
ORA-01502: 索引'P_ABCD.PK_WEB_BASE'或这类索引的分区处于不可用状态
查看>>
Spring EL hello world实例
查看>>
百度地图API地理位置和坐标转换
查看>>
MyBatis学习总结(六)——调用存储过程
查看>>
code-代码平台服务器路径
查看>>
离线安装 Visual Studio Express 而不下载整个镜像文件的方法(转载)
查看>>
2014年国际数学家大会台历
查看>>
[数分提高]2014-2015-2第3教学周第1次课
查看>>
2017-2018-2偏微分方程复习题解析10
查看>>
Java抽象类和接口的比较
查看>>
web技术工具帖
查看>>
SpringBoot项目中常见的注解
查看>>
一次性搞明白 service和factory区别
查看>>
select下拉二级联动
查看>>
iOS UI控件5-UIPickerView
查看>>
深入Java虚拟机读书笔记第三章安全
查看>>
IO流 总结一
查看>>