C#加解密
2014-03-25 13:39
190 查看
/// <summary> /// 加密数据 /// </summary> /// <param name="Text"></param> 加密文本 /// <param name="sKey"></param> 加密key /// <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(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms=new System.IO.MemoryStream(); CryptoStream cs=new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write); cs.Write(inputByteArray,0,inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret=new StringBuilder(); foreach( byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}",b); } return ret.ToString(); }
/// <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/2; byte[] inputByteArray = new byte[len]; int x,i; for(x=0;x<len;x++) { i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); inputByteArray[x]=(byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8)); System.IO.MemoryStream ms=new System.IO.MemoryStream(); CryptoStream cs=new CryptoStream(ms,des.CreateDecryptor(),CryptoStreamMode.Write); cs.Write(inputByteArray,0,inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); }
相关文章推荐
- 适用于SQLSERVER2000存储过程,函数,视图,触发器解密(转载)
- ios下使用rsa算法与php进行加解密通讯
- aes 加密,解密(2)
- WEB系统中加密/解密URL传输参数
- JAVA 3DES-Cipher加密解密
- iOS开发-加密与解密之CommonCrypto与Security.framework
- SQL 2000 存储过程如何加密,解密
- 使用crypto api的加密解密源代码
- C#之AES256位加密解密
- 存储过程解密(破解函数,过程,触发器,视图.仅限于SQLSERVER2000)
- OpenSSL公钥私钥加密解密程序
- 加密和解密,(数字)签名和验证,公钥私钥
- javascript又一解密过程,推荐的,会这个基本上好多都能解决了第1/2页
- JAVA的md5解密后导致乱码
- Enigma机密码加密解密的实现
- 不错的隐藏AV大法解密
- 庖丁解牛-----Live555源码彻底解密(v0.78--2013.09.18)
- 分享一些免费的MD5解密网站
- 网游:新三国策IV的加解密机制分析及外挂方面的思考
- android(客户端)和PC(服务器端)通信RSA 加密解密