/// <summary> /// C#/PHP/JSP 3DES 加密与解密(只支持UTF-8编码) /// </summary> public class Crypto3DES { /// <summary> /// 默认密钥 /// </summary> private string Keys; /// <summary> /// 密钥与加密字符串不足8字符时的填充字符 /// </summary> private char paddingChar = ' '; /// <summary> /// 实例化 Crypto3DES 类 /// </summary> /// <param name="key">密钥</param> public Crypto3DES(string key) { this.Keys = key; } /// <summary> /// 获取密钥,不足8字符的补满8字符,超过8字符的截取前8字符 /// </summary> /// <param name="key">密钥</param> /// <returns></returns> private string GetKeyCode(string key) { if (key.Length > 8) return key.Substring(0, 8); else return key.PadRight(8, paddingChar); } /// <summary> /// 获取加密字符串,不足8字符的补满8字符 /// </summary> /// <param name="strString">The STR string.</param> /// <returns></returns> private string GetString(string strString) { if (strString.Length < 8) return strString.PadRight(8, paddingChar); return strString; } /// <summary> /// 加密 /// </summary> /// <param name="strString">加密字符串</param> /// <returns></returns> public string Encrypt(string strString) { try { strString = this.GetString(strString); DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = Encoding.UTF8.GetBytes(this.GetKeyCode(this.Keys)); DES.Mode = CipherMode.ECB; DES.Padding = PaddingMode.Zeros; ICryptoTransform DESEncrypt = DES.CreateEncryptor(); byte[] Buffer = Encoding.UTF8.GetBytes(strString); return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)); } catch (Exception ex) { return ex.Message; } } /// <summary> /// 解密 /// </summary> /// <param name="strString">解密字符串</param> /// <returns></returns> public string Decrypt(string strString) { try { DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); DES.Key = Encoding.UTF8.GetBytes(this.GetKeyCode(this.Keys)); DES.Mode = CipherMode.ECB; DES.Padding = PaddingMode.Zeros; ICryptoTransform DESDecrypt = DES.CreateDecryptor(); byte[] Buffer = Convert.FromBase64String(strString); return UTF8Encoding.UTF8.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length)).Replace("