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 2022-12-01 15:11  groby  阅读(270)  评论(0编辑  收藏  举报