博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DES加密解决算法
阅读量:4585 次
发布时间:2019-06-09

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

    ///         /// DES加密算法        /// sKey为8位或16位        ///         /// 需要加密的字符串        /// 密钥        /// 
public static string DesEncryptStr(string pToEncrypt, string sKey) { StringBuilder ret = new StringBuilder(); 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(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString(); //return a; } /// /// DES解密算法 /// sKey为8位或16位 /// /// 需要解密的字符串 /// 密钥 ///
public static string DesDecryptStr(string pToDecrypt, string sKey) { MemoryStream ms = new MemoryStream(); 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); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); }

 

转载于:https://www.cnblogs.com/Xanthus/p/9578202.html

你可能感兴趣的文章
8天学通MongoDB——第二天 细说增删查改
查看>>
TextBloc研究
查看>>
Engine auto idle help conserve fuel reduce noise
查看>>
MAC下安装pomelo
查看>>
182. Duplicate Emails
查看>>
Redis、Memcache和MongoDB的区别
查看>>
设计模式笔记 ------ 原型模式
查看>>
通过Repeater控件绑定数据,相同数据合并单元格。
查看>>
h5 和之前版本的区别
查看>>
【UVAlive 3989】 Ladies' Choice (稳定婚姻问题)
查看>>
【FFT&NTT 总结】
查看>>
洛谷——P1802 5倍经验日
查看>>
leetcode121—Best Time to Buy and Sell Stock
查看>>
【系统优化】为系统提速,何须重装
查看>>
让Chrome 接管邮件连接,收发邮件更方便了
查看>>
cmd 编码 utf8
查看>>
jquery-file-upload demo
查看>>
第一期_Nor Flash
查看>>
oracle 10g
查看>>
ecshop那些事
查看>>