铁马冰河2000

导航

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

对称加密算法

  加密密钥和解密密钥相同,加密运算和解密运算互为逆运算。是一种初等的加密算法。主要的算法有DES(3DES)、AES、PBE、IDEA。

DES

  数据加密标准(Data Encryption Standard),IBM提交的算法。

在使用bc进行DES加解密的时候除了需要使用Security.addProvider()方法增加一个BouncyCastle,还需要指定密钥生成器的提供者为BC,如上面的代码的57行,否则会默认使用sun的JCE。应用场景:

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

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

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class SecurityDES {
    
    public static String DES_MODE_JDK = "jdk";
    public static String DES_MODE_BC = "bouncyCastle";
    private Key key;

    //单例模式
    private static SecurityDES securityDES;
    public static SecurityDES getInstance(String password, String succurityMode) {
        if(securityDES == null) {
            securityDES = new SecurityDES(password, succurityMode);
        }
        return securityDES;
    }
    
    private SecurityDES(String password, String succurityMode)  {
        try{
            KeyGenerator generator;
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(password.getBytes());
            if(StringUtils.isNotBlank(succurityMode) && StringUtils.equals(DES_MODE_BC, succurityMode)) {
                Security.addProvider(new BouncyCastleProvider());//增加provider
                generator = KeyGenerator.getInstance("DES", "BC");
                generator.init(56, secureRandom);
                generator.getProvider();
            }else {
                generator = KeyGenerator.getInstance("DES");
                generator.init(56, secureRandom);
            }
            key = generator.generateKey();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public byte[] encrypt(byte[] array) {
        try{
            Cipher cipher = Cipher.getInstance("DES/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("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key);
            return cipher.doFinal(array);
        } catch(Exception e ){
            e.printStackTrace();
        }
        return null;
    }
}

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

@Test
    public void test_DES() {
        String src = "面向对象编程,object-oriented!@#*5"; // 需要加密的原始字符串
        String password = "123456";
        SecurityDES securityJdkDES = SecurityDES.getInstance(password, SecurityDES.DES_MODE_JDK);
        byte[] encodeJdkDES = securityJdkDES.encrypt(src.getBytes());
        System.out.println("JDK DES加密:" + Base64.encodeBase64String(encodeJdkDES));
        byte[] decodeJdkDES = securityJdkDES.decrypt(encodeJdkDES);
        System.out.println("JDK DES解密:" + new String(decodeJdkDES));
        
        SecurityDES securityBcDES = SecurityDES.getInstance(password, SecurityDES.DES_MODE_BC);
        byte[] encodeBcDES = securityBcDES.encrypt(src.getBytes());
        System.out.println("bouncyCastle DES加密:" + Base64.encodeBase64String(encodeBcDES));
        byte[] decodeBcDES = securityBcDES.decrypt(encodeBcDES);
        System.out.println("bouncyCastle DES解密:" + new String(decodeBcDES));
    }

 

 

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