3des的加、解密过程

3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法。由于计算机运算能力的增强,原版DES密码的密钥长度变得容易被暴力破解;3DES即是设计用来提供一种相对简单的方法,即通过增加DES的密钥长度来避免类似的攻击,而不是设计一种全新的块密码算法。
3DES又称Triple DES,是DES加密算法的一种模式,它使用3条56位的密钥对数据进行三次加密。数据加密标准(DES)是美国的一种由来已久的加密标准,它使用对称密钥加密法,并于1981年被ANSI组织规范为ANSI X.3.92。DES使用56位密钥和密码块的方法,而在密码块的方法中,文本被分成64位大小的文本块然后再进行加密。比起最初的DES,3DES更为安全。
3DES(即Triple DES)是DES向AES过渡的加密算法(1999年,NIST将3-DES指定为过渡的加密标准),是DES的一个更安全的变形。它以DES为基本模块,通过组合分组方法设计出分组加密算法,其具体实现如下:设Ek()和Dk()代表DES算法的加密和解密过程,K代表DES算法使用的密钥,P代表明文,C代表密文,这样:
3DES加密过程为:C=Ek3(Dk2(Ek1(P)))
3DES解密过程为:P=Dk1(EK2(Dk3(C)))
代码如下:

/**
	* @Description:	DES3 解密   
	* @param key
	* @param src
	* @return String
	 */
    public static String decryptEde(String key, String src) // throws  
                                                            // DecryptException  
    {  
        String plainText = null;  
        try  
        {  
            byte[] keyBytes = new byte[24]; // DES3 为24Bytes密钥  
            asciiToBcdBytes(key, keyBytes, Math.min(32, key.length()) / 2);  

            for (int i = 0; i < 8; ++i)  
                keyBytes[16 + i] = keyBytes[i];  

            // 从原始密匙数据创建一个DESKeySpec对象  
            KeySpec dks = new DESedeKeySpec(keyBytes);  

            // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成  
            // 一个SecretKey对象  
            SecretKey secKey = SecretKeyFactory.getInstance("DESede").generateSecret(dks);  

            // Cipher对象实际完成解密操作  
            Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");  

            // DES算法要求有一个可信任的随机数源  
            SecureRandom sr = new SecureRandom();  

            // 用密匙初始化Cipher对象  
            cipher.init(Cipher.DECRYPT_MODE, secKey, sr);  

            int count = (src.length() + 1) / 2;  
            byte[] inputBytes = new byte[count];  
            asciiToBcdBytes(src, inputBytes, count);  

            // 正式执行解密操作  
            byte[] decryptBytes = cipher.doFinal(inputBytes);  

            int olen = decryptBytes.length - 1;  
            for (; decryptBytes[olen] == 0 && olen >= 0; --olen)  
            {  
            }  

            plainText = new String(decryptBytes, 0, olen + 1);  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  

        return plainText;  
    }  

    private static void asciiToBcdBytes(String str, byte[] hex, int count)// throws  
                                                                            // Exception  
    {  
        byte[] inputBytes = str.getBytes();  
        for (int i = 0, j = 0; j < inputBytes.length && i < count; ++i)  
        {  
            byte v = inputBytes[j];  
            ++j;  
            if (v <= 0x39)  
                hex[i] = (byte) (v - 0x30);  
            else  
                hex[i] = (byte) (v - 0x41 + 10);  

            hex[i] <<= 4;  

            if (j >= inputBytes.length)  
                break;  

            v = inputBytes[j];  
            ++j;  

            if (v <= 0x39)  
                hex[i] += (byte) (v - 0x30);  
            else  
                hex[i] += (byte) (v - 0x41 + 10);  
        }  
    }  

    /**
    * @Description:	DES3 加密   
    * @param key
    * @param src
    * @return String
     */
    public static String encryptEde(String key, String src)// throws  
                                                            // DecryptException  
    {  
        String plainText = null;  
        try  
        {  
            byte[] keyBytes = new byte[24]; // DES3 为24Bytes密钥  
            asciiToBcdBytes(key, keyBytes, Math.min(32, key.length()) / 2);  

            for (int i = 0; i < 8; ++i)  
                keyBytes[16 + i] = keyBytes[i];  

            // 从原始密匙数据创建一个DESKeySpec对象  
            KeySpec dks = new DESedeKeySpec(keyBytes);  

            // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成  
            // 一个SecretKey对象  
            SecretKey secKey = SecretKeyFactory.getInstance("DESede").generateSecret(dks);  

            // Cipher对象实际完成解密操作  
            Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");  

            // DES算法要求有一个可信任的随机数源  
            SecureRandom sr = new SecureRandom();  

            // 用密匙初始化Cipher对象  
            cipher.init(Cipher.ENCRYPT_MODE, secKey, sr);  

            byte[] srcBytes = src.getBytes("GBK");  
            int srcLen = srcBytes.length;  
            int encLen = ((srcLen % 8) == 0) ? srcLen : ((srcLen / 8 + 1) * 8);  

            byte[] encBytes = new byte[encLen];  
            System.arraycopy(srcBytes, 0, encBytes, 0, srcLen);  

            // 正式执行解密操作  
            byte[] encryptBytes = cipher.doFinal(encBytes);  
            plainText = bcdBytesToAscii(encryptBytes, encLen);  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  

        return plainText;  
    }  

    private static String bcdBytesToAscii(byte[] hex, int count)// throws  
                                                                // Exception  
    {  
        byte[] ascii = new byte[2 * count];  
        int t;  
        for (int i = 0; i < count; i++)  
        {  
            t = hex[i] & 0xf0;  
            t = t >> 4;  
            if (t <= 9)  
                t += '0';  
            else if (t >= 10 && t <= 15)  
                t += 'A' - 10;  
            else  
                t = '0';  
            ascii[2 * i] = (byte) t;  

            t = hex[i] & 0x0f;  
            if (t <= 9)  
                t += '0';  
            else if (t >= 10 && t <= 15)  
                t += 'A' - 10;  
            else  
                t = '0';  
            ascii[2 * i + 1] = (byte) t;  
        }  

        return (new String(ascii));  
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值