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

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

#region  MD5 加密        ///         /// MD5加密静态方法        ///         /// 待加密的密文        /// 
public static string MD5Encrypt(string EncryptString) { if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); } MD5 m_ClassMD5 = new MD5CryptoServiceProvider(); string m_strEncrypt = ""; try { m_strEncrypt = BitConverter.ToString(m_ClassMD5.ComputeHash(Encoding.Default.GetBytes(EncryptString))).Replace("-", ""); } catch (ArgumentException ex) { throw ex; } catch (CryptographicException ex) { throw ex; } catch (Exception ex) { throw ex; } finally { m_ClassMD5.Clear(); } return m_strEncrypt; } #endregion #region DES 加密解密 可以自己设定 private static string DesKey = "wanwanzz"; /// /// DES 加密(数据加密标准,速度较快,适用于加密大量数据的场合) /// /// 待加密的密文 ///
public static string DESEncrypt(string EncryptString) { if (string.IsNullOrEmpty(EncryptString)) { throw (new Exception("密文不得为空")); } if (string.IsNullOrEmpty(DesKey)) { throw (new Exception("密钥不得为空")); } if (DesKey.Length!=8) { throw (new Exception("密钥必须为8位")); } //定义偏移量 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; string m_strEncrypt = ""; DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider(); try { byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString); MemoryStream m_stream = new MemoryStream(); CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateEncryptor(Encoding.Default.GetBytes(DesKey), m_btIV), CryptoStreamMode.Write); m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length); m_cstream.FlushFinalBlock(); m_strEncrypt = Convert.ToBase64String(m_stream.ToArray()); m_stream.Close(); m_stream.Dispose(); m_cstream.Close(); m_cstream.Dispose(); } catch (IOException ex) { throw ex; } catch (CryptographicException ex) { throw ex; } catch (ArgumentException ex) { throw ex; } catch (Exception ex) { throw ex; } finally { m_DESProvider.Clear(); } return m_strEncrypt; } /// /// DES 解密(数据加密标准,速度较快,适用于加密大量数据的场合) /// /// ///
public static string DESDecrypt(string DecryptString) { if (string.IsNullOrEmpty(DecryptString)) { throw (new Exception("密文不得为空")); } if (string.IsNullOrEmpty(DesKey)) { throw (new Exception("密钥不得为空")); } if (DesKey.Length != 8) { throw (new Exception("密钥必须为9位")); } byte[] m_btIV = { 0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF}; string m_strDecrpty = ""; DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider(); try { byte[] m_btDecrptyString = Convert.FromBase64String(DecryptString); MemoryStream m_stream = new MemoryStream(); CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateDecryptor(Encoding.Default.GetBytes(DesKey), m_btIV), CryptoStreamMode.Write); m_cstream.Write(m_btDecrptyString, 0, m_btDecrptyString.Length); m_cstream.FlushFinalBlock(); m_strDecrpty = Encoding.Default.GetString(m_stream.ToArray()); m_stream.Close(); m_stream.Dispose(); m_cstream.Close(); m_cstream.Dispose(); } catch (IOException ex) { throw ex; } catch (CryptographicException ex) { throw ex; } catch (ArgumentException ex) { throw ex; } catch (Exception ex) { throw ex; } return m_strDecrpty; } #endregion #region MD5加密解密 /// /// MD5加密 /// /// 需要加密的字符串 ///
MD5加密后的字符串
public static string Md5Encrypt(string strSource) { //把字符串放到byte数组中 byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource); //建立加密对象的密钥和偏移量 byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量 byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥 //实例DES加密类 DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider(); mobjCryptoService.Key = iv; mobjCryptoService.IV = key; ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor(); //实例MemoryStream流加密密文件 System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); cs.Write(bytIn, 0, bytIn.Length); cs.FlushFinalBlock(); return System.Convert.ToBase64String(ms.ToArray()); } #endregion #region MD5解密 /// /// MD5解密 /// /// 需要解密的字符串 ///
MD5解密后的字符串
public static string Md5Decrypt(string Source) { //将解密字符串转换成字节数组 byte[] bytIn = System.Convert.FromBase64String(Source); //给出解密的密钥和偏移量,密钥和偏移量必须与加密时的密钥和偏移量相同 byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };//定义偏移量 byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//定义密钥 DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider(); mobjCryptoService.Key = iv; mobjCryptoService.IV = key; //实例流进行解密 System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length); ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader strd = new StreamReader(cs, Encoding.Default); return strd.ReadToEnd(); } #endregion

  

转载于:https://www.cnblogs.com/pushYYL/p/10026190.html

你可能感兴趣的文章
2018年第三季欧洲DDoS攻击量急剧增加
查看>>
人体运动轨迹的人工智能动画模拟
查看>>
spring,orm 题目
查看>>
Java 回调函数
查看>>
2018/02/13
查看>>
echarts(二)
查看>>
Hibernate映射文件结构
查看>>
rollPagerView引导页轮播图
查看>>
redis集群介绍,redis集群搭建配置,redis集群操作
查看>>
Gitbilt hooks 简单的账户操作权限控制
查看>>
消息推送如何平推推送和用户体验
查看>>
干货|区块链技术入门——比特币运行及交易原理剖析(分享实录)
查看>>
shell特殊符号cut命令、sort_wc_uniq命令、tee_tr_split命令、shell特殊符号下
查看>>
【TensorFlow系列】【六】多模型部署
查看>>
自制简单博客模板
查看>>
ASP.NET---PageOffice集成说明
查看>>
百度嵌入式深度学习框架Paddle-Mobile介绍
查看>>
都别争了!分布式锁也是锁
查看>>
艺术源于生活而高于生活,IT编程亦是如此
查看>>
了解Logstash
查看>>