From 86df207c37502cce1b2043e1c7c0486459eef1d6 Mon Sep 17 00:00:00 2001 From: mitao <2763622819@qq.com> Date: 星期四, 13 三月 2025 19:58:45 +0800 Subject: [PATCH] 党员、诉求评论基础代码 --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/helper/AESUtil.java | 133 ++++++++++++++++++++----------------------- 1 files changed, 62 insertions(+), 71 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/helper/AESUtil.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/helper/AESUtil.java index b1a57dc..b36bfc7 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/helper/AESUtil.java +++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/model/helper/AESUtil.java @@ -1,30 +1,36 @@ package com.panzhihua.common.model.helper; -import javax.crypto.Cipher; -import javax.crypto.SecretKey; -import javax.crypto.SecretKeyFactory; -import javax.crypto.spec.IvParameterSpec; -import javax.crypto.spec.PBEKeySpec; -import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.spec.KeySpec; import java.util.Arrays; import java.util.Base64; +import javax.crypto.*; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.PBEKeySpec; +import javax.crypto.spec.SecretKeySpec; + /** * AES 工具类 */ public class AESUtil { + private static SecretKeySpec secretKey; + private static byte[] key; /** * AES 256 加密 - * @param strToEncrypt 要加密的字符串 - * @param secKey 秘钥 - * @param salt 盐 + * + * @param strToEncrypt + * 要加密的字符串 + * @param secKey + * 秘钥 + * @param salt + * 盐 * @return 加密后的字符串 */ public static String encrypt256(String strToEncrypt, String secKey, String salt) { @@ -39,8 +45,7 @@ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec); - return Base64.getEncoder() - .encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8))); + return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8))); } catch (Exception e) { System.out.println("Error while encrypting: " + e.toString()); } @@ -49,12 +54,16 @@ /** * AES 256 解密 - * @param strToDecrypt 要解密的字符串 - * @param secKey 秘钥 - * @param salt 盐 + * + * @param strToDecrypt + * 要解密的字符串 + * @param secKey + * 秘钥 + * @param salt + * 盐 * @return 解密后的字符串 */ - public static String decrypt256( String strToDecrypt, String secKey, String salt) { + public static String decrypt256(String strToDecrypt, String secKey, String salt) { try { byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; IvParameterSpec ivspec = new IvParameterSpec(iv); @@ -73,71 +82,52 @@ return null; } - - private static SecretKeySpec secretKey; - private static byte[] key; - - public static void setKey(String myKey) - { + public static void setKey(String myKey) { MessageDigest sha = null; try { - key = myKey.getBytes("UTF-8"); + key = myKey.getBytes(StandardCharsets.UTF_8); sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key); key = Arrays.copyOf(key, 16); secretKey = new SecretKeySpec(key, "AES"); - } - catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } - catch (UnsupportedEncodingException e) { + } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } } - /** - * AES 128 加密 - * @param strToEncrypt 要加密的字符串 - * @param secret 秘钥 + * AES 128(base64)加密 + * + * @param strToEncrypt + * 要加密的字符串 + * @param secret + * 秘钥 * @return 加密后的字符串 */ public static String encrypt128(String strToEncrypt, String secret) - { - try - { - setKey(secret); - Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); - cipher.init(Cipher.ENCRYPT_MODE, secretKey); - return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))); - } - catch (Exception e) - { - System.out.println("Error while encrypting: " + e.toString()); - } - return null; + throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, + BadPaddingException, IllegalBlockSizeException { + setKey(secret); + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, secretKey); + return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8))); } /** - * AES 128 解密 - * @param strToDecrypt 要解密的字符串 - * @param secret 秘钥 + * AES 128(base64) 解密 + * + * @param strToDecrypt + * 要解密的字符串 + * @param secret + * 秘钥 * @return 解密后的字符串 */ - public static String decrypt128(String strToDecrypt, String secret) - { - try - { - setKey(secret); - Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); - cipher.init(Cipher.DECRYPT_MODE, secretKey); - return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))); - } - catch (Exception e) - { - System.out.println("Error while decrypting: " + e.toString()); - } - return null; + public static String decrypt128(String strToDecrypt, String secret) throws NoSuchPaddingException, + NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidKeyException { + setKey(secret); + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); + cipher.init(Cipher.DECRYPT_MODE, secretKey); + return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))); } } @@ -159,20 +149,21 @@ class AES128Example { public static void main(String[] args) { - String[] originalString = new String[]{"51011234567892013", "13612345678", "四川省成都市高新区", "四川省成都市高新"}; + String[] originalString = new String[] {"51011234567892013", "13612345678", "四川省成都市高新区", "四川省成都市高新"}; String key = "5165465461dsfas"; + Arrays.stream(originalString).forEach(os -> { + try { + String encryptedString = AESUtil.encrypt128(os, key); + String decryptedString = AESUtil.decrypt128(encryptedString, key); - Arrays.stream(originalString).forEach(os ->{ - String encryptedString = AESUtil.encrypt128(os, key); - String decryptedString = AESUtil.decrypt128(encryptedString, key); - - System.out.println(os); - System.out.println(encryptedString); - System.out.println(decryptedString); - - + System.out.println(os); + System.out.println(encryptedString); + System.out.println(decryptedString); + } catch (Exception e) { + e.printStackTrace(); + } }); } } \ No newline at end of file -- Gitblit v1.7.1