C# 加密、解密函数
2024-08-30 05:34:09
#region ========加密======== /// <summary>
/// 加密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Encrypt(string Text)
{
if (Text == null)
return Text;
return Encrypt(Text, "123456789abcd");
}
/// <summary>
/// 加密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Encrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray;
inputByteArray = Encoding.Default.GetBytes(Text);
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
} #endregion #region ========解密======== /// <summary>
/// 解密
/// </summary>
/// <param name="Text"></param>
/// <returns></returns>
public static string Decrypt(string Text)
{
if (Text == null)
return Text;
return Decrypt(Text, "123456789abcd");
}
/// <summary>
/// 解密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Decrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len = Text.Length / ;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = ; x < len; x++)
{
i = Convert.ToInt32(Text.Substring(x * , ), );
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(, ));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, , inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
} #endregion
最新文章
- [Spring框架]Spring 事务管理基础入门总结.
- 《2016ThoughtWorks技术雷达峰会----微服务架构》
- hibernate考试题
- 工作中一个简单的shell程序
- js方法之间的调用之——传参方法
- mysql join 查询图
- matrix_last_acm_3
- 【工具篇】notepad++
- The ";Run One Program Only"; Phenomenon
- oracle instr函数
- Ubuntu Linux系统下的SVN客户端工具PySVN
- 二.java下使用RabbitMQ实现hello world
- lesson - 3 笔记 ls /alias /ldd /cd /pwd /环境变量 / 目录
- 普通用户添加sudo权限
- Leetcode 两数之和 (散列表)
- PHP使用MySQL实现消息队列
- Scala进阶之路-统计商家id的标签数以及TopN示例案例分析
- centos 7.2 安装域名服务器(bind9.9 集群--主从架构),私有域名服务器+缓存
- linux内核分析第五周学习笔记
- XVFB实现selenium在linux上无界面运行安装篇
热门文章
- HDU4080Stammering Aliens(后缀数组+二分)
- P2060 [HNOI2006]马步距离
- 【Lintcode】033.N-Queens
- 洛谷 P3804 [模板] 后缀自动机
- 优化EF的性能
- JAVA基础知识 String s = new String(";ABC";) VS String s = ";abc";
- jquery 插件 国外
- java.lang.NoClassDefFoundError: org/apache/commons/collections/map/LRUMap 解决方法
- Oracle数据去重
- bootstrap下拉框式标签页