欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java中AES加密,解密的工具类,以及MD5加密

程序员文章站 2022-07-13 13:39:58
...
/**
 * zhaotengtao
 * AES工具类 20/11/13.
 */
public class AESUtils {

    /**
     * 加密方法
     *
     * @param encData    要加密的数据
     * @param key        加密的**
     * @param tripIv     加密的偏移量
     * @param encodeName 加密的编码
     * @return
     */
    public static String encrypt(String encData, String key, String tripIv, String encodeName) {
        try {
            byte[] raw = key.getBytes(encodeName);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES / CBC / PKCS5Padding");// "算法/模式/补码方式"
            IvParameterSpec iv = new IvParameterSpec(tripIv.getBytes(encodeName));// 使用CBC模式,需要一个向量iv,可增加加密算法的强度
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            byte[] encrypted = cipher.doFinal(encData.getBytes(encodeName));
            return Base64.encodeBase64String(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密方法
     *
     * @param decData    解密的数据
     * @param key        解密的**
     * @param tripIv     解密的偏移量
     * @param encodeName 解密的编码
     * @return
     */
    public static String decrypt(String decData, String key, String tripIv, String encodeName) {
        try {
            byte[] raw = key.getBytes(encodeName);
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES / CBC / PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec(tripIv.getBytes(encodeName));
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = Base64.decodeBase64(decData);
            byte[] original = cipher.doFinal(encrypted1);
            return new String(original, encodeName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * MD5加密
     *
     * @param str
     * @return
     */
    public static String md5(String str) {
        if (str == null) {
            return null;
        }
        MessageDigest messageDigest = null;
        try {
            messageDigest = java.security.MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(str.getBytes("utf-8"));
        } catch (NoSuchAlgorithmException e) {
            return str;
        } catch (UnsupportedEncodingException e) {
            return str;
        }
        byte[] byteArray = messageDigest.digest();
        StringBuilder md5StrBuff = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
            else
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return md5StrBuff.toString();
    }

}

MD5验证网址:https://md5jiami.51240.com
Java中AES加密,解密的工具类,以及MD5加密

AES加密解密网址:http://tool.chacuo.net/cryptaes
Java中AES加密,解密的工具类,以及MD5加密