加密,解密 自定义密钥
2016-03-25 09:28
357 查看
/// <summary> /// 加密 /// </summary> /// <param name="pToEncrypt"></param> /// <param name="sKey">密钥必须是8位</param> /// <returns></returns> public static string MD5Encrypt(string pToEncrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new 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); } ret.ToString(); return ret.ToString(); } /// <summary> /// 解密 /// </summary> /// <param name="pToDecrypt"></param> /// <param name="sKey"></param> /// <returns></returns> public static string MD5Decrypt(string pToDecrypt, string sKey) { string text = ""; try { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); text = System.Text.Encoding.Default.GetString(ms.ToArray()); } catch { text = pToDecrypt; } return text; }
相关文章推荐
- document.cookie.indexof的解释
- 开源软件的商业化策略模型
- Linux之内存使用限制
- sha2 替换sha1 时间表
- 持续集成篇_04_Maven使用Jetty或Tomcat运行项目
- matlab标准化和反标准化——zscore
- java拦截器模型
- 支持中文加密解密
- 报错java.lang.IllegalMonitorStateException报错
- iOS应用设计模式开发中对简单工厂和工厂方法模式的运用
- 从0自学C#10--在线绘制类图
- Eclipse Git下载问题:Internal error; consult Eclipse error log.
- Android studio和Eclipse分别生成jar包并混淆jar包
- notepad++上zend-coding的使用
- 【转载】【果壳日志】企业如何成功运用WEB版ERP项目
- 现代软件工程第五次作业
- JS获取整个网页html代码
- linux分享六:字符串处理
- File存储,用于存储简单的数据,以文件格式保存(SDcard 存储、Environment环境)
- sha2 替换sha1 时间表