AES 加解密算法Java实现

AES 加解密算法Java实现

项目地址

算法描述:

AES:高级加密标准 Advanced Encryption Standard。

/**
 * @author youngbear
 * @email youngbear@aliyun.com
 * @date 2021/8/8 9:34
 * @blog https://blog.csdn.net/next_second
 * @github https://github.com/YoungBear
 * AES 加解密
 * 实践中,可以封装加解密方法,封装异常抛出统一异常
 * 密文格式一般为:加密算法id+密钥id+iv值+密文数据
 * 结合密钥管理系统,缓存,实现加解密
 * 本示例使用 AES/GCM/NoPadding
 */
public class AESUtils {

    /**
     * AES 加密算法
     * AES/GCM/NoPadding
     */
    private static final String CIPHER_ALGORITHM = "AES/GCM/NoPadding";

    /**
     * GCM 消息认证码长度(bit) 128bit
     */
    private static final int GCM_AUTH_TAG_LENGTH = 8 * 16;

    /**
     * AES 算法名称
     */
    private static final String AES_ALGORITHM_NAME = "AES";

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    /**
     * 加密
     *
     * @param secretKey 密钥
     * @param iv        iv值
     * @param plainData 待加密的明文数据
     * @return 密文
     * @throws NoSuchPaddingException             异常
     * @throws NoSuchAlgorithmException           异常
     * @throws NoSuchProviderException            异常
     * @throws InvalidAlgorithmParameterException 异常
     * @throws InvalidKeyException                异常
     * @throws IllegalBlockSizeException          异常
     * @throws BadPaddingException                异常
     */
    public static byte[] encrypt(byte[] secretKey, byte[] iv, byte[] plainData)
            throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException,
            InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, BouncyCastleProvider.PROVIDER_NAME);
        AlgorithmParameterSpec parameterSpec = new GCMParameterSpec(GCM_AUTH_TAG_LENGTH, iv);
        Key secretKeySpec = new SecretKeySpec(secretKey, AES_ALGORITHM_NAME);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, parameterSpec);
        return cipher.doFinal(plainData);
    }

    /**
     * 解密
     *
     * @param secretKey  密钥
     * @param iv         iv值
     * @param cipherData 待解密的密文数据
     * @return 明文
     * @throws NoSuchPaddingException             异常
     * @throws NoSuchAlgorithmException           异常
     * @throws NoSuchProviderException            异常
     * @throws InvalidAlgorithmParameterException 异常
     * @throws InvalidKeyException                异常
     * @throws IllegalBlockSizeException          异常
     * @throws BadPaddingException                异常
     */
    public static byte[] decrypt(byte[] secretKey, byte[] iv, byte[] cipherData)
            throws NoSuchPaddingException, NoSuchAlgorithmException, NoSuchProviderException,
            InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM, BouncyCastleProvider.PROVIDER_NAME);
        AlgorithmParameterSpec parameterSpec = new GCMParameterSpec(GCM_AUTH_TAG_LENGTH, iv);
        Key secretKeySpec = new SecretKeySpec(secretKey, AES_ALGORITHM_NAME);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, parameterSpec);
        return cipher.doFinal(cipherData);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AES加解密算法是一种常用的对称加密算法,它使用相同的密钥进行加密和解密操作。在Java中,可以使用javax.crypto包中的Cipher类来实现AES加解密。 首先,需要创建一个Cipher对象,并指定使用AES算法。可以使用以下代码进行初始化: ``` Cipher cipher = Cipher.getInstance("AES"); ``` 接下来,需要创建一个密钥,并将其转换为SecretKeySpec对象。可以使用以下代码生成一个128位的密钥: ``` byte\[\] keyBytes = "0123456789abcdef".getBytes(); // 这里使用一个16字节的密钥 SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); ``` 然后,可以使用Cipher对象进行加密或解密操作。对于加密,可以使用以下代码: ``` cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte\[\] encryptedBytes = cipher.doFinal(content.getBytes()); ``` 对于解密,可以使用以下代码: ``` cipher.init(Cipher.DECRYPT_MODE, keySpec); byte\[\] decryptedBytes = cipher.doFinal(encryptedBytes); ``` 需要注意的是,加密和解密的内容都需要转换为字节数组进行处理。另外,密钥的长度需要与AES算法的要求相匹配。 以上是使用AES算法进行加解密的基本步骤。更详细的实现可以参考引用\[2\]中的链接,其中提供了关于AES加密算法原理和流程的详细解释。 #### 引用[.reference_title] - *1* *3* [Java实现加密(一)AES加解密](https://blog.csdn.net/qq_33204709/article/details/126930720)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Java使用AES加解密](https://blog.csdn.net/weixin_34226706/article/details/89896874)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值