热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

#region3DES<summary>3DES加密<summary>
#region 3DES
        /// 
        /// 3DES加密
        /// 
        /// 需加密的字符串
        /// 密匙
        /// 
        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);
        }

        ///  
        /// 字节数组转16进制字符串 
        ///  
        ///  
        ///  
        private static string byte2hex(byte[] dd)
        {
            StringBuilder ret = new StringBuilder();
            foreach (var item in dd)
            {
                ret.AppendFormat("{0:X2}", item);
            }
            return ret.ToString();
        }
        ///  
        /// 字符串转16进制字节数组 
        ///  
        ///  
        ///  
        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[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
        /// 
        /// 3DES解密
        /// 
        /// 需解密的字符串
        /// 密匙
        /// 
        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
        /// 
        /// 3DES加密
        /// 
        /// 需加密的字符串
        /// 密匙
        /// 
        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);
        }

        /// 
        /// 3DES解密
        /// 
        /// 需解密的字符串
        /// 密匙
        /// 
        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

 


推荐阅读
author-avatar
QJ974
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有