DES 加密 解密
public static class Common { /// <summary> /// 密钥 /// </summary> public static string key = "@#$!"; #region 解密 /// <summary> /// 解密 /// </summary> /// <param name="str"></param> /// <param name="key"></param> /// <returns></returns> public static string Decode(string str, string key) { string sss = ""; byte[] keyBytes = Encoding.Unicode.GetBytes(key); byte[] inputByteArray = Convert.FromBase64String(str); DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); MemoryStream mStream = new MemoryStream(); CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyBytes), CryptoStreamMode.Write); cStream.Write(inputByteArray, 0, inputByteArray.Length); cStream.FlushFinalBlock(); sss = Encoding.Unicode.GetString(mStream.ToArray()); //Unicode编码 return sss; } #endregion #region 加密 /// <summary> /// 加密 /// </summary> /// <param name="str"></param> /// <param name="key"></param> /// <returns></returns> public static string Encode(string str, string key) { DESCryptoServiceProvider provider = new DESCryptoServiceProvider(); byte[] btKey = Encoding.Unicode.GetBytes(key); //向量 密钥 byte[] buffer = Encoding.Unicode.GetBytes(str); //Unicode编码 MemoryStream stream = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(btKey, btKey), CryptoStreamMode.Write); stream2.Write(buffer, 0, buffer.Length); stream2.FlushFinalBlock(); return Convert.ToBase64String(stream.ToArray()); } #endregion #region public static string GetSubString(string string_0, int int_0, string string_1) { return GetSubString(string_0, 0, int_0, string_1); } public static string GetSubString(string string_0, int int_0, int int_1, string string_1) { string str = string_0; byte[] bytes = Encoding.UTF8.GetBytes(string_0); foreach (char ch in Encoding.UTF8.GetChars(bytes)) { if (((ch > 'ࠀ') && (ch < '一')) || ((ch > 0xac00) && (ch < 0xd7a3))) { if (int_0 < string_0.Length) { return string_0.Substring(int_0, ((int_1 + int_0) > string_0.Length) ? (string_0.Length - int_0) : int_1); } return ""; } } if (int_1 < 0) { return str; } byte[] sourceArray = Encoding.Default.GetBytes(string_0); if (sourceArray.Length <= int_0) { return str; } int length = sourceArray.Length; if (sourceArray.Length <= (int_0 + int_1)) { int_1 = sourceArray.Length - int_0; string_1 = ""; } else { length = int_1 + int_0; } int num2 = int_1; int[] numArray = new int[int_1]; byte[] destinationArray = null; int num3 = 0; for (int i = int_0; i < length; i++) { if (sourceArray[i] > 0x7f) { num3++; if (num3 == 3) { num3 = 1; } } else { num3 = 0; } numArray[i] = num3; } if ((sourceArray[length - 1] > 0x7f) && (numArray[int_1 - 1] == 1)) { num2 = int_1 + 1; } destinationArray = new byte[num2]; Array.Copy(sourceArray, int_0, destinationArray, 0, num2); return (Encoding.Default.GetString(destinationArray) + string_1); } #endregion }