/// <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)
{
return Decrypt(Text, "DTcms");
}
/// <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

C# 数据的加密解密的更多相关文章

  1. 使用 GPG 对数据进行加密解密签名

    一:使用 GPG 对数据进行加密解密签名 基本的工具使用 1. GPG 是GNUPG 免费开源的gpg加密工具,和同pgp兼容,pgp收费. 2. 在mac上使用https://gpgtools.or ...

  2. iOS中使用RSA对数据进行加密解密

    RSA算法是一种非对称加密算法,常被用于加密数据传输.如果配合上数字摘要算法, 也可以用于文件签名. 本文将讨论如何在iOS中使用RSA传输加密数据. 本文环境 mac os openssl-1.0. ...

  3. 与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密

    原文:与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密 [索引页][源码下载] 与众不同 wi ...

  4. asp.net core 使用中间件拦截请求和返回数据,并对数据进行加密解密。

    原文:asp.net core 使用中间件拦截请求和返回数据,并对数据进行加密解密. GitHub demo https://github.com/zhanglilong23/Asp.NetCore. ...

  5. 使用Crypto对数据进行加密解密

    注释都在代码里: 先撸客户端: from Crypto.Cipher import AES import base64,requests class Message(object): def __in ...

  6. 序列化和反序列化在浏览器和 Web 服务器之间传递的数据、加密解密

    js中数组不能传递到后台,需进行json序列化: var data = new Array(); data.push({para1:name,para2:answer}); string data = ...

  7. Cookie中存放数据l加密解密的算法

    public class CookieUtil { /** * * @param response HttpServletResponse类型的响应 * @param cookie 要设置httpOn ...

  8. 重新想象 Windows 8 Store Apps (31) - 加密解密: 哈希算法, 对称算法

    原文:重新想象 Windows 8 Store Apps (31) - 加密解密: 哈希算法, 对称算法 [源码下载] 重新想象 Windows 8 Store Apps (31) - 加密解密: 哈 ...

  9. 使用JDK中的安全包对数据进行加解密

    本文以使用DES对称加密算法为例使用jdk对数据进行加密解密. 首先需要了解Provider类,它是jdk引入的密码服务提供者概念,实现了Java安全性的一部分或者全部.Provider 可能实现的服 ...

随机推荐

  1. mysql如果搜索长度过宽 导致显示不全的情况解决

    今天我在搜索数据库里面优惠码字段 直接使用 select * from table 的命令的时候 由于 第一个 字段过长导致后面的都无法显示全..我还是宽屏! 所以 搜索了一下 可以让 它单行显示 使 ...

  2. 如何调试最新的asp.net mvc源码

    vs2013调试 一.源码当前为5.2.0.0,按下面改为5.0.0.1 二./web.config 版本为5.0.0.0 改为5.0.0.1 三.vs2013 x86 本机工具命令提示 sn.exe ...

  3. 第二百一十九天 how can I 坚持

    今天好冷,白天在家待了一天,晚上,老贾生日,生日快乐,去海底捞吃了个火锅,没感觉呢. 今天还发现了个好游戏,纪念碑谷,挺新颖,就是难度有点大了. 好累.睡觉,明天想去爬山啊,可是该死的天气.

  4. js闭包测试

    本文的诞生,源自近期打算做的一个关于javascript中的闭包的专题,由于需要解析闭包对垃圾回收的影响,特此针对不同的javascript引擎,做了相关的测试. 为了能从本文中得到需要的知识,看本文 ...

  5. rdlc Report Viewer Configuration Error

    主要是web.config没有配置 方法参考http://msdn.microsoft.com/zh-cn/library/ms251661.aspx

  6. LightOJ 1370 - Bi-shoe and Phi-shoe (欧拉函数思想)

    http://lightoj.com/volume_showproblem.php?problem=1370 Bi-shoe and Phi-shoe Time Limit:2000MS     Me ...

  7. [转载]关于AutoCAD.NET的辅助方法

    转载自:http://www.cnblogs.com/milian/p/3315000.html 求中点坐标: /// <summary> /// 中点 /// </summary& ...

  8. thinkphp 定位查询 Model:last您所请求的方法不存在!

    thinkphp 定位查询 Model:last您所请求的方法不存在!   用thinkphp3.1做项目的时候为了获取记录表中最后一个id用到了last()方法,出现了这个错误:Model:last ...

  9. 导入导出Excel

    最近需要频繁的使用导入导出,各么,又不想使用ms的PIA,在4.0以下,存在版本兼容的问题. 于是网上查找了很久,找到两款开源的excel组件. 1.CSharpJExcel,这是JExcel的.ne ...

  10. Spring+Struts+Ibatis的配置

    指定Spring配置文件位置 <context-param> <param-name>contextConfigLocation</param-name> < ...