描述
由于项目需要,需要对HTTP请求参数和响应的参数进行加密,故找了个工具类。
使用
加密工具类AES
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.SecureRandom;
/**
* AES 对称算法加密/解密工具类
*/
public class AESUtils {
/**
* 密钥长度: 128, 192 or 256
*/
private static final int KEY_SIZE = 128;
/**
* 加密/解密算法名称
*/
private static final String ALGORITHM = "AES";
/**
* 随机数生成器(RNG)算法名称
*/
private static final String RNG_ALGORITHM = "SHA1PRNG";
/**
* AES加密/解密用的原始密码
*/
private static final String key = "abdcdefgijk";
/**
* 生成密钥对象
*/
private static SecretKey generateKey(byte[] key) throws Exception {
// 创建安全随机数生成器
SecureRandom random = SecureRandom.getInstance(RNG_ALGORITHM);
// 设置 密钥key的字节数组 作为安全随机数生成器的种子
random.setSeed(key);
// 创建 AES算法生成器
KeyGenerator gen = KeyGenerator.getInstance(ALGORITHM);
// 初始化算法生成器
gen.init(KEY_SIZE, random);
// 生成 AES密钥对象, 也可以直接创建密钥对象: return new SecretKeySpec(key, ALGORITHM);
return gen.generateKey();
}
/**
* 数据加密: 明文 -> 密文
*/
public static byte[] encrypt(byte[] plainBytes, byte[] key) throws Exception {
// 生成密钥对象
SecretKey secKey = generateKey(key);
// 获取 AES 密码器
Cipher cipher = Cipher.getInstance(ALGORITHM);
// 初始化密码器(加密模型)
cipher.init(Cipher.ENCRYPT_MODE, secKey);
// 加密数据, 返回密文
byte[] cipherBytes = cipher.doFinal(plainBytes);
return cipherBytes;
}
/**
* 数据解密: 密文 -> 明文
*/
public static byte[] decrypt(byte[] cipherBytes, byte[] key) throws Exception {
// 生成密钥对象
SecretKey secKey = generateKey(key);
// 获取 AES 密码器
Cipher cipher = Cipher.getInstance(ALGORITHM);
// 初始化密码器(解密模型)
cipher.init(Cipher.DECRYPT_MODE, secKey);
// 解密数据, 返回明文
byte[] plainBytes = cipher.doFinal(cipherBytes);
return plainBytes;
}
/**
* 加密文件: 明文输入 -> 密文输出
*/
public static void encryptFile(File plainIn, File cipherOut, byte[] key) throws Exception {
aesFile(plainIn, cipherOut, key, true);
}
/**
* 解密文件: 密文输入 -> 明文输出
*/
public static void decryptFile(File cipherIn, File plainOut, byte[] key) throws Exception {
aesFile(plainOut, cipherIn, key, false);
}
/**
* AES 加密/解密文件
* plainFile : 输入文件(原始文件 / 加密后的文件)
* cipherFile: 输出文件(加密后的文件 / 解密后的文件)
* isEncrypt : true:加密 ,false : 解密
* key : 原始加密/解密 密码的字节数组
*/
private static void aesFile(File plainFile, File cipherFile, byte[] key, boolean isEncrypt) throws Exception {
// 获取 AES 密码器
Cipher cipher = Cipher.getInstance(ALGORITHM);
// 生成密钥对象
SecretKey secKey = generateKey(key);
// 初始化密码器
cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, secKey);
// 加密/解密数据
InputStream in = null;
OutputStream out = null;
try {
if (isEncrypt) {
// 加密: 明文文件为输入, 密文文件为输出
in = new FileInputStream(plainFile);
out = new FileOutputStream(cipherFile);
} else {
// 解密: 密文文件为输入, 明文文件为输出
in = new FileInputStream(cipherFile);
out = new FileOutputStream(plainFile);
}
byte[] buf = new byte[1024];
int len = -1;
// 循环读取数据 加密/解密
while ((len = in.read(buf)) != -1) {
out.write(cipher.update(buf, 0, len));
}
out.write(cipher.doFinal()); // 最后需要收尾
out.flush();
} finally {
close(in);
close(out);
}
}
private static void close(Closeable c) {
if (c != null) {
try {
c.close();
} catch (IOException e) {
// nothing
}
}
}
public static void main(String[] args) throws Exception {
String content = "Hello world!"; // 原文内容
// String key = "123456"; // AES加密/解密用的原始密码
// 加密数据, 返回密文
byte[] cipherBytes = AESUtils.encrypt(content.getBytes(), key.getBytes());
// 解密数据, 返回明文
byte[] plainBytes = AESUtils.decrypt(cipherBytes, key.getBytes());
// 输出解密后的明文: "Hello world!"
System.out.println(new String(plainBytes));
/*
* AES 对文件的加密/解密
*/
// 将 文件demo.jpg 加密后输出到 文件demo.jpg_cipher
AESUtils.encryptFile(new File("desinput.txt"), new File("desoutput.txt"), key.getBytes());
// 将 文件demo.jpg_cipher 解密后输出到 文件demo.jpg_plain
AESUtils.decryptFile(new File("desoutput.txt"), new File("desinput2.txt"), key.getBytes());
// 对比 原文件demo.jpg 和 解密得到的文件demo.jpg_plain 两者的 MD5 将会完全相同
}
/**
* 加密
*
* @param content 需要加密的字符串
* @return
*/
public static byte[] encryptEnd(String content) {
try {
return AESUtils.encrypt(content.getBytes(), key.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
*
* @param cipherBytes 需要解密的字符串
* @return
*/
public static String decryptEnd(byte[] cipherBytes) {
try {
byte[] plainBytes = AESUtils.decrypt(cipherBytes, key.getBytes());
return new String(plainBytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
HTTP请求工具类(byte字节数组)
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* 请求工具类
*/
@Component
public class HttpHelper {
@Autowired
static CloseableHttpClient m_HttpClient;
public HttpHelper() {
m_HttpClient = HttpClients.createDefault();
}
/**
* post请求
*
* @param url 请求地址
* @param bytes 请求参数byte字节数组
* @param contentType 请求头
* @return
* @throws IOException
*/
public static byte[] post(String url, byte[] bytes, String contentType) throws IOException {
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new ByteArrayEntity(bytes));
if (contentType != null)
httpPost.setHeader("Content-type", contentType);
CloseableHttpResponse httpResponse = m_HttpClient.execute(httpPost);
try {
HttpEntity entityResponse = httpResponse.getEntity();
int contentLength = (int) entityResponse.getContentLength();
if (contentLength <= 0)
throw new IOException("No response");
byte[] respBuffer = new byte[contentLength];
if (entityResponse.getContent().read(respBuffer) != respBuffer.length)
throw new IOException("Read response buffer error");
return respBuffer;
} finally {
httpResponse.close();
}
}
/**
* post请求,返回结果字节数组
*
* @param url 请求地址
* @param bytes 请求参数byte字节数组
* @return
* @throws IOException
*/
public static byte[] post(String url, byte[] bytes) throws IOException {
return post(url, bytes, null);
}
/**
* post请求返回请求字符串
*
* @param url 请求地址
* @param bytes 请求参数byte字节数组
* @return 返回结果的字符串
* @throws IOException
*/
public static String postResultStr(String url, byte[] bytes) throws IOException {
byte[] result = post(url, bytes, null);
return new String(result, StandardCharsets.UTF_8);
}
/**
* post请求
*
* @param url 请求地址
* @param str 请求参数字符串
* @return
* @throws IOException
*/
public static String postXml(String url, String str) throws IOException {
byte[] reqBuffer = str.getBytes(StandardCharsets.UTF_8);
byte[] respBuffer = post(url, reqBuffer, "application/xml; charset=UTF-8");
String resp = new String(respBuffer, StandardCharsets.UTF_8);
return resp;
}
}
支持http请求参数为byte数组,返回亦可以为JSON字符串或者字节数组,可以自行转换解码。