From faa12009a89e79a66b6c6f36a951abb43b194696 Mon Sep 17 00:00:00 2001 From: 21819 Date: Sat, 2 Mar 2024 12:30:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8getMimeEncoder()=E4=BF=AE?= =?UTF-8?q?=E5=A4=8Dbase64=E5=8A=A0=E5=AF=86=E7=BC=96=E7=A0=81=E4=B8=8D?= =?UTF-8?q?=E4=B8=80=E8=87=B4=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/biutag/lan/util/SmsLan.java | 37 ++++++++----- .../src/test/java/com/biutag/lan/AESUtil.java | 55 +++++++++++++++++++ .../src/test/java/com/biutag/lan/SmsTest.java | 38 ++++++------- 3 files changed, 93 insertions(+), 37 deletions(-) create mode 100644 mailbox-lan/src/test/java/com/biutag/lan/AESUtil.java diff --git a/mailbox-lan/src/main/java/com/biutag/lan/util/SmsLan.java b/mailbox-lan/src/main/java/com/biutag/lan/util/SmsLan.java index da31b2e..e5fd339 100644 --- a/mailbox-lan/src/main/java/com/biutag/lan/util/SmsLan.java +++ b/mailbox-lan/src/main/java/com/biutag/lan/util/SmsLan.java @@ -53,7 +53,7 @@ public class SmsLan { String jsonStr = JSON.toJSONString(dataMap); log.info("jsonStr: {}", jsonStr); - String key = encrypt(jsonStr, PASSWORD, true); + String key = encrypt(jsonStr, PASSWORD); log.info("key: {}", key); key = URLEncoder.encode(key, "UTF-8"); log.info("key: {}", key); @@ -87,7 +87,7 @@ public class SmsLan { String jsonStr = JSON.toJSONString(dataMap); log.info("jsonStr: {}", jsonStr); - String key = encrypt(jsonStr, PASSWORD, true); + String key = encrypt(jsonStr, PASSWORD); log.info("key: {}", key); key = URLEncoder.encode(key, "UTF-8"); log.info("key: {}", key); @@ -98,10 +98,11 @@ public class SmsLan { } return null; } + /** - * @param mobiles 手机号 多个用","隔开 (英文逗号)【必填】 - * @param content 短信内容 【必填】 - * @param orderNo 批次号 【必填】 + * @param mobiles 手机号 多个用","隔开 (英文逗号)【必填】 + * @param content 短信内容 【必填】 + * @param orderNo 批次号 【必填】 * @return */ public static String send(String mobiles, String content, String orderNo) { @@ -118,7 +119,7 @@ public class SmsLan { dataMap.put("validTime", ""); String jsonStr = JSON.toJSONString(dataMap); log.info("jsonStr: {}", jsonStr); - String key = encrypt(jsonStr, PASSWORD, true); + String key = encrypt(jsonStr, PASSWORD); log.info("key: {}", key); key = URLEncoder.encode(key, "UTF-8"); log.info("key: {}", key); @@ -171,16 +172,22 @@ public class SmsLan { } - public static String encrypt(String input, String key, Boolean bs64) throws Exception { - SecretKeySpec skey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"); + public static String encrypt(String jsonData, String key) throws Exception { + // 必须兼容MIME协议,编码器会在每76个字符后插入换行符。这使得编码后的字符串适合在MIME协议中使用 + String base64Encoded = Base64.getMimeEncoder().encodeToString(jsonData.getBytes(StandardCharsets.UTF_8)); + + // 使用AES加密编码后的字符串 + return encryptAES(base64Encoded, key); + } + + // AES 加密 + public static String encryptAES(String data, String key) throws Exception { + SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); - cipher.init(Cipher.ENCRYPT_MODE, skey); - byte[] crypted = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8)); + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); + byte[] result = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); - if (bs64) { - return Base64.getEncoder().encodeToString(crypted); - } else { - throw new UnsupportedOperationException("Non-base64 is not supported."); - } + // 必须兼容MIME协议,编码器会在每76个字符后插入换行符。这使得编码后的字符串适合在MIME协议中使用 + return Base64.getMimeEncoder().encodeToString(result); } } diff --git a/mailbox-lan/src/test/java/com/biutag/lan/AESUtil.java b/mailbox-lan/src/test/java/com/biutag/lan/AESUtil.java new file mode 100644 index 0000000..edf5a04 --- /dev/null +++ b/mailbox-lan/src/test/java/com/biutag/lan/AESUtil.java @@ -0,0 +1,55 @@ +package com.biutag.lan; + +import javax.crypto.Cipher; +import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +public class AESUtil { + + // AES加密算法 + public static String encrypt(String data, String key) { + try { + // 创建AES密钥 + SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"); + + // 创建密码器 + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + + // 初始化为加密模式的密码器 + cipher.init(Cipher.ENCRYPT_MODE, secretKey); + + // 加密 + byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); + + // 使用Base64编码 + return Base64.getEncoder().encodeToString(encrypted); + } catch (Exception e) { + throw new RuntimeException("Error encrypting data", e); + } + } + + // AES解密算法 + public static String decrypt(String encryptedData, String key) { + try { + // 创建AES密钥 + SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"); + + // 创建密码器 + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + + // 初始化为解密模式的密码器 + cipher.init(Cipher.DECRYPT_MODE, secretKey); + + // 解密 + byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encryptedData)); + + // 返回解密后的字符串 + return new String(decrypted, StandardCharsets.UTF_8); + } catch (Exception e) { + throw new RuntimeException("Error decrypting data", e); + } + } +} + + diff --git a/mailbox-lan/src/test/java/com/biutag/lan/SmsTest.java b/mailbox-lan/src/test/java/com/biutag/lan/SmsTest.java index 89c2c2f..dec0f31 100644 --- a/mailbox-lan/src/test/java/com/biutag/lan/SmsTest.java +++ b/mailbox-lan/src/test/java/com/biutag/lan/SmsTest.java @@ -26,43 +26,37 @@ public class SmsTest { String result = "dk0vL%2FHI%2FMgeyko9w%2F2buOQSLLv%2FWd4xfDWUgVC%2FQt0tf8P%2Fr%2F7iP9srgLBRnrignGZCXWeaZ8cf%0D%0AqexHGp4GO7LTM0tEg9IwOEOJdMJIbuHaFvDY8SvORhT%2BZanP8UvHUevoO6fcNoZ0yBOHceS8zZ%2Ba%0D%0Ak8fQtrgakYQV7KszrVOz%2B9EcGPFGLfWmFCsFwK1P8hPqywvtpZprdXSj4d2NrnhWCbt%2BZVt9GX2u%0D%0AAPRkQuYQAmNFFGZM1iF0Jx2vt%2BoN"; - //System.out.println("jsonStr:"+jsonStr); String key = encrypt(jsonStr, PWD); //System.out.println("key:"+key); key = URLEncoder.encode(key, "UTF-8"); + System.out.println("key:" + key); + System.out.println("result:" + result); System.out.println(result.equals(key)); } catch (Exception e) { e.printStackTrace(); } - - - } + // 使用Base64编码后进行AES加密的方法 + public static String encrypt(String jsonData, String key) throws Exception { + // 必须兼容MIME协议,编码器会在每76个字符后插入换行符。这使得编码后的字符串适合在MIME协议中使用 + String base64Encoded = Base64.getMimeEncoder().encodeToString(jsonData.getBytes(StandardCharsets.UTF_8)); - /** - * AES加密 - * - * @param input 明文 - * @param key 秘钥 - * @return 加密后的BASE64编码的字符串 - * @throws Exception - */ - public static String encrypt(String input, String key) throws Exception { - // 创建一个AES密钥 - SecretKeySpec skey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"); + // 使用AES加密编码后的字符串 + return encryptAES(base64Encoded, key); + } - // 创建Cipher实例 + // AES 加密 + public static String encryptAES(String data, String key) throws Exception { + SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); - cipher.init(Cipher.ENCRYPT_MODE, skey); - - // 执行加密操作 - byte[] crypted = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8)); + cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); + byte[] result = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)); - // 使用Base64进行编码 - return Base64.getEncoder().encodeToString(crypted); + // 必须兼容MIME协议,编码器会在每76个字符后插入换行符。这使得编码后的字符串适合在MIME协议中使用 + return Base64.getMimeEncoder().encodeToString(result); }