常用的加解密算法

包含MD5、SHA1、SHA256、AES

工具类

import org.apache.commons.codec.binary.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CryptoUtil {

    private static final String UTF_8 = "UTF-8";

    /**
     * MD5
     * 默认得到32位的小写结果
     * 想要获取16位MD5可md5(content).substring(8, 24)
     * https://md5jiami.bmcx.com/
     */
    public static String md5(String content) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.update(content.getBytes(UTF_8));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("算法不存在", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("编码失败", e);
        }
        return byte2Hex(messageDigest.digest());
    }

    /**
     * SHA1
     * http://www.ttmd5.com/hash.php?type=5
     */
    public static String sha1(String content) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("SHA-1");
            messageDigest.update(content.getBytes(UTF_8));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("算法不存在", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("编码失败", e);
        }
        return byte2Hex(messageDigest.digest());
    }

    /**
     * SHA256
     * http://www.ttmd5.com/hash.php?type=9
     */
    public static String sha256(String content) {
        MessageDigest messageDigest = null;
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            messageDigest.update(content.getBytes(UTF_8));
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("算法不存在", e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("编码失败", e);
        }
        return byte2Hex(messageDigest.digest());
    }

    /**
     * byte转String
     */
    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }

    /**
     * AES加密/解密
     * http://tool.chacuo.net/cryptaes
     */
    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding"; // 算法名称/加密模式/数据填充方式
    private static final String AES = "AES";

    /**
     * AES加密
     */
    public static String aesEncrypt(String content, String encryptKey) {
        byte[] b = null;
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            SecretKeySpec secretKeySpec = new SecretKeySpec(encryptKey.getBytes(), AES);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
            b = cipher.doFinal(content.getBytes(UTF_8));
        } catch (NoSuchPaddingException | BadPaddingException e) {
            throw new RuntimeException("填充失败", e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("算法不存在", e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException("请输入16位的密码", e);
        } catch (UnsupportedEncodingException | IllegalBlockSizeException e) {
            throw new RuntimeException("编码失败", e);
        }
        return Base64.encodeBase64String(b);
    }

    /**
     * AES解密
     */
    public static String aesDecrypt(String content, String decryptKey) {
        byte[] decryptBytes = null;
        try {
            Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
            SecretKeySpec secretKeySpec = new SecretKeySpec(decryptKey.getBytes(), AES);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] encryptBytes = Base64.decodeBase64(content);
            decryptBytes = cipher.doFinal(encryptBytes);
        } catch (NoSuchPaddingException | BadPaddingException e) {
            throw new RuntimeException("填充失败", e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("算法不存在", e);
        } catch (InvalidKeyException e) {
            throw new RuntimeException("请输入16位的密码", e);
        } catch (IllegalBlockSizeException e) {
            throw new RuntimeException("编码失败", e);
        }
        return new String(decryptBytes);
    }

    public static void main(String[] args) {
        System.out.println("----------------------------");
        String content = "123456";
        System.out.println("明文: " + content);

        String md5 = CryptoUtil.md5(content);

        System.out.println("md5: " + md5);

        System.out.println("sha1: " + CryptoUtil.sha1(content));

        System.out.println("sha256: " + CryptoUtil.sha256(content));

        System.out.println("----------------------------");
        System.out.println("明文: " + content);

        String key = md5.substring(8, 24);
        System.out.println("密码(123456的16位MD5): " + key);

        String s = CryptoUtil.aesEncrypt(content, key);
        System.out.println("密文: " + s);

        String s1 = CryptoUtil.aesDecrypt(s, key);
        System.out.println("解密的明文: " + s1);

        System.out.println("----------------------------");
    }

}

结果

image

你可能感兴趣的:(常用的加解密算法)