数据加密方式总结
1、Base64编解码(最简单的加密方式,但实际只是进行了一个编码而已,严格意义上来说,算不上真正的加密)
Base64编码:
/// <summary> /// Base64编码 /// </summary> /// <param name="code_type">编码类型</param> /// <param name="code">待编码的字符串</param> /// <returns></returns> public static string Base64Encode(string code_type, string code) { string encode = ""; byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code); try { encode = Convert.ToBase64String(bytes); } catch { encode = code; } return encode; }
Base64解码:
/// <summary> /// Base64解码 /// </summary> /// <param name="code_type">编码类型</param> /// <param name="code">带解码的字符串</param> /// <returns></returns> public static string Base64Decode(string code_type, string code) { string decode = ""; byte[] bytes = Convert.FromBase64String(code); try { decode = Encoding.GetEncoding(code_type).GetString(bytes); } catch { decode = code; } return decode; }
2、倒序加1加密解密
倒序加1加密:
public static string EncryptOrderStr(string data) //倒序加1加密 { byte[] by = new byte[data.Length]; for (int i = 0; i <= data.Length - 1; i++) { by[i] = (byte)((byte)data[i] + 1); } data = ""; for (int i = by.Length - 1; i >= 0; i--) { data += ((char)by[i]).ToString(); } return data; }
顺序减1解密:
public static string DecryptOrderStr(string data) { byte[] by = new byte[data.Length]; for (int i = 0; i <= data.Length - 1; i++) { by[i] = (byte)((byte)data[i] - 1); } data = ""; for (int i = by.Length - 1; i >= 0; i--) { data += ((char)by[i]).ToString(); } return data; }
3、MD5加密
public static string MD5Encrypt(string paramstr) { string privateKey = "lovexiaohong"; string tempStr = FormsAuthentication.HashPasswordForStoringInConfigFile(privateKey, "MD5"); return FormsAuthentication.HashPasswordForStoringInConfigFile(paramstr + tempStr, "MD5").ToLower(); }
4、盐渍MD5哈希加密
public static string SaltedHash(string salted, string source) { if (salted.Length == 0) { salted = "hehe"; } byte[] Original = Encoding.Default.GetBytes(source); byte[] SaltValue = Encoding.Default.GetBytes(salted); byte[] ToSalt = new byte[Original.Length + SaltValue.Length]; Original.CopyTo(ToSalt, 0); SaltValue.CopyTo(ToSalt, Original.Length); MD5 st = MD5.Create(); byte[] SaltPWD = st.ComputeHash(ToSalt); byte[] PWD = new byte[SaltPWD.Length + SaltValue.Length]; SaltPWD.CopyTo(PWD, 0); SaltValue.CopyTo(PWD, SaltPWD.Length); return Convert.ToBase64String(PWD); }
5、对称加密解密(向量)
private SymmetricAlgorithm mobjCryptoService; private string key; private string iv; public Encrypt() { mobjCryptoService = new RijndaelManaged(); key = "Gu(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";//自定义的密串 iv = "G4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";//自定义的对称算法向量 } /// <summary> /// 获得密钥 /// </summary> /// <returns>密钥</returns> private byte[] GetLegalKey() { string sTemp = key; mobjCryptoService.GenerateKey();// 当在派生类中重写时,生成用于该算法的随机密钥 byte[] bytTemp = mobjCryptoService.Key; int KeyLength = bytTemp.Length; if (sTemp.Length > KeyLength) sTemp = sTemp.Substring(0, KeyLength); else if (sTemp.Length < KeyLength) sTemp = sTemp.PadRight(KeyLength, ' '); return ASCIIEncoding.ASCII.GetBytes(sTemp); } /// <summary> /// 获得初始向量IV /// </summary> /// <returns>初试向量IV</returns> private byte[] GetLegalIV() { string sTemp =iv; mobjCryptoService.GenerateIV(); byte[] bytTemp = mobjCryptoService.IV;// 获取或设置对称算法的初始化向量 int IVLength = bytTemp.Length;//获得一个 32 位整数,表示 System.Array 的所有维数中元素的总数 if (sTemp.Length > IVLength) sTemp = sTemp.Substring(0, IVLength); else if (sTemp.Length < IVLength) sTemp = sTemp.PadRight(IVLength, ' '); return ASCIIEncoding.ASCII.GetBytes(sTemp); }
加密:
/// <summary> /// 加密方法(先实例化Encrypt类) /// </summary> /// <param name="Source">待加密的串</param> /// <returns>经过加密的串</returns> public string EncrypStrByIV(string Source) { byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source); MemoryStream ms = new MemoryStream(); mobjCryptoService.Key = GetLegalKey(); mobjCryptoService.IV = GetLegalIV(); ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); cs.Write(bytIn, 0, bytIn.Length); cs.FlushFinalBlock(); ms.Close(); byte[] bytOut = ms.ToArray(); return Convert.ToBase64String(bytOut); }
解密:
/// <summary> /// 解密方法(先实例化Encrypt类) /// </summary> /// <param name="Source">待解密的串</param> /// <returns>经过解密的串</returns> public string DecrypStrByIV(string Source) { byte[] bytIn = Convert.FromBase64String(Source); MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); mobjCryptoService.Key = GetLegalKey(); mobjCryptoService.IV = GetLegalIV(); ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); return sr.ReadToEnd(); }
6、DES加密解密
DES加密:
public static string DESEncrypt(string Text, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray; inputByteArray = Encoding.Default.GetBytes(Text); des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "sha1").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "sha1").Substring(0, 8)); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cs.Close(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } return ret.ToString(); }
DES解密:
public static string DESDecrypt(string Text, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); int len; len = Text.Length / 2; byte[] inputByteArray = new byte[len]; int x, i; for (x = 0; x < len; x++) { i = Convert.ToInt32(Text.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "sha1").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "sha1").Substring(0, 8)); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); cs.Close(); return Encoding.Default.GetString(ms.ToArray()); }
7、TripleDES加密解密
TripleDES加密:
public static string TripleDESEncrypting(string strSource) { try { byte[] bytIn = Encoding.Default.GetBytes(strSource); byte[] key = { 42, 21, 93, 156, 78, 4, 218, 32, 15, 167, 44, 80, 26, 20, 155, 112, 2, 94, 11, 204, 119, 35, 184, 197 }; //定义密钥 byte[] IV = { 55, 103, 246, 79, 36, 99, 167, 3 }; //定义偏移量 TripleDESCryptoServiceProvider TripleDES = new TripleDESCryptoServiceProvider(); TripleDES.IV = IV; TripleDES.Key = key; ICryptoTransform encrypto = TripleDES.CreateEncryptor(); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write); cs.Write(bytIn, 0, bytIn.Length); cs.FlushFinalBlock(); byte[] bytOut = ms.ToArray(); return System.Convert.ToBase64String(bytOut); } catch (Exception ex) { throw new Exception("加密时候出现错误!错误提示:\n" + ex.Message); } }
TripleDES解密:
public static string TripleDESDecrypting(string Source) { try { byte[] bytIn = System.Convert.FromBase64String(Source); byte[] key = { 42, 21, 93, 156, 78, 4, 218, 32, 15, 167, 44, 80, 26, 20, 155, 112, 2, 94, 11, 204, 119, 35, 184, 197 }; //定义密钥 byte[] IV = { 55, 103, 246, 79, 36, 99, 167, 3 }; //定义偏移量 TripleDESCryptoServiceProvider TripleDES = new TripleDESCryptoServiceProvider(); TripleDES.IV = IV; TripleDES.Key = key; ICryptoTransform encrypto = TripleDES.CreateDecryptor(); MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader strd = new StreamReader(cs, Encoding.Default); return strd.ReadToEnd(); } catch (Exception ex) { throw new Exception("解密时候出现错误!错误提示:\n" + ex.Message); } }