import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;


/**
 * 3DES 加密
 *
 * 密钥secretKey说明
 * java中只提供了3倍长3des的算法,也就是secretKey的长度必须是24字节,
 * 如果想要使用2倍长3des,需要自己将后8个字节补全(就是将16个字节的前8个字节补到最后,变成24字节)。
 * 如果提供的secretKey不足24字节,将会报错,如果超过24字节,将会截取前24字节作为secretKey
 */
public class DES3Util {
    private final static String iv = "ac1rm9cp";
    // 加解密统一使用的编码方式
    private final static String encoding = "UTF-8";

    /**
     * 3DES 加密数据
     *
     * @param plainText
     *            加密明文
     * @param secretKey
     *            密钥
     * @return 加密后的密文
     * @throws Exception
     */
    public static String encode(String plainText, String secretKey) throws ArithmeticException{
        try {
            return executeEncode(plainText, secretKey);
        } catch (Exception e) {
            throw new ArithmeticException("数据加解密异常,请找加密类提供方!异常信息:" + e.getMessage());
        }
    }

    public static String executeEncode(String plainText, String secretKey)
            throws Exception {
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
        byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));
        return new String(Base64.encodeBase64(encryptData),encoding);
    }

    /**
     * 3DES 解密数据
     *
     * @param encryptText
     *            解密密文
     * @param secretKey
     *            密钥
     * @return 解密后的明文
     * @throws Exception
     */
    public static String decode(String encryptText, String secretKey)throws ArithmeticException{
        try {
            return executeDecode(encryptText, secretKey);
        } catch (Exception e) {
            throw new ArithmeticException("数据加解密异常,请找加密类提供方!异常信息:" + e.getMessage());
        }
    }

    private static String executeDecode(String encryptText, String secretKey)throws Exception{
        Key deskey = null;
        DESedeKeySpec spec = new DESedeKeySpec(secretKey.getBytes());
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
        deskey = keyfactory.generateSecret(spec);
        Cipher cipher = Cipher.getInstance("desede/CBC/NoPadding");
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);
        byte[] decryptData = cipher.doFinal(Base64.decodeBase64(encryptText.getBytes(encoding)));
        return new String(decryptData, encoding).trim();
    }

    public static void main(String[] args) {
        String secretKey = "69d1GwTn4XU6GWEfK8fAA6e8nn";
        String encode = encode("墙上画凤凰,凤凰画在粉红墙。红凤凰、粉凤凰,红粉凤凰、花凤凰。红凤凰,黄凤凰,红粉凤凰,粉红凤凰,花粉花凤凰。",secretKey);
        System.out.println("加密明文:=====================》"+encode);

        System.out.println("/t/t========解密:=========");
        String decode = decode(encode,secretKey);
        System.out.println("解密明文:=====================》"+decode);
    }

}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐