我是靠谱客的博主 孤独香水,最近开发中收集的这篇文章主要介绍JAVA - AES加密工具类,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

JAVA - RSA加解密工具类 By JINJame

    • JAVA - AES加密工具类
      • 涉及的依赖
      • JAVA版本
      • 方法(函数)说明
      • AESUtils

JAVA - AES加密工具类

以博客代工具集,为以后开发提速提效。
本工具类注重可拓展性,使用时可以再封装一层,以达业务所需

涉及的依赖

org.apache.commons.codec.binary.Base64

JAVA版本

建议 JDK8 以上

方法(函数)说明

方法名使用
createKeyString生成AES可视化密钥(keyString)方法,密钥长度可选 128、192、256
encryptByAES加密方法,使用keyString加密source
decryptByAES解密方法,使用keyString解密encrypted

AESUtils

import org.apache.commons.codec.binary.Base64;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
/**
* AES加解密工具类
* <br>JINJames
* <br>2022/3/8
**/
public class AESUtils {
/**AES密钥标识*/
public static final String SIGN_AES = "AES";
/**字符串编码*/
public static final String UTF_8 = "UTF-8";
/**密码器AES模式*/
public static final String CIPHER_AES= "AES/ECB/PKCS5Padding";
/**密钥长度128*/
public static final int KEY_SIZE_128_LENGTH = 128;
/**密钥长度192*/
public static final int KEY_SIZE_192_LENGTH = 192;
/**密钥长度256*/
public static final int KEY_SIZE_256_LENGTH = 256;
/**
* 生成密钥,请使用合适的长度128 192 256
* @param keySize
* @return
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
*/
public static String createKeyString(int keySize) throws NoSuchAlgorithmException, UnsupportedEncodingException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(keySize);
SecretKey secretKey = keyGenerator.generateKey();
byte[] keyBytes = secretKey.getEncoded();
return new String(Base64.encodeBase64(keyBytes), UTF_8);
}
/**
* 生成Key对象
* @param keyString
* @return
* @throws UnsupportedEncodingException
*/
public static Key generateKey(String keyString) throws UnsupportedEncodingException {
byte[] decodedKey = Base64.decodeBase64(keyString.getBytes(UTF_8));
Key key = new SecretKeySpec(decodedKey, SIGN_AES);
return key;
}
/**
* AES加密
* @param source
* @param keyString
* @return
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws UnsupportedEncodingException
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
public static String encryptByAES(String source, String keyString) throws NoSuchPaddingException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
Cipher cipher = Cipher.getInstance(CIPHER_AES);
cipher.init(Cipher.ENCRYPT_MODE, generateKey(keyString));
byte[] encrypted = cipher.doFinal(source.getBytes(UTF_8));
return Base64.encodeBase64String(encrypted);
}
/**
* AES解密
* @param encrypted
* @param keyString
* @return
* @throws UnsupportedEncodingException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws InvalidKeyException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
*/
public static String decryptByAES(String encrypted, String keyString) throws UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException, InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException {
Cipher cipher = Cipher.getInstance(CIPHER_AES);
cipher.init(Cipher.DECRYPT_MODE, generateKey(keyString));
byte[] decrypted = cipher.doFinal(Base64.decodeBase64(encrypted));
return new String(decrypted, UTF_8);
}
}

最后

以上就是孤独香水为你收集整理的JAVA - AES加密工具类的全部内容,希望文章能够帮你解决JAVA - AES加密工具类所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部