1 changed files with 41 additions and 0 deletions
@ -0,0 +1,41 @@
|
||||
package com.biutag.lan; |
||||
|
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
|
||||
import javax.crypto.Cipher; |
||||
import javax.crypto.spec.SecretKeySpec; |
||||
import java.util.Base64; |
||||
|
||||
public class SmsTest { |
||||
|
||||
private static final String ALGORITHM = "AES/ECB/pkcs5padding"; |
||||
private static final String CHARSET = "UTF8"; |
||||
|
||||
// AES密钥(长度应为16个字节)
|
||||
private static final String SECRET_KEY = "fzzd12345678dxpt"; |
||||
|
||||
@Test |
||||
public void encrypt() throws Exception { |
||||
String input = "{\"timeStamp\":\"1709210345\",\"orderNo\":\"TEST001\",\"mobiles\":\"11111111111\",\"appendID\":\"\",\"validTime\":\"\",\"content\":\"123\",\"sendTime\":\"\"}"; |
||||
String key = "fzzd12345678dxpt"; |
||||
String result = "dk0vL%2FHI%2FMgeyko9w%2F2buOQSLLv%2FWd4xfDWUgVC%2FQt0tf8P%2Fr%2F7iP9srgLBRnrignGZCXWeaZ8cf%0D%0AqexHGp4GO7LTM0tEg9IwOEOJdMJIbuHaFvDY8SvORhT%2BZanP8UvHUevoO6fcNoZ0yBOHceS8zZ%2Ba%0D%0Ak8fQtrgakYQV7KszrVOz%2B9EcGPFGLfWmFCsFwK1P8hPqywvtpZprdXSj4d2NrnhWCbt%2BZVt9GX2u%0D%0AAPRkQuYQAmNFFGZM1iF0Jx2vt%2BoN"; |
||||
|
||||
// Base64编码
|
||||
String base64Data = Base64.getEncoder().encodeToString(input.getBytes(CHARSET)); |
||||
|
||||
// AES加密
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM); |
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(CHARSET), "AES"); |
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); |
||||
byte[] encryptedBytes = cipher.doFinal(base64Data.getBytes(CHARSET)); |
||||
|
||||
|
||||
// 将加密后的数据转换为Base64编码
|
||||
String encryptedBase64 = Base64.getEncoder().encodeToString(encryptedBytes); |
||||
|
||||
System.out.println(encryptedBase64); |
||||
System.out.println(result.equals(encryptedBase64)); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue