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
时间: 2024-11-08 23:47:34

3DES 加解密,对长度不限制的相关文章

3DES加解密【示例】

代码 /** * 3DES加解密 */ public class DESedeUtils { private static final String ALGORITHM_MD5 = "md5"; private static final String ALGORITHM_DESEDE = "DESede";//加密算法,可用 DES,DESede,Blowfish private static final String CHARSET = "UTF-8&q

DES、3DES 加解密;MAC算法

/// <summary> /// DES.3DES 加解密:MAC算法 /// </summary> public sealed class DES_Cryptographycs { #region --- 字段 Begin --- private PaddingMode mPaddingMode; private CipherMode mCipherMode; private byte[] mbyKey; private byte[] mbyIV; #endregion ---

3DES加解密

3DES(或称为Triple DES),它相当于是对每个数据块应用三次DES加密算法.3*8字节密钥. 设Ek()和Dk()代表DES算法的加密和解密过程,K代表DES算法使用的密钥,P代表明文,C代表密文: 3DES加密过程为:C=Ek3(Dk2(Ek1(P))) 3DES解密过程为:P=Dk1((EK2(Dk3(C))) 补齐方式PKCS7              k=8,l=数据长度 01 -- if l mod k = k-1  02 02 -- if l mod k = k-2   

PHP版3DES加解密类

<?php /** * * PHP版3DES加解密类 * * 可与java的3DES(DESede)加密方式兼容 * * @Author:蓝凤(ilanfeng.com) * * @version: V0.1 2011.02.18 * */ class Cc3des{ //加密的时候只用替换key就行了,ecb模式不需要提供iv值 public $key = "0123456789QWEQWEEWQQ1234"; public $iv = "33889955"

PHP和.NET通用的加密解密函数类,均使用3DES加解密 .

以下为php代码 <PRE class=PHP name="code"> </PRE><PRE class=PHP name="code">PHP加解密函数:</PRE><PRE class=PHP name="code"> </PRE><PRE class=PHP name="code"> function encrypt($string)

PHP 3DES 加解密(CBC模式,pkcs5padding填充)

1.前言:项目中接入第三方支付遇到3DES加密,以前也没用过,搜了好多,都不适用,各种不对,后来自己结合搜到的终于弄正确了,检测地址:http://tool.chacuo.net/crypt3des. 2.下面是类 适用(CBC加密模式,pkcs5padding填充) 代码: /** * @des 3DES加密算法,cbc模式,pkcs5Padding字符填充方式 */ class TDEA{ /** * @param string $crypt 需要加密的字符串 * @param string

JAVA C++ 3DES加解密对接,转载

转载自: http://www.cnblogs.com/WonKerr/archive/2009/11/11/DES_C_JAVA.html

python的des和3des加解密

1.加密: pyDes.des(key, [mode], [IV], [pad], [padmode]) pyDes.triple_des(key, [mode], [IV], [pad], [padmode]) key -> Bytes containing the encryption key. 8 bytes for DES, 16 or 24 bytes for Triple DES mode -> Optional argument for encryption type, can

6. Java 加解密技术系列之 3DES

Java 加解密技术系列之 3DES 序 背景 概念 原理 代码实现 结束语 序 上一篇文章讲的是对称加密算法 — — DES,这篇文章打算在 DES 的基础上,继续多讲一点,也就是 3 重 DES — — Triple DES. 背景 至于 3DES 为什么会出现呢?其实,这个不难想到.由于 DES 是一种非常简便的加密算法,但是密钥长度比较短,计算量比较小,相对来说,比较容易被破解.因此,在 DES 的基础上,使用三重数据加密算法,对数据进行加密,这样来说,破解的概率就小了很多. 概念 3D