加密工具类

This commit is contained in:
张良(004796)
2023-12-27 18:10:53 +08:00
parent b4ceb243e3
commit c90ffdbef5

View File

@@ -0,0 +1,23 @@
package com.ruoyi.cai.util;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class AES {
public static void main(String[] args) throws Exception {
String jsonData ="asdjkljfiohweioufhkjshjkvbajkdsbjksjasdhfjanskjd"; // 要加密的JSON 数据
String key = "K2AwvosrwtoAgOEp";
System.out.println(encrypt(jsonData,key));
}
public static String encrypt(String data, String key) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encryptedBytes);
}
}