加密解密
DES加密解密:
using System; using System.Collections.Generic; using System.Xml.Linq; using System.Text; using System.Security.Cryptography; using System.IO; namespace MemoryPassword { public class MyEncrypt { private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; private static string encryptKey = "abcdefgh"; /// <summary> /// DES加密字符串 /// </summary> /// <param name="encryptString">待加密的字符串</param> /// <param name="encryptKey">加密密钥,要求为8位</param> /// <returns>加密成功返回加密后的字符串,失败返回源串</returns> public static string EncryptDES(string encryptString) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Convert.ToBase64String(mStream.ToArray()); } catch { return encryptString; } } /// <summary> /// DES解密字符串 /// </summary> /// <param name="decryptString">待解密的字符串</param> /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param> /// <returns>解密成功返回解密后的字符串,失败返源串</returns> public static string DecryptDES(string decryptString) { try { byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey); byte[] rgbIV = Keys; byte[] inputByteArray = Convert.FromBase64String(decryptString); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); return Encoding.UTF8.GetString(mStream.ToArray()); } catch { return decryptString; } } } }
MD5加密解密:
public class Encrypt_Decrypt { //对密码进行MD5加密的函数(添加盐值:&%#@?,:*) public string getEncryPassword(string Password) { string EncryedPassword=FormsAuthentication.HashPasswordForStoringInConfigFile( Password+"&%#@?,:*", "md5"); // Or "sha1" return EncryedPassword; } // 加密 public string EncryptText(String strText) { return Encrypt(strText, "&%#@?,:*"); // return Encrypt(strText,DateTime.Now.ToString() ); } //'解密 public String DecryptText(String strText) { return Decrypt(strText, "&%#@?,:*"); // return Decrypt(strText,DateTime.Now.ToString()); } //'加密函数 private String Encrypt(String strText, String strEncrKey) { Byte[] byKey = {}; Byte[] IV = { 0x01, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; try { byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText); 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(Exception ex) { return ex.Message; } } //'解密函数 private String Decrypt(String strText, String sDecrKey) { char[] stBase=strText.ToCharArray(); for (int i = 0; i < stBase.Length; i++) { if (stBase[i] ==' ') { stBase[i] = '+'; } } strText = ""; for (int i = 0; i < stBase.Length; i++) { strText += stBase[i]; } Byte[] byKey = {}; Byte[] IV = { 0x01,0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; Byte[] inputByteArray = new byte[strText.Length]; try { byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(strText); 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 = System.Text.Encoding.UTF8; return encoding.GetString(ms.ToArray()); } catch(Exception ex) { return ex.Message; } } }
posted on 2013-08-30 17:37 Lucifer_Tian 阅读(371) 评论(0) 收藏 举报
浙公网安备 33010602011771号