AES加密解密代码

package com.huizhuyun.jeemis.api.openAPI.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import java.util.Base64;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static java.util.Base64.*;

/**
* Aes加密解密
*/
public class AesMethodOpenService {
private static final Logger LOGGER = LoggerFactory.getLogger(AesMethodOpenService.class);
private static final String ENCODING = "GBK";
private static final String KEY_ALGORITHM = "AES";
private static final Base64.Decoder decoder = Base64.getDecoder();
private static final Base64.Encoder encoder = Base64.getEncoder();
/**
* 加解密算法/工作模式/填充方式
*/
private static final String DEFAUIT_CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
/**
* 填充向量
*/
private static final String FILL_VECTOR = "1234560405060708";


/**
* 加密
*
* @param sSrc 加密前的字符串
* @param sKey 加密KEY
* @return
* @throws Exception
* @author yinning
*/
public static String encrypt (String sSrc, String sKey) throws Exception{
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance(DEFAUIT_CIPHER_ALGORITHM);//"算法/模式/补码方式"
IvParameterSpec iv = new IvParameterSpec(sKey.getBytes());//使用CBC模式,需要一个向量iv,可增加加密算法的强度
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes());
String encodedText = encoder.encodeToString(encrypted);
return encodedText;//此处使用BASE64做转码功能,同时能起到2次加密的作用。

}

/**
* 解密
*
* @param sSrc 解密前的字符串
* @param sKey 解密KEY
* @return
* @throws Exception
* @author yinning
*/
public static String decrypt(String sSrc, String sKey) {
try {
// 判断Key是否正确
if (sKey == null) {
System.out.print("Key为空null");
return null;
}
// 判断Key是否为16位
if (sKey.length() != 16) {
System.out.print("Key长度不是16位");
return null;
}
byte[] raw = sKey.getBytes("utf-8");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance(DEFAUIT_CIPHER_ALGORITHM);
IvParameterSpec iv = new IvParameterSpec(sKey.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = decoder.decode(sSrc);//先用base64解密
try {
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
} catch (Exception ex) {
System.out.println(ex.toString());
return null;
}
}

public static void main(String[] args) throws Exception {

String cKey = "9230967890982316";
// 需要加密的字串
String cSrc = "211421188811111212";
System.out.println(cSrc);
// 加密
long lStart = System.currentTimeMillis();
String enString = encrypt(cSrc, cKey);
System.out.println("加密后的字串是:" + enString);

long lUseTime = System.currentTimeMillis() - lStart;
System.out.println("加密耗时:" + lUseTime + "毫秒");
lStart = System.currentTimeMillis();
String DeString = decrypt(enString, cKey);
System.out.println("解密后的字串是:" + DeString);
lUseTime = System.currentTimeMillis() - lStart;
System.out.println("解密耗时:" + lUseTime + "毫秒");

}
}

猜你喜欢

转载自www.cnblogs.com/fengfeng21/p/12154665.html