在Java中DES加密/解密的实现[工具类]

使用前需导入Crypto包:

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-crypto</artifactId>
    <version>5.2.1.RELEASE</version>
</dependency>

Utils工具类:

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * @author Piconjo
 * @date 2020/6/1  20:15
 */

public class DesUtil {

    private static DesUtil instance;
    private Key key;
    private Cipher encryptCipher;
    private Cipher decryptCipher;

    // 密钥
    private static final String SECRET_KEY="iloveumybaby";

    protected DesUtil() {
    }

    protected DesUtil(String strKey) {
        key = setKey(strKey);
        try {
            encryptCipher = Cipher.getInstance("DES");
            encryptCipher.init(Cipher.ENCRYPT_MODE, key);
            decryptCipher = Cipher.getInstance("DES");
            decryptCipher.init(Cipher.DECRYPT_MODE, key);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
    }

    // 初始化实例
    public static DesUtil getInstance() {
        if (instance == null) {
            instance = new DesUtil(SECRET_KEY);
        }
        return instance;
    }

    //  根据参数生成KEY
    private Key setKey(String strKey) {
        try {
            KeyGenerator _generator = KeyGenerator.getInstance("DES");
            _generator.init(new SecureRandom(strKey.getBytes()));
            return _generator.generateKey();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    // 加密 - String明文输入 String密文输出
    public String setEncString(String strMing) {
        BASE64Encoder base64en = new BASE64Encoder();
        try {
            byte[] byteMing = strMing.getBytes("UTF-8");
            byte[] byteMi = getEncCode(byteMing);
            return base64en.encode(byteMi);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 加密 - byte[]明文输入 byte[]密文输出
    private byte[] getEncCode(byte[] byteS) {
        byte[] byteFina = null;
        try {
            byteFina = encryptCipher.doFinal(byteS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byteFina;
    }

    // 解密 - String密文输入 String明文输出
    public String setDesString(String strMi) {
        BASE64Decoder base64De = new BASE64Decoder();
        try {
            byte[] byteMi = base64De.decodeBuffer(strMi);
            byte[] byteMing = getDesCode(byteMi);
            return new String(byteMing, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 解密 - byte[]密文输入 byte[]明文输出
    private byte[] getDesCode(byte[] byteD) {
        byte[] byteFina = null;
        try {
            byteFina = decryptCipher.doFinal(byteD);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return byteFina;
    }

	// 单测
    public static void main(String[] args) {
        DesUtil desUtil = DesUtil.getInstance();

        String code= dtDesUtil.setEncString("[email protected]");
        System.out.println(code);
        System.out.println(dtDesUtil.setDesString(code));
    }
}

使用须知:

使用前 调用DesUtil的getInstance()方法 生成DesUtil类的实例
然后再使用返回的DesUtil对象进行密码加密解密操作

不能直接调用DesUtil里的方法进行加密解密 否则会报空指针异常


猜你喜欢

转载自blog.csdn.net/Piconjo/article/details/106496037