package com.net.ninemm.util;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
public class AesUtil {
public static void main(String args[]) throws Exception {
String content = "明文 123 abc";
//aes加密解密示例
//加密
String encrypted = encrypt(content, KEY, IV);
System.out.println("加密前:" + content);
System.out.println("加密后:" + encrypted);
//解密
String decrypted2 = decrypt(encrypted, KEY, IV);
System.out.println("解密后:" + decrypted2);
//签名加密appkey+ timestamp + secret
// String content =appkey+ timestamp + secret;
// System.out.println("加密前"+content);
//加密
// String encrypt = AesUtil.encrypt(content, KEY, IV);
//解密
// String encry = AesUtil.encry(encrypt);
// System.out.println(encry);
}
/**
* 长度必须是 16
*/
private static String KEY = "abcdef0123456789";
/**
* 长度必须是 16
*/
private static String IV = "abcdef0123456789";
private static int token_length = 30;
/**
* appkey:nm+uuid前十五位生成,共十七位
*/
private static String appkey = "nm819fce29518a820";
/**
* 当前时间戳,有效时间15分钟,十三位单位毫秒
*/
private static Long timestamp = System.currentTimeMillis();
// private static Long timestamp = 1639020216513L;
/**
* 随机串uuid
*/
private static String secret = "c4025f40fd9fb456cc3b1c7851884749";
/**
* token验证
*/
private static String tokenyan = "04bc0f1";
/**
* 加密返回的数据转换成 String 类型
* @param content 明文
* @param key 秘钥
* @param iv 初始化向量是16位长度的字符串
* @return
* @throws Exception
*/
public static String encrypt(String content, String key, String iv) throws Exception {
// 将返回的加密过的 byte[] 转换成Base64编码字符串 !!!!很关键
return base64ToString(AES_CBC_Encrypt(content.getBytes(), key.getBytes(), iv.getBytes()));
}
public static String encrypt(String content) throws Exception {
// 将返回的加密过的 byte[] 转换成Base64编码字符串 !!!!很关键
return base64ToString(AES_CBC_Encrypt(content.getBytes(), KEY.getBytes(), IV.getBytes()));
}
/**
* 将解密返回的数据转换成 String 类型
* @param content Base64编码的密文
* @param key 秘钥
* @param iv 初始化向量是16位长度的字符串
* @return
* @throws Exception
*/
public static String decrypt(String content, String key, String iv) throws Exception {
// stringToBase64() 将 Base64编码的字符串转换成 byte[] !!!与base64ToString()配套使用
return new String(AES_CBC_Decrypt(stringToBase64(content), key.getBytes(), iv.getBytes()));
}
private static byte[] AES_CBC_Encrypt(byte[] content, byte[] keyBytes, byte[] iv){
try {
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,key, new IvParameterSpec(iv));
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
System.out.println("exception:"+e.toString());
}
return null;
}
private static byte[] AES_CBC_Decrypt(byte[] content, byte[] keyBytes, byte[] iv){
try {
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE,key, new IvParameterSpec(iv));
byte[] result = cipher.doFinal(content);
return result;
} catch (Exception e) {
System.out.println("exception:"+e.toString());
}
return null;
}
/**
* 字符串装换成 Base64
*/
public static byte[] stringToBase64(String key) throws Exception {
return Base64.decodeBase64(key.getBytes());
}
/**
* Base64装换成字符串
*/
public static String base64ToString(byte[] key) throws Exception {
return new Base64().encodeToString(key);
}
public static String encry(String encrypt) {
if(null == encrypt || encrypt.equals("")){
return "签名错误";
}
if(encrypt.equals("notAes")){
return "success";
}
try {
//解密
String decrypt = AesUtil.decrypt(encrypt, KEY, IV);
if(decrypt.length()<token_length){
return "签名错误1";
}
String resappkey = decrypt.substring(0, 17);
String ressecret = decrypt.substring(30);
//token验证
String token = shaEncode(resappkey + ressecret);
// System.out.println("token=="+token);
String substring = token.substring(token.length() - 7);
// System.out.println("tokenyan=="+substring);
//生成签名
if(!substring.equals(tokenyan)){
return "签名错误2";
}
String restimestamp = decrypt.substring(17, 30);
long timeSolt = (System.currentTimeMillis() - 15 * 60 * 1000) - Long.valueOf(restimestamp);
if(timeSolt>0){
return "时间戳无效";
}
// System.out.println("appkey:"+resappkey);
// System.out.println("timestamp:"+restimestamp);
// System.out.println("secret:"+ressecret);
} catch (Exception e){
e.printStackTrace();
return "签名错误3";
}
return "success";
}
/**
* @Comment SHA1实现
*/
public static String shaEncode(String inStr) throws Exception {
MessageDigest sha = null;
try {
sha = MessageDigest.getInstance("SHA");
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
byte[] byteArray = inStr.getBytes("UTF-8");
byte[] md5Bytes = sha.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
}
aes加密工具类java
最新推荐文章于 2024-07-11 16:07:10 发布