package com.pajk.synhisdgszyy.utils;
import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom;
public class CryptUtil { /** * 加密 * * @param content * 需要加密的内容 * @param keyStr * 加密密钥 * * @return 加密后的内容 * @throws UnsupportedEncodingException * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public static String encrypt(String content, String keyStr) throws Exception { SecretKey secretKey = getKey(keyStr); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
// 创建密码器
Cipher cipher = Cipher.getInstance("AES");
byte[] byteContent = content.getBytes("utf-8");
// 初始化加密
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(byteContent);
// 返回加密内容
return parseByte2HexStr(result);
}
/**
* 解密
*
* @param content
* 待解密内容
* @param keyStr
* 解密密钥
* @return
* @throws UnsupportedEncodingException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*
*/
public static String decrypt(String content, String keyStr) throws Exception {
SecretKey secretKey = getKey(keyStr);
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
// 创建密码器
Cipher cipher = Cipher.getInstance("AES");
// 初始化解密
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(parseHexStr2Byte(content));
// 返回解密内容
return new String(result, "UTF-8");
}
/**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
private static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
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();
}
/**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
private static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1){
return null;
}
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
private static SecretKey getKey(String keyStr) {
try {
KeyGenerator generator = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(keyStr.getBytes());
generator.init(128, secureRandom);
return generator.generateKey();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws Exception {
// String key = "dasdsadsad32rwefrewrewrew";
// String content = "中国平安%#&,平安中国d21321324&^@!^%#&@#@";
//
// System.out.println("===========>加密前:" + content);
//
// String c1 = encrypt(content, key);
// System.out.println("===========>加密后:" + c1);
//
// String c2 = decrypt(c1, key);
// System.out.println("===========>解密后:" + c2);
System.out.println(encrypt("123123123", "123123123"));
}
复制代码
}