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

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

//注意:密钥必须为8位        private const string m_strEncryptKey = "abcd1234";        ///         /// 加密字符串        ///         /// 明码        /// 
加密后的密码
public static string DesEncrypt(string p_strInput) { byte[] byKey = null; byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; try { byKey = System.Text.Encoding.UTF8.GetBytes(m_strEncryptKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.UTF8.GetBytes(p_strInput); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } catch (System.Exception ex) { throw (ex); } } /// /// 解密字符串 /// /// 加了密的字符串 public static string DesDecrypt(string p_strInput) { byte[] byKey = null; byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; byte[] inputByteArray = new Byte[p_strInput.Length]; try { byKey = System.Text.Encoding.UTF8.GetBytes(m_strEncryptKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(p_strInput); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = new System.Text.UTF8Encoding(); return encoding.GetString(ms.ToArray()); } catch (System.Exception ex) { throw (ex); } }

 

转载于:https://www.cnblogs.com/Evelia/p/3678239.html

你可能感兴趣的文章