在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裏的方法進行加密解密 否則會報空指針異常


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章