package com.cloud7.utils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
public class SignAesUtil {
/**
*解密AES
* @param cipherStr
* @return
*/
public static String decrypt(String cipherStr) {
String key = "webthanos123.com";
String IV = "wivthanos123.com";
String result="";
BASE64Decoder decoder = new BASE64Decoder();
IvParameterSpec iv = new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8));
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
result = new String(cipher.doFinal(decoder.decodeBuffer(cipherStr)), StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
// public static String encrypt(String cipherStr) {
// String key = "webthanos123.com";
// String IV = "wivthanos123.com";
// String result="";
// BASE64Encoder encoder = new BASE64Encoder();
// IvParameterSpec iv = new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8));
// SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
// try {
// Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
// byte[] dataBytes = cipherStr.getBytes();
// int blockSize = cipher.getBlockSize();
// int plaintextLength = dataBytes.length;
// if (plaintextLength % blockSize != 0) {
// plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
// }
// byte[] plaintext = new byte[plaintextLength];
// System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
// byte[] encrypted = cipher.doFinal(plaintext);
// result=encoder.encode(encrypted).trim();
// // new String(cipher.doFinal(encoder.encode(cipherStr.getBytes(StandardCharsets.UTF_8)).getBytes(StandardCharsets.UTF_8)));
// } catch (Exception e) {
// e.printStackTrace();
// }
// return result;
// }
/**
* 加密
* @param data
* @return
* @throws Exception
*/
public static String encryptAES(String data) throws Exception {
String key = "webthanos123.com";
String IV = "wivthanos123.com";
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int plaintextLength = dataBytes.length;
// if (plaintextLength % blockSize != 0) {
// plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
// }
// byte[] plaintext = new byte[plaintextLength];
// System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
IvParameterSpec iv = new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8));
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] encrypted = cipher.doFinal(dataBytes);
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(encrypted).trim(); // BASE64做转码。
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) throws Exception {
String body=encryptAES("{\"userName\":\"lic\",\"password\":\"Lic135.com\",\"code\":\"d8gp\"}");
System.out.println(body);
//HeKB9CslDyypING/BZqBfJxhKhBHJ8UUWvnG7ygVSBDqE8YhO3ijMhSEK4b121KLxUQjPUMJbwma4PTYeeUI3A==
//HeKB9CslDyypING/BZqBfJxhKhBHJ8UUWvnG7ygVSBDqE8YhO3ijMhSEK4b121KLxUQjPUMJbwma4PTYeeUI3A==
System.out.println(decrypt(body));
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix