Browse Source

使用getMimeEncoder()修复base64加密编码不一致的问题

master
21819 2 years ago
parent
commit
faa12009a8
  1. 37
      mailbox-lan/src/main/java/com/biutag/lan/util/SmsLan.java
  2. 55
      mailbox-lan/src/test/java/com/biutag/lan/AESUtil.java
  3. 38
      mailbox-lan/src/test/java/com/biutag/lan/SmsTest.java

37
mailbox-lan/src/main/java/com/biutag/lan/util/SmsLan.java

@ -53,7 +53,7 @@ public class SmsLan {
String jsonStr = JSON.toJSONString(dataMap); String jsonStr = JSON.toJSONString(dataMap);
log.info("jsonStr: {}", jsonStr); log.info("jsonStr: {}", jsonStr);
String key = encrypt(jsonStr, PASSWORD, true); String key = encrypt(jsonStr, PASSWORD);
log.info("key: {}", key); log.info("key: {}", key);
key = URLEncoder.encode(key, "UTF-8"); key = URLEncoder.encode(key, "UTF-8");
log.info("key: {}", key); log.info("key: {}", key);
@ -87,7 +87,7 @@ public class SmsLan {
String jsonStr = JSON.toJSONString(dataMap); String jsonStr = JSON.toJSONString(dataMap);
log.info("jsonStr: {}", jsonStr); log.info("jsonStr: {}", jsonStr);
String key = encrypt(jsonStr, PASSWORD, true); String key = encrypt(jsonStr, PASSWORD);
log.info("key: {}", key); log.info("key: {}", key);
key = URLEncoder.encode(key, "UTF-8"); key = URLEncoder.encode(key, "UTF-8");
log.info("key: {}", key); log.info("key: {}", key);
@ -98,10 +98,11 @@ public class SmsLan {
} }
return null; return null;
} }
/** /**
* @param mobiles 手机号 多个用","隔开 英文逗号必填 * @param mobiles 手机号 多个用","隔开 英文逗号必填
* @param content 短信内容 必填 * @param content 短信内容 必填
* @param orderNo 批次号 必填 * @param orderNo 批次号 必填
* @return * @return
*/ */
public static String send(String mobiles, String content, String orderNo) { public static String send(String mobiles, String content, String orderNo) {
@ -118,7 +119,7 @@ public class SmsLan {
dataMap.put("validTime", ""); dataMap.put("validTime", "");
String jsonStr = JSON.toJSONString(dataMap); String jsonStr = JSON.toJSONString(dataMap);
log.info("jsonStr: {}", jsonStr); log.info("jsonStr: {}", jsonStr);
String key = encrypt(jsonStr, PASSWORD, true); String key = encrypt(jsonStr, PASSWORD);
log.info("key: {}", key); log.info("key: {}", key);
key = URLEncoder.encode(key, "UTF-8"); key = URLEncoder.encode(key, "UTF-8");
log.info("key: {}", key); log.info("key: {}", key);
@ -171,16 +172,22 @@ public class SmsLan {
} }
public static String encrypt(String input, String key, Boolean bs64) throws Exception { public static String encrypt(String jsonData, String key) throws Exception {
SecretKeySpec skey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"); // 必须兼容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 cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] crypted = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8)); byte[] result = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
if (bs64) { // 必须兼容MIME协议,编码器会在每76个字符后插入换行符。这使得编码后的字符串适合在MIME协议中使用
return Base64.getEncoder().encodeToString(crypted); return Base64.getMimeEncoder().encodeToString(result);
} else {
throw new UnsupportedOperationException("Non-base64 is not supported.");
}
} }
} }

55
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);
}
}
}

38
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"; 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); //System.out.println("jsonStr:"+jsonStr);
String key = encrypt(jsonStr, PWD); String key = encrypt(jsonStr, PWD);
//System.out.println("key:"+key); //System.out.println("key:"+key);
key = URLEncoder.encode(key, "UTF-8"); key = URLEncoder.encode(key, "UTF-8");
System.out.println("key:" + key); System.out.println("key:" + key);
System.out.println("result:" + result);
System.out.println(result.equals(key)); System.out.println(result.equals(key));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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加密编码后的字符串
* AES加密 return encryptAES(base64Encoded, key);
* }
* @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");
// 创建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 cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skey); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] result = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
// 执行加密操作
byte[] crypted = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8));
// 使用Base64进行编码 // 必须兼容MIME协议,编码器会在每76个字符后插入换行符。这使得编码后的字符串适合在MIME协议中使用
return Base64.getEncoder().encodeToString(crypted); return Base64.getMimeEncoder().encodeToString(result);
} }

Loading…
Cancel
Save