Java加密工具类EncryptUtils

该文章提供了一个Java实现的加密工具类EncryptionUtils,包含了MD5、SHA系列、AES和DES等常见加密算法。类中使用了String.format()确保字节到十六进制字符串的转换正确,并使用UTF-8字符集保证兼容性。同时提供了Base64编码和解码的方法。
摘要由CSDN通过智能技术生成

Java 提供了一些常见的加密算法,如 MD5、SHA、AES、DES,现将这些实现方法放进加密工具类 EncryptionUtils

  • 使用了 String.format() 来确保每个字节都能够正确的被转化为成十六进制字符串,而且不会因为缺少前导零而导致结果不正确
  • 配合 StringBuilder 提高字符串处理效率
  • 使用标准字符集 UTF-8 替换默认字符集,保证不同系统的兼容性和一致性。
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;

public class EncryptUtils {
	/**
	 * 将字节数组转化为 16 进制字符串
	 * @param bytes 字节数组
	 * @return 16 进制字符串
	 */
	private static String byteArrayToHexString(byte[] bytes) {
		StringBuilder sb = new StringBuilder();
		for (byte b : bytes) {
			sb.append(String.format("%02x", b & 0xff));
		}
		return sb.toString();
	}

	/**
	 * 将 16 进制字符串转化为字节数组
	 * @param hexString 16 进制字符串
	 * @return 字节数组
	 */
	private static byte[] hexStringToByteArray(String hexString) {
		int len = hexString.length();
		byte[] bytes = new byte[len / 2];
		for (int i = 0; i < len; i += 2) {
			bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
					+ Character.digit(hexString.charAt(i+1), 16));
		}
		return bytes;
	}

	/**
	 * 对字符串进行 Base64 编码
	 * @param str 需要进行编码的字符串
	 * @return Base64 编码后的字符串
	 */
	public static String encodeWithBase64(String str) {
		byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
		byte[] encoded = Base64.getEncoder().encode(bytes);
		return new String(encoded, StandardCharsets.UTF_8);
	}

	/**
	 * 对 Base64 编码的字符串进行解码
	 * @param encodedStr 需要进行解码的 Base64 编码后的字符串
	 * @return 解码后的字符串
	 */
	public static String decodeWithBase64(String encodedStr) {
		byte[] encoded = encodedStr.getBytes(StandardCharsets.UTF_8);
		byte[] bytes = Base64.getDecoder().decode(encoded);
		return new String(bytes, StandardCharsets.UTF_8);
	}

	/**
	 * 使用 MD5 算法加密字符串
	 * @param str 待加密的字符串
	 * @return 加密后的字符串
	 */
	public static String encryptWithMD5(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			byte[] hash = md.digest(str.getBytes());
			return byteArrayToHexString(hash);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 使用 SHA-1 算法加密字符串
	 * @param str 待加密的字符串
	 * @return 加密后的字符串
	 */
	public static String encryptWithSHA1(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-1");
			byte[] hash = md.digest(str.getBytes(StandardCharsets.ISO_8859_1));
			return byteArrayToHexString(hash);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 使用 SHA-256 算法加密字符串
	 * @param str 待加密的字符串
	 * @return 加密后的字符串
	 */
	public static String encryptWithSHA256(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-256");
			byte[] hash = md.digest(str.getBytes(StandardCharsets.UTF_8));
			return byteArrayToHexString(hash);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 使用 SHA-512 算法加密字符串
	 * @param str 待加密的字符串
	 * @return 加密后的字符串
	 */
	public static String encryptWithSHA512(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-512");
			byte[] hash = md.digest(str.getBytes(StandardCharsets.UTF_8));
			return byteArrayToHexString(hash);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 使用 SHA3-256 算法加密字符串
	 * @param str 待加密的字符串
	 * @return 加密后的字符串
	 */
	public static String encryptWithSHA3_256(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA3-256");
			byte[] hash = md.digest(str.getBytes(StandardCharsets.UTF_8));
			return byteArrayToHexString(hash);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 使用 SHA3-512 算法加密字符串
	 * @param str 待加密的字符串
	 * @return 加密后的字符串
	 */
	public static String encryptWithSHA3_512(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA3-512");
			byte[] hash = md.digest(str.getBytes(StandardCharsets.UTF_8));
			return byteArrayToHexString(hash);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 使用 AES 算法加密字符串
	 * @param key 密钥
	 * @param str 需要加密的数据
	 * @return 加密后的字符串
	 */
	public static String encryptWithAES(String str, String key) {
		try {
			byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
			SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
			byte[] encrypted = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8));
			return Base64.getEncoder().encodeToString(encrypted);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 使用 AES 算法解密字符串
	 * @param key 密钥
	 * @param str 需要加密的数据
	 * @return 解密后的字符串
	 */
	public static String decryptWithAES(String str, String key) {
		try {
			byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8);
			SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
			cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
			byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(str));
			return new String(decrypted, StandardCharsets.UTF_8);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 使用 DES 算法加密字符串
	 * @param key 密钥
	 * @param str 需要加密的字符串
	 * @return 加密后的字符串
	 */

	public static String encryptWithDES(String str, String key) {
		try {
			byte[] iv = new byte[8];
			new SecureRandom().nextBytes(iv);

			DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

			Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
			IvParameterSpec ivSpec = new IvParameterSpec(iv);
			cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivSpec);

			byte[] encrypted = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8));
			byte[] combined = Arrays.copyOf(iv, iv.length + encrypted.length);
			System.arraycopy(encrypted, 0, combined, iv.length, encrypted.length);

			return Base64.getEncoder().encodeToString(combined);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 使用 DES 算法解密字符串
	 * @param key 密钥
	 * @param str 需要加密的字符串
	 * @return 加密后的字符串
	 */
	public static String decryptWithDES(String str, String key) {
		try {
			Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
			DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
			SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
			cipher.init(Cipher.DECRYPT_MODE, secretKey);
			byte[] decoded = Base64.getDecoder().decode(str);
			byte[] decrypted = cipher.doFinal(decoded);
			return new String(decrypted, StandardCharsets.UTF_8);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获取字符串的加密值
	 * @param plainText 明文字符串
	 * @param encryptType 加密算法
	 * @return 加密值
	 */
	private static String encrypt(String plainText, String encryptType) {
		try {
			MessageDigest messageDigest = MessageDigest.getInstance(encryptType);
			messageDigest.update(plainText.getBytes(StandardCharsets.UTF_8));
			byte[] bytes = messageDigest.digest();
			return byteArrayToHexString(bytes);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		}
	}
}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值