public class EncryptString { /// <summary> /// MD5加密 /// </summary> /// <param name="instr"></param> /// <returns></returns> public static string EncryptMD5(string instr) { string result; try { byte[] toByte = Encoding.Default.GetBytes(instr); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); toByte = md5.ComputeHash(toByte); result = BitConverter.ToString(toByte).ToLower().Replace("-", ""); } catch { result = ""; } return result; } /// <summary> /// 加密(自定义的字节加密) /// </summary> /// <param name="str"></param> /// <param name="bo">是否加随机数</param> /// <returns></returns> public static string MyEncrypt(string str, bool bo) { char[] cs = str.ToCharArray(); byte[] by; string results = "", temp = ""; for (int i = 0; i < cs.Length; i++) { by = System.Text.Encoding.UTF8.GetBytes(cs[i].ToString()); temp = Convert.ToBase64String(by);//每个字转换为四位 if (bo) { temp += Common.GetMes.Random(10, 99).ToString();//同时加两个随机变量 } results += temp;//(每个变为6位)全部相加 } return results; } /// <summary> /// 解密(自定义的字节加密) /// </summary> /// <param name="str"></param> /// <param name="t">要和上面的对应,如果加了随机数则t值应该为6,不加的话是4,否则出错</param> /// <returns></returns> public static string DeMyEnncrypt(string str, int t) { string result = "", strtemp = ""; try { char[] cs = str.ToCharArray(); string[] temp = Common.GetMes.ArrayListstr(str, t); byte[] by; for (int i = 0; i < temp.Length; i++) { strtemp = (temp[i].ToString()).Substring(0, 4); by = Convert.FromBase64String(strtemp); result += System.Text.Encoding.UTF8.GetString(by); } return result; } catch { return ""; } } /// <summary> /// 把一个字符串按照n位长度分割为数组 /// </summary> /// <param name="str"></param> /// <param name="n"></param> /// <returns></returns> public static string[] ArrayListstr(string str, int n) { string[] result; try { if (n <= 0) { n = str.Length; } int arrynum = 0; arrynum = (int)(str.Length / n); if (arrynum <= 0) { arrynum = 1; } result = new string[arrynum]; for (int i = 0; i < arrynum; i++) { result[i] = str.Substring(n * i, n); } } catch { result = new string[1] { "" }; } return result; } }