3DES 加解密,对长度不限制

#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

 2015-1-13 添加

在我传递key时,出现

指定键的大小对于此算法无效。和 <wbr>指定的初始化向量(IV)与此算法的块大小不匹配。错误处理

查资料,原来是key的长度大于24了,(IV则是8位,但我没有传递)

修改后的3DES算法

#region 3DES

        /// <summary>

        /// 3DES加密

        /// </summary>

        /// <param name="strString">需加密的字符串</param>

        /// <param name="strKey">密匙</param>

        /// <returns></returns>

        public static string DES3Encrypt(string strString, string strKey)

        {

            byte[] bMsg = Encoding.UTF8.GetBytes(strString);

            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

            DES.Mode = CipherMode.ECB;



            string key = strKey;

            while (key.Length < 24)

            {

                key += strKey;

            }

            byte[] digestOfPassword = Encoding.UTF8.GetBytes(key);

            byte[] keyBytes = new byte[24];

            Array.Copy(digestOfPassword.Take(24).ToArray(), keyBytes, digestOfPassword.Take(24).ToArray().Length);



            DES.Key = keyBytes;

            ICryptoTransform DESEncrypt = DES.CreateEncryptor();

            var dd = DESEncrypt.TransformFinalBlock(bMsg, 0, bMsg.Length);

            return Convert.ToBase64String(dd);

        }



        /// <summary>

        /// 3DES解密

        /// </summary>

        /// <param name="strString">需解密的字符串</param>

        /// <param name="strKey">密匙</param>

        /// <returns></returns>

        public static string DES3Decrypt(string strString, string strKey)

        {

            TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

            DES.Mode = CipherMode.ECB;

            string key = strKey;

            while (key.Length < 24)

            {

                key += strKey;

            }

            byte[] digestOfPassword = Encoding.UTF8.GetBytes(key);

            byte[] keyBytes = new byte[24];

            Array.Copy(digestOfPassword.Take(24).ToArray(), keyBytes, digestOfPassword.Take(24).ToArray().Length);

            DES.Key = keyBytes;

            ICryptoTransform DESDecrypt = DES.CreateDecryptor();

            string result = "";

            try

            {

                byte[] Buffer = Convert.FromBase64String(strString);

                byte[] bb = DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length);

                result = Encoding.UTF8.GetString(bb);

            }

            catch (Exception e)

            {

                throw e;

            }

            return result;

        }

        #endregion

 

你可能感兴趣的:(3des)