JAVA-3DES对称加解密工具(不依赖第三方库)


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncryptUtil {

    // 密钥
    public static final String ENCRYPT_KEY = "ABCD1234";

    // 定义加密算法,可用DES,DESede,Blowfish
    private static final String ALGORITHM_DESDE = "DESede";

    public static void main(String[] args) {
        // 加密
        String encPassword = desEdeEncoder("123456", EncryptUtil.ENCRYPT_KEY);
        System.out.println(encPassword);
        // 解密
        String desPassword = desEdeDecoder(encPassword, EncryptUtil.ENCRYPT_KEY);
        System.out.println(desPassword);
    }


    public static boolean isEmpty(String str) {
        return null == str || str.trim().length() == 0;
    }

    /**
     * 3DES加密
     *
     * @param src
     * @param key
     * @return
     */
    public static String desEdeEncoder(String src, String key) {
        byte[] bytes = null;
        try {
            SecretKeySpec secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESDE);
            Cipher cipher = Cipher.getInstance(ALGORITHM_DESDE);
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            bytes = cipher.doFinal(src.getBytes(StandardCharsets.UTF_8));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return byte2HexStr(bytes);
    }

    /**
     * 3DES 解密
     *
     * @param dest
     * @param key
     * @return
     */
    public static String desEdeDecoder(String dest, String key) {
        String desStr;
        try {
            SecretKeySpec secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESDE);
            Cipher cipher = Cipher.getInstance(ALGORITHM_DESDE);
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            byte[] bytes = cipher.doFinal(str2ByteArray(dest));
            desStr = new String(bytes, StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return desStr;
    }


    public static String md5Digest(String src) throws NoSuchAlgorithmException {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        byte[] bytes = md5.digest(src.getBytes(StandardCharsets.UTF_8));
        return byte2HexStr(bytes);
    }

    private static byte[] build3DesKey(String keyStr) {
        byte[] bytes = new byte[24];
        byte[] temp = keyStr.getBytes(StandardCharsets.UTF_8);
        System.arraycopy(temp, 0, bytes, 0, Math.min(bytes.length, temp.length));
        return bytes;
    }

    private static byte[] str2ByteArray(String str) {
        int byteArrayLength = str.length() / 2;
        byte[] bytes = new byte[byteArrayLength];
        for (int i = 0; i < byteArrayLength; i++) {
            byte b0 = (byte) Integer.valueOf(str.substring(i * 2, i * 2 + 2), 16).intValue();
            bytes[i] = b0;
        }
        return bytes;
    }

    private static String byte2HexStr(byte[] bytes) {
        StringBuilder sbf = new StringBuilder();
        for (byte aByte : bytes) {
            String s = Integer.toHexString(aByte & 0xFF);
            if (s.length() == 1) {
                sbf.append("0");
            }
            sbf.append(s.toUpperCase());
        }
        return sbf.toString();
    }

}

你可能感兴趣的:(java)