铁马冰河2000

导航

Java加解密-DES3对称加密算法

3重DES

  3DES的好处是密钥长度增加。迭代次数增加。

  实现的方式通常由JDK和BC两种,实现的方式和DES的实现方式大同小异:

============================================================================DES3加解密工具类:

import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class SecurityDES3 {
    
    public static String DES3_MODE_JDK = "jdk";
    public static String DES3_MODE_BC = "bouncyCastle";
    private Key key;
    
    //单例模式
    private static SecurityDES3 securityDES3;
    public static SecurityDES3 getInstance(String password, String succurityMode) {
        if(securityDES3 == null) {
            securityDES3 = new SecurityDES3(password, succurityMode);
        }
        return securityDES3;
    }
    
    private SecurityDES3(String password, String succurityMode)  {
        try{
            //1.生成key
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(password.getBytes());
            KeyGenerator keyGenerator;
            if(StringUtils.isNotBlank(succurityMode) && StringUtils.equals(DES3_MODE_BC, succurityMode)) {
                 Security.addProvider(new BouncyCastleProvider());
                 keyGenerator = KeyGenerator.getInstance("desede", "BC");
            }else {
                keyGenerator = KeyGenerator.getInstance("desede");
            }
            keyGenerator.init(168, secureRandom);//初始化key的长度,只能是128,
            SecretKey secretKey = keyGenerator.generateKey();//生成key
            byte[] keyBytes = secretKey.getEncoded();//得到key的字节数组
            //2.key的转换
            DESedeKeySpec deSedeKeySpec = new DESedeKeySpec(keyBytes);
            SecretKeyFactory factory = SecretKeyFactory.getInstance("desede");//秘密密钥工厂
            key = factory.generateSecret(deSedeKeySpec);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public byte[] encrypt(byte[] array) {
        try{
            Cipher cipher = Cipher.getInstance("desede/ECB/PKCS5Padding");//加解密方式+工作模式+填充方式
            cipher.init(Cipher.ENCRYPT_MODE, key);//以加密模式初始化
            return cipher.doFinal(array);
        } catch(Exception e ){
            e.printStackTrace();
        }
        return null;
    }
    
    public byte[] decrypt(byte[] array) {
        try{
            Cipher cipher = Cipher.getInstance("desede/ECB/PKCS5Padding");//加解密方式+工作模式+填充方式
            cipher.init(Cipher.DECRYPT_MODE, key);
            return cipher.doFinal(array);
        } catch(Exception e ){
            e.printStackTrace();
        }
        return null;
    }
}

============================================================================DES3加解密工具测试类:

    @Test
    public void test_DES3() {
        String src = "面向对象编程,object-oriented!@#*5"; // 需要加密的原始字符串
        String password = "123456";
        SecurityDES3 securityJdkDES3 = SecurityDES3.getInstance(password, SecurityDES3.DES3_MODE_JDK);
        byte[] encodeJdkDES3 = securityJdkDES3.encrypt(src.getBytes());
        System.out.println("JDK DES3加密:" + Base64.encodeBase64String(encodeJdkDES3));
        byte[] decodeJdkDES3 = securityJdkDES3.decrypt(encodeJdkDES3);
        System.out.println("JDK DES3解密:" + new String(decodeJdkDES3));
        
        SecurityDES3 securityBcDES3 = SecurityDES3.getInstance(password, SecurityDES3.DES3_MODE_BC);
        byte[] encodeBcDES3 = securityBcDES3.encrypt(src.getBytes());
        System.out.println("bouncyCastle DES3加密:" + Base64.encodeBase64String(encodeBcDES3));
        byte[] decodeBcDES3 = securityBcDES3.decrypt(encodeBcDES3);
        System.out.println("bouncyCastle DES3解密:" + new String(decodeBcDES3));
    }

 

posted on 2022-01-23 22:34  铁马冰河2000  阅读(1088)  评论(0编辑  收藏  举报