ECB模式256位加解密案例
import java.nio.charset.StandardCharsets;
import java.security.Security;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
* @author qhong
* @date 2021/12/30 16:47
*
* http://tool.chacuo.net/cryptaes
**/
@Slf4j
@SuppressWarnings("all")
public class AESUtils {
public final static String KEY_ALGORITHMS = "AES";
public final static int KEY_SIZE_128 = 128;
public final static int KEY_SIZE_256 = 256;
public final static String UTF_8 = "UTF-8";
public final static String AES256_KEY = "3C4azDdu5itcWLW/LXKl+jJxp4SERkXb";
/**
* 加密/解密算法/工作模式/填充方式
*
* JAVA 支持PKCS5PADDING填充方式 Bouncy castle支持PKCS7Padding填充方式
*/
public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS7Padding";
static {
Security.addProvider(new BouncyCastleProvider());
}
/**
* AES 加密字符串,base64Key对象
*/
public static String encrypt(String encryptData) {
return encrypt(AES256_KEY, encryptData, UTF_8);
}
/**
* AES 解密字符串,base64Key对象
*/
public static String decrypt(String decryptData) {
return decrypt(AES256_KEY, decryptData, UTF_8);
}
/**
* AES 加密字符串,base64Key对象
*/
public static String encrypt(String base64Key, String encryptData) {
String result = encrypt(base64Key, encryptData, UTF_8);
if (result == null) {
return encryptData;
}
return result;
}
/**
* AES 解密字符串,base64Key对象
*/
public static String decrypt(String base64Key, String decryptData) {
String result = decrypt(base64Key, decryptData, UTF_8);
if (result == null) {
return decryptData;
}
return result;
}
/**
* AES 加密字符串,base64Key对象
*/
public static String encrypt(String base64Key, String encryptData, String encode) {
SecretKey key = loadKeyAES(base64Key);
try {
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptBytes = encryptData.getBytes(encode);
byte[] result = cipher.doFinal(encryptBytes);
return Base64.encodeBase64String(result);
} catch (Exception e) {
log.info("encrypt error:{}", e);
return null;
}
}
/**
* AES 解密字符串,base64Key对象
*/
public static String decrypt(String base64Key, String decryptData, String encode) {
SecretKey key = loadKeyAES(base64Key);
try {
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptBytes = Base64.decodeBase64(decryptData);
byte[] result = cipher.doFinal(decryptBytes);
return new String(result, encode);
} catch (Exception e) {
log.info("decrypt error:{}", e);
return null;
}
}
/**
* 生成AES密钥,base64编码格式 (256)
*/
private static String getKeyAES_256() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance(KEY_ALGORITHMS);
keyGen.init(KEY_SIZE_256);
SecretKey key = keyGen.generateKey();
String base64str = Base64.encodeBase64String(key.getEncoded());
return base64str;
}
/**
* 根据base64Key获取SecretKey对象
*/
private static SecretKey loadKeyAES(String base64Key) {
//byte[] bytes = Base64.decodeBase64(base64Key);
byte[] bytes = base64Key.getBytes(StandardCharsets.UTF_8);
SecretKeySpec secretKeySpec = new SecretKeySpec(bytes, KEY_ALGORITHMS);
return secretKeySpec;
}
public static void main(String[] args) {
System.out.println(AES256_KEY.length());
String input = "{\"loanStatus\":0,\"pageIndex\":1,\"pageSize\":30}";
String encryptStr = encrypt(AES256_KEY, input);
System.out.println(encryptStr);
System.out.println(decrypt(AES256_KEY, encryptStr));
System.out.println(encrypt("hongda"));
System.out.println(encrypt("27"));
System.out.println(decrypt("dcNkjsS5xxZAg34HRpb4grgmDNx8hrQHlHYaJ8BERGd1WYMOimMn+Tv9dUpHoStI"));
}
}
GCM模式加解密案例
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
* @author qhong
* @date 2021/12/30 16:47
*
* http://tool.chacuo.net/cryptaes
**/
@Slf4j
@SuppressWarnings("all")
public class AESUtils {
public final static String KEY_ALGORITHMS = "AES";
public static final int GCM_TAG_LENGTH = 16;
public final static int KEY_SIZE_128 = 128;
public final static int KEY_SIZE_256 = 256;
public final static String UTF_8 = "UTF-8";
public final static String AES256_KEY = "3C4azDdu5itcWLW/LXKl+jJxp4SERkXb";
public final static String IV_KEY = "iLNfqLdYeXd326+oHfO8nw==";
/**
* 加密/解密算法/工作模式/填充方式
*
* JAVA 支持PKCS5PADDING填充方式 Bouncy castle支持PKCS7Padding填充方式
*/
public static final String CIPHER_ALGORITHM = "AES/GCM/NoPadding";
static {
Security.addProvider(new BouncyCastleProvider());
}
/**
* AES 加密字符串,base64Key对象
*/
public static String encrypt(String base64Key, String encryptData, String iv) {
try {
byte[] result = encrypt(encryptData.getBytes(UTF_8), base64Key.getBytes(UTF_8), iv.getBytes(UTF_8));
return Base64.encodeBase64String(result);
} catch (Exception e) {
log.info("encrypt error:{}", e);
return encryptData;
}
}
/**
* AES 解密字符串,base64Key对象
*/
public static String decrypt(String base64Key, String decryptData, String iv) {
try {
byte[] result = decrypt(Base64.decodeBase64(decryptData), base64Key.getBytes(UTF_8), iv.getBytes(UTF_8));
return new String(result);
} catch (Exception e) {
log.info("decrypt error:{}", e);
return decryptData;
}
}
private static byte[] encrypt(byte[] plaintext, byte[] enckey, byte[] IV) throws Exception {
// Get Cipher Instance
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
// Create SecretKeySpec
SecretKeySpec keySpec = new SecretKeySpec(enckey, KEY_ALGORITHMS);
// Create GCMParameterSpec
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, IV);
// Initialize Cipher for ENCRYPT_MODE
cipher.init(Cipher.ENCRYPT_MODE, keySpec, gcmParameterSpec);
// Perform Encryption
byte[] cipherText = cipher.doFinal(plaintext);
return cipherText;
}
private static byte[] decrypt(byte[] cipherText, byte[] deckey, byte[] IV) throws Exception {
// Get Cipher Instance
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
// Create SecretKeySpec
SecretKeySpec keySpec = new SecretKeySpec(deckey, KEY_ALGORITHMS);
// Create GCMParameterSpec
GCMParameterSpec gcmParameterSpec = new GCMParameterSpec(GCM_TAG_LENGTH * 8, IV);
// Initialize Cipher for DECRYPT_MODE
cipher.init(Cipher.DECRYPT_MODE, keySpec, gcmParameterSpec);
// Perform Decryption
byte[] decryptedText = cipher.doFinal(cipherText);
return decryptedText;
}
public static void main(String[] args) {
System.out.println(AES256_KEY.length());
String input = "{\"loanStatus\":0,\"pageIndex\":1,\"pageSize\":30}";
String encryptStr = encrypt(AES256_KEY, input, IV_KEY);
System.out.println(encryptStr);
System.out.println(decrypt(AES256_KEY, encryptStr, IV_KEY));
// System.out.println(encrypt("hongda"));
// System.out.println(encrypt("27"));
// System.out.println(decrypt("dcNkjsS5xxZAg34HRpb4grgmDNx8hrQHlHYaJ8BERGd1WYMOimMn+Tv9dUpHoStI"));
}
}
参考:
AES测试网站
AES-128-GCM 加密算法在 ruby 和 java 中的不同
id-aes128-GCM 加解密example