package com.nova.sankuai.infra.utils; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Base64; import java.util.Random; /** * @Description AES对称加密算法工具类 * @Author CWR * @Date 2022/3/21 20:26 */ public final class AesSecretUtils { /** * 私有构造方法 */ private AesSecretUtils() { } private static final String AES = "AES"; private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding"; private static final String SHA_1 = "SHA-1"; /** * base64编码、解码对象 */ private static final Base64.Encoder ENCODER = Base64.getEncoder(); private static final Base64.Decoder DECODER = Base64.getDecoder(); /** * 加密 * * @param plaintext 明文 * @param secretKey 密钥 * @return 密文base64 */ public static String encrypt(String plaintext, String secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { // 获取密钥对象 SecretKey key = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), AES); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] plaintextBytes = plaintext.getBytes(StandardCharsets.UTF_8); // 加密 byte[] ciphertextBytes = cipher.doFinal(plaintextBytes); return new String(ENCODER.encode(ciphertextBytes), StandardCharsets.UTF_8); } /** * 解密 * * @param ciphertext 密文 * @param secretKey 密钥 * @return 密文base64 */ public static String decrypt(String ciphertext, String secretKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { // 获取密钥对象 SecretKey key = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), AES); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); byte[] ciphertextBytes = ciphertext.getBytes(StandardCharsets.UTF_8); // 解密 byte[] plaintextBytes = cipher.doFinal(DECODER.decode(ciphertextBytes)); return new String(plaintextBytes, StandardCharsets.UTF_8); } /** * SHA-1加密 * * @param plaintext * @return * @throws NoSuchAlgorithmException */ public static String sha1(String plaintext) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance(SHA_1); byte[] plaintextBytes = plaintext.getBytes(StandardCharsets.UTF_8); digest.update(plaintextBytes); // 获取字节数组 byte[] messageDigest = digest.digest(); StringBuilder hexString = new StringBuilder(); // 字节数组转换为 ⼗六进制 数 for (byte b : messageDigest) { String shaHex = Integer.toHexString(b & 0xFF); if (shaHex.length() < 2) { hexString.append(0); } hexString.append(shaHex); } return hexString.toString().toUpperCase(); } /** * 随机生成字符串. * * @param length 字符串长度 */ public static String getRandomString(int length) { String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; Random random = new Random(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { int number = random.nextInt(base.length()); sb.append(base.charAt(number)); } return sb.toString(); } /** * 获取当前时间戳 * * @return */ public static Long getTimeStamp() { return System.currentTimeMillis() / 1000L; } public static void main(String[] args) throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { String plaintext = "this is aes"; String ciphertext = encrypt(plaintext, "1234123412341234"); System.out.println("明文:" + plaintext); System.out.println("密文:" + ciphertext); String decryptString = decrypt(ciphertext, "1234123412341234"); System.out.println("解密后:" + decryptString); System.out.println("SHA-1:" + sha1(plaintext)); } }