随笔 - 127  文章 - 0  评论 - 0  阅读 - 74293

java aes加密解密

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


}
posted on   groby  阅读(278)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示