早上起来,头一阵疼痛,昨天睡了一天,今天再也不能这样睡下去了,于是洗漱完后往公司赶去,到公司后冷冷静静的只有我一个人,哎,又多愁善感了,闲话不多说,今天要分享的是C#中加密与解密的运算,代码如下:
/// <summary> /// 创建key /// </summary> /// <returns></returns> public static string GenerateKey() { System.Security.Cryptography.DESCryptoServiceProvider desCrypto = (System.Security.Cryptography.DESCryptoServiceProvider)System.Security.Cryptography.DESCryptoServiceProvider.Create(); return ASCIIEncoding.ASCII.GetString(desCrypto.Key); } /// <summary> /// MD5加密 /// </summary> /// <param name="text">需要加密的字符串</param> /// <param name="key">加密需要的KEY</param> /// <returns>返回加密后的字符串</returns> public static string MD5Encrypt(string text, string key) { try { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); //把字符串放到byte数组中 byte[] inputByteArray = Encoding.Default.GetBytes(text); //建立加密对象的密钥和偏移量 //使得输入密码必须输入英文文本 des.Key = ASCIIEncoding.ASCII.GetBytes(key); des.IV = ASCIIEncoding.ASCII.GetBytes(key); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateEncryptor(), System.Security.Cryptography.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(); } catch { return String.Empty; } } /// <summary> /// MD5解密 /// </summary> /// <param name="text">需要解密的字符串</param> /// <param name="key">解密需要的KEY</param> /// <returns>返回解密后的字符串</returns> public static string MD5Decrypt(string text, string key) { try { System.Security.Cryptography.DESCryptoServiceProvider des = new System.Security.Cryptography.DESCryptoServiceProvider(); byte[] inputByteArray = new byte[text.Length / 2]; for (int x = 0; x < text.Length / 2; x++) { int i = (Convert.ToInt32(text.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } //建立加密对象的密钥和偏移量,此值重要,不能修改 des.Key = ASCIIEncoding.ASCII.GetBytes(key); des.IV = ASCIIEncoding.ASCII.GetBytes(key); System.IO.MemoryStream ms = new System.IO.MemoryStream(); System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, des.CreateDecryptor(), System.Security.Cryptography.CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); //建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象 StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); } catch { return String.Empty; } }
调用端的代码:
/// <summary> /// 生成加密与解密的KEY /// </summary> String MD5KEY = GenerateKey(); /// <summary> /// 调用加密的方法,对文本框中的字符串进行加密 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { textBox2.Text = MD5Encrypt(textBox1.Text, MD5KEY); } /// <summary> /// 调用解密的方法,对加密后的字符串进行解密 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { textBox3.Text = MD5Decrypt(textBox2.Text, MD5KEY); }
这里需要注意的是,加密用的MD5KEY和解密用的MD5KEY必须一样,否则无法正常解密!