AES工具类

package com.sinosoft.cms.common.util;
import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONObject;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.List;

public class AESUtil {

/**
* AES加密
*
* @param content 要加密的内容
* @return encrypt
*/
public static String encryptWithAes(String content, String password) {
try {
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed(password.getBytes());
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, random);
SecretKey secretKey = kgen.generateKey();
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);

StringBuilder sb = new StringBuilder();
for (byte aResult : result) {
String hex = Integer.toHexString(aResult & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
| UnsupportedEncodingException | IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
return null;
}

/**
* AES解密
*
* @param hexStr 待解密内容
* @return decrypt
*/
public static String decryptWithAes(String hexStr, String password) {
if (StringUtils.isBlank(hexStr)) {
return hexStr;
}

byte[] content = 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);
content[i] = (byte) (high * 16 + low);
}trySecureRandom random = SecureRandom.getInstance("SHA1PRNG");random.setSeed(password.getBytes());KeyGenerator kgen = KeyGenerator.getInstance("AES")kgen.init(128, random);

SecretKey secretKey = kgen.generateKey();
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(content);
return new String(result);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
| IllegalBlockSizeException | BadPaddingException e) {
e.printStackTrace();
}
return null;
}

// public static void main(String[] args) {
// String content = "{'projUnique':'5','serialNOS':['D4277efa99b99f','Lcb9cfc5e23f745baf']}";
// JSONObject josn = JSONObject.fromObject(content);
// List<String > ss = (List<String>) josn.get("serialNOS");
// for(int w=0;w<ss.size();w++){
// System.out.println(ss.get(w));
// }
// String password = "自定义";
// System.out.println("加密前:" + content);
// String encryptResultStr = AESUtil.encryptWithAes(content, password);
// System.out.println("加密后:" + encryptResultStr);
// //解密
// String hexStr = "9F79FBABDD5137B78F1F83350A051C069986B40E23E77341759AD505C";
// String decryptFrom = AESUtil.decryptWithAes(hexStr, password);
// System.out.println("解密后:" + decryptFrom);
// }
}

posted on 2020-03-12 17:38  多言  阅读(285)  评论(0编辑  收藏  举报

导航