import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Random;
/**
* AES加密工具类
*/
public class AESUtil {
/**
* 加密Key 需要16位 可用数字与字母组成
*/
private static String key = "Ijasw2o93jd81kdD";
/**
* 偏移量 需要16位
*/
private static String iv = "4w2Df1xSj5ff662d";
private static Logger log = LoggerFactory.getLogger(AESUtil.class);
private static Base64.Decoder decoder;
private static Base64.Encoder encoder;
static {
decoder = Base64.getDecoder();
encoder = Base64.getEncoder();
}
public static String getSixteenBitString(){
StringBuffer sb = new StringBuffer();
String[] chars = new String[]{
"1","2","3","4","5","6","7","8","9","a","b",
"c","d","e","f","g","h","i","j","k","l","m",
"n","o","p","q","r","s","t","u","v","w","x",
"y","z","A","B","C","D","E","F","G","H","I",
"J","K","L","M","N","O","P","Q","R","S","T",
"U","V","W","X","Y","Z",
};
int len = chars.length;
Random random = new Random();
for (int i = 0; i < 16; i++) {
sb.append(chars[random.nextInt(len-1)]);
}
return sb.toString();
}
/**
* AES加密
* @param data
* @param key
* @param iv
* @return
* @throws Exception
*/
public static String encryptAES_CBC(String data,String key,String iv) {
Cipher cipher = null;
try {
cipher = Cipher.getInstance("AES/CBC/NoPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int dataLength = dataBytes.length;
if (dataLength % blockSize != 0) {
dataLength = dataLength + (blockSize - (dataLength % blockSize));
}
byte[] plaintext = new byte[dataLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
byte[] bytes = cipher.doFinal(plaintext);
return encoder.encodeToString(bytes);
}catch (Exception e) {
log.error("AES加密失败");
log.error(e.getMessage());
}
return null;
}
/**
* AES解密
* @param data
* @param key
* @param iv
* @return
* @throws Exception
*/
public static String decrptyAES_CBC(String data,String key,String iv){
try {
byte[] bytes = decoder.decode(data);
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
byte[] plainByte = cipher.doFinal(bytes);
return new String(plainByte).trim();
}catch (Exception e){
log.error("AES解密失败");
log.error(e.getMessage());
}
return null;
}
/**
* AES加密
* @param data
* @return
* @throws Exception
*/
public static String encryptAES_ECB(String data,String key) throws Exception{
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes();
int dataLength = dataBytes.length;
if(dataLength % blockSize != 0){
dataLength = dataLength + (blockSize - (dataLength % blockSize));
}
byte [] plaintext = new byte[dataLength];
System.arraycopy(dataBytes,0,plaintext,0,dataBytes.length);
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),"AES");
cipher.init(Cipher.ENCRYPT_MODE,secretKey);
byte[] bytes = cipher.doFinal(plaintext);
return encoder.encodeToString(bytes);
}
/**
* AES解密
* @param data
* @return
* @throws Exception
*/
public static String decrptyAES_ECB(String data,String key) throws Exception{
byte[] bytes = decoder.decode(data);
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(),"AES");
cipher.init(Cipher.DECRYPT_MODE,secretKey);
byte[] plainByte = cipher.doFinal(bytes);
return new String(plainByte).trim();
}
public static void main(String[] args) throws Exception{
String s = "123231312";
String encrypt = encryptAES_ECB(s,key);
System.out.println(encrypt);
System.out.println(decrptyAES_ECB(encrypt,key));
String s1 = encryptAES_CBC(s, key, iv);
System.out.println(s1);
System.out.println(decrptyAES_CBC(s1,key,iv));
getSixteenBitString();
}
}
Java AES CBC/ECB NoPadding 加密128位
最新推荐文章于 2024-05-13 17:04:37 发布
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Llama Factory
模型微调
LLama-Factory
LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调
4250

被折叠的 条评论
为什么被折叠?



