AES /ECB /PKCS7Padding 加密算法
支持jdk8 ,
AES ECB PKCS7Padding /base64代码
package org.thingsboard.gateway.util.AES;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class AESUtil{
/**
* 密钥算法
*/
private static final String KEY_ALGORITHM = "AES";
/**
* 加密/解密算法 / 工作模式 / 填充方式
*/
private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";
public static byte[] keyFormat(String key){
byte[] keyBytes = key.getBytes();
//java不支持pkcs7 补齐方式,因此需要人为补齐
int base = 16;
if (keyBytes.length % base != 0) {
int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
keyBytes = temp;
}
// 初始化
Security.addProvider(new BouncyCastleProvider());
return keyBytes;
}
/**
* AES 加密操作
*
* @param source 待加密内容
* @param key 加密密钥
* @return 返回Base64转码后的加密数据
*/
public static String encrypt(String source,String key) {
try{
byte[] sourceBytes = source.getBytes("UTF-8");
byte[] keyBytes = keyFormat(key);
SecretKeySpec sKey = new SecretKeySpec(keyBytes,CIPHER_ALGORITHM);
Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(Cipher.ENCRYPT_MODE,sKey);
byte[] decrypted = cipher.doFinal(sourceBytes);
return Base64.encodeBase64String(decrypted);
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e){
e.printStackTrace();
}catch (NoSuchProviderException e){
e.printStackTrace();
}catch (NoSuchPaddingException e){
e.printStackTrace();
}catch (InvalidKeyException e){
e.printStackTrace();
}catch (IllegalBlockSizeException e){
e.printStackTrace();
}catch (BadPaddingException e){
e.printStackTrace();
}
return "err";
}
/**
* AES 解密操作
*
* @param content
* @param key
* @return
*/
public static String decrypt(String content, String key) {
try{
byte[] sourceBytes = Base64.decodeBase64(content);
byte[] keyBytes = keyFormat(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, KEY_ALGORITHM));
byte[] decoded = cipher.doFinal(sourceBytes);
return new String(decoded, "UTF-8");
}catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e){
e.printStackTrace();
}catch (NoSuchProviderException e){
e.printStackTrace();
}catch (NoSuchPaddingException e){
e.printStackTrace();
}catch (InvalidKeyException e){
e.printStackTrace();
}catch (IllegalBlockSizeException e){
e.printStackTrace();
}catch (BadPaddingException e){
e.printStackTrace();
}
return "err";
}
}
AES ECB PKCS7Padding /hex代码
package com.thinkgem.jeesite.mobile.utils;
import java.security.Security;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.apache.commons.codec.binary.Hex;
/**
*
* @author Ajit
*
* AES 128 bit ECB PKCS7 padding example
*
*/
public class AesUtil {
/**
* 密钥算法
*/
private static final String KEY_ALGORITHM = "AES";
/**
* 加密/解密算法 / 工作模式 / 填充方式
*/
private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";
public static byte[] keyFormat(String key){
byte[] keyBytes = key.getBytes();
//java不支持pkcs7 补齐方式,因此需要人为补齐
int base = 16;
if (keyBytes.length % base != 0) {
int groups = keyBytes.length / base + (keyBytes.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyBytes, 0, temp, 0, keyBytes.length);
keyBytes = temp;
}
// 初始化
Security.addProvider(new BouncyCastleProvider());
return keyBytes;
}
/**
* AES 加密操作
*
* @param source 待加密内容
* @param key 加密密钥
* @return 返回Base64转码后的加密数据
*/
public static String encrypt(String source,String key) {
try{
byte[] sourceBytes = source.getBytes("UTF-8");
byte[] keyBytes = keyFormat(key);
SecretKeySpec sKey = new SecretKeySpec(keyBytes,CIPHER_ALGORITHM);
Cipher cipher=Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(Cipher.ENCRYPT_MODE,sKey);
byte[] decrypted = cipher.doFinal(sourceBytes);
String hexString = Hex.encodeHexString(decrypted);
return hexString;
}catch (Exception e) {
e.printStackTrace();
}
return "err";
}
/**
* AES 解密操作
*
* @param content
* @param key
* @return
*/
public static String decrypt(String content, String key) {
try{
byte[] sourceBytes = Hex.decodeHex(content.toCharArray());
byte[] keyBytes = keyFormat(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, "BC");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyBytes, KEY_ALGORITHM));
byte[] decoded = cipher.doFinal(sourceBytes);
return new String(decoded, "UTF-8");
}catch (Exception e) {
e.printStackTrace();
}
return "err";
}
public static void main(String[] args) {
String result = encrypt("0000005958^2020-07-29^16:56:47","%lTP1$p~9");
System.out.println("加密过后"+result);
String parm = decrypt(result,"%lTP1$p~9");
System.out.println("解密过后"+parm);
}
}