#region 3DES /// <summary> /// 3DES加密 /// </summary> /// <param name="strString">需加密的字符串</param> /// <param name="strKey">密匙</param> /// <returns></returns> public static string DES3Encrypt(string strString, string strKey) { strString = strString + "".PadLeft(8); byte[] bMsg = Encoding.GetEncoding("GBK").GetBytes(strString); int l = (bMsg.Length / 16 + 1) * 16; byte[] btMsg = new byte[l]; Array.Copy(bMsg, btMsg, bMsg.Length); byte[] digestOfPassword = Encoding.Default.GetBytes(strKey); byte[] keyBytes = new byte[24]; Array.Copy(digestOfPassword, keyBytes, digestOfPassword.Length); for (int j = 0, k = 16; j < 8; ) { keyBytes[k++] = keyBytes[j++]; } TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); DES.Mode = CipherMode.ECB; ICryptoTransform DESEncrypt = DES.CreateEncryptor(keyBytes, keyBytes); var dd = DESEncrypt.TransformFinalBlock(btMsg, 0, btMsg.Length); return byte2hex(dd).ToString().Substring(0, (bMsg.Length / 8 + 1) * 16); } public static String getAdd(int length, String strKey) { byte[] btMsg = new byte[length / 2]; byte[] digestOfPassword = Encoding.UTF8.GetBytes(strKey); //byte[] digestOfPassword = Encoding.Default.GetBytes(strKey); byte[] keyBytes = new byte[24]; Array.Copy(digestOfPassword, keyBytes, digestOfPassword.Length); for (int j = 0, k = 16; j < 8; ) { keyBytes[k++] = keyBytes[j++]; } TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); DES.Mode = CipherMode.ECB; ICryptoTransform DESEncrypt = DES.CreateEncryptor(keyBytes, keyBytes); var dd = DESEncrypt.TransformFinalBlock(btMsg, 0, btMsg.Length); String rtn = byte2hex(dd); return rtn.Substring(length); } /// <summary> /// 字节数组转16进制字符串 /// </summary> /// <param name="bytes"></param> /// <returns></returns> private static string byte2hex(byte[] dd) { StringBuilder ret = new StringBuilder(); foreach (var item in dd) { ret.AppendFormat("{0:X2}", item); } return ret.ToString(); } /// <summary> /// 字符串转16进制字节数组 /// </summary> /// <param name="hexString"></param> /// <returns></returns> private static byte[] strToToHexByte(string hexString) { hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString += " "; byte[] returnBytes = new byte[hexString.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16); return returnBytes; } /// <summary> /// 3DES解密 /// </summary> /// <param name="strString">需解密的字符串</param> /// <param name="strKey">密匙</param> /// <returns></returns> public static string DES3Decrypt(string strString, string strKey) { strString += getAdd(strString.Length, strKey); byte[] digestOfPassword = Encoding.Default.GetBytes(strKey); byte[] keyBytes = new byte[24]; Array.Copy(digestOfPassword, keyBytes, digestOfPassword.Length); for (int j = 0, k = 16; j < 8; ) { keyBytes[k++] = keyBytes[j++]; } TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider(); DES.Mode = CipherMode.ECB; ICryptoTransform DESDecrypt = DES.CreateDecryptor(keyBytes, keyBytes); string result = ""; try { byte[] Buffer = strToToHexByte(strString); byte[] bb = DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length); result = Encoding.GetEncoding("GBK").GetString(bb); } catch (Exception e) { throw e; } return result; } #endregion
时间: 2024-11-08 23:47:34