|
|
|
|
@ -8,7 +8,9 @@ import javax.crypto.Cipher;
|
|
|
|
|
import javax.crypto.IllegalBlockSizeException; |
|
|
|
|
import javax.crypto.NoSuchPaddingException; |
|
|
|
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
|
import java.net.URLDecoder; |
|
|
|
|
import java.net.URLEncoder; |
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
import java.security.InvalidKeyException; |
|
|
|
|
import java.security.NoSuchAlgorithmException; |
|
|
|
|
@ -18,7 +20,7 @@ import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class AESTest { |
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException { |
|
|
|
|
public static void main(String[] args) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException { |
|
|
|
|
Map<String,Object> datamap = new HashMap<>(); |
|
|
|
|
datamap.put("timeStamp", "1709208164"); |
|
|
|
|
datamap.put("mobiles", "15608487213"); |
|
|
|
|
@ -27,24 +29,33 @@ public class AESTest {
|
|
|
|
|
datamap.put("appendID", ""); |
|
|
|
|
datamap.put("sendTime", ""); |
|
|
|
|
datamap.put("validTime", ""); |
|
|
|
|
String key = "fzzd12345678dxpt"; // 16字节密钥
|
|
|
|
|
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES"); |
|
|
|
|
// 创建AES加密实例
|
|
|
|
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); |
|
|
|
|
|
|
|
|
|
String s = "{\"timeStamp\":\"1709208164\",\"orderNo\":\"TEST001\",\"mobiles\":\"15608487213\",\"appendID\":\"\",\"validTime\":\"\",\"content\":\"123\",\"sendTime\":\"\"}"; |
|
|
|
|
// 加密
|
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); |
|
|
|
|
byte[] encryptedBytes = cipher.doFinal(Base64.getEncoder().encodeToString(JSON.toJSONString(datamap).getBytes(StandardCharsets.UTF_8)).getBytes()); |
|
|
|
|
|
|
|
|
|
// Base64编码加密后的数据
|
|
|
|
|
String base64Encoded = Base64.getEncoder().encodeToString(encryptedBytes); |
|
|
|
|
System.out.println(base64Encoded); |
|
|
|
|
String encrypt = encrypt(s); |
|
|
|
|
System.out.println(encrypt); |
|
|
|
|
|
|
|
|
|
System.out.println("---------------------------------------"); |
|
|
|
|
String s1 = "dk0vL%2FHI%2FMgeyko9w%2F2buBdoNASCWHQLuc0VdNhlEPotf8P%2Fr%2F7iP9srgLBRnrignGZCXWeaZ8cf%0D%0AqexHGp4GO7LTM0tEg9IwOEOJdMJIbuF1qcdjr%2Fb8OqeLkpNP1Ai6UevoO6fcNoZ0yBOHceS8zZ%2Ba%0D%0Ak8fQtrgakYQV7KszrVOz%2B9EcGPFGLfWmFCsFwK1P8hPqywvtpZprdXSj4d2NrnhWCbt%2BZVt9GX2u%0D%0AAPRkQuYQAmNFFGZM1iF0Jx2vt%2BoN"; |
|
|
|
|
System.out.println(URLDecoder.decode(s1)); |
|
|
|
|
System.out.println(URLDecoder.decode(s1, "UTF-8")); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String encrypt(String val) { |
|
|
|
|
try { |
|
|
|
|
// 加密
|
|
|
|
|
String key = "fzzd12345678dxpt"; // 16字节密钥
|
|
|
|
|
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES"); |
|
|
|
|
// 创建AES加密实例
|
|
|
|
|
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); |
|
|
|
|
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); |
|
|
|
|
byte[] encryptedBytes = cipher.doFinal(Base64.getEncoder().encodeToString(val.getBytes()).getBytes()); |
|
|
|
|
|
|
|
|
|
// Base64编码加密后的数据
|
|
|
|
|
String base64Encoded = Base64.getEncoder().encodeToString(encryptedBytes); |
|
|
|
|
return base64Encoded; |
|
|
|
|
} catch (NoSuchPaddingException | IllegalBlockSizeException | NoSuchAlgorithmException | BadPaddingException | |
|
|
|
|
InvalidKeyException e) { |
|
|
|
|
throw new RuntimeException(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|