AES/CBC加密

本文档通过AESUtilDemo类展示了AES加密算法在CBC模式下,特别是NoPadding和PKCS5Padding的应用,重点讲解了密钥和偏移矢量的长度及加密后数据长度的处理。实例中,作者提供了如何检查密钥和偏移矢量,以及加密和解密过程的详细步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class AESUtilDemo {


    /**
     * 算法/模式/填充                    16字节加密后数据长度         不满16字节加密后长度
     *
     * AES/CBC/NoPadding                  16                         不支持
     * AES/CBC/PKCS5Padding               32                          16
     * AES/CBC/ISO10126Padding            32                          16
     * AES/CFB/NoPadding                  16                          原始数据长度
     * AES/CFB/PKCS5Padding               32                          16
     * AES/CFB/ISO10126Padding            32                          16
     * AES/ECB/NoPadding                  16                          不支持
     * AES/ECB/PKCS5Padding               32                          16
     * AES/ECB/ISO10126Padding            32                          16
     * AES/OFB/NoPadding                  16                          原始数据长度
     * AES/OFB/PKCS5Padding               32                          16
     * AES/OFB/ISO10126Padding            32                          16
     * AES/PCBC/NoPadding                 16                          不支持
     * AES/PCBC/PKCS5Padding              32                          16
     * AES/PCBC/ISO10126Padding           32                          16
     */

    public static final String AES = "AES";
    public static final String AES_CBC_NO_PADDING = "AES/CBC/NoPadding";
    public static final int KEY_LENGTH = 16;
    public static final int IV_LENGTH = 16;

    public static void main(String[] args) throws Exception {
        String key = "qqqqwwwweeeerrrr";
        String iv = "aaaassssddddffff";
        AESUtilDemo aesUtilDemo = new AESUtilDemo();
        String hello = aesUtilDemo.Encryption(key, iv, "hello");
        System.out.println(hello);
        String decrypt = aesUtilDemo.Decrypt(key, iv, hello);
        System.out.println(decrypt);
    }

    /**
     * AES/CBC加密
     * @param key  密钥
     * @param iv   偏移矢量
     * @param content 加密的内容
     * @return
     */
    public String Encryption(String key, String iv, String content) throws Exception {
        //检查是否为16位
        checkKey(key);
        checkIV(iv);

        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), AES);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        Cipher cipher = Cipher.getInstance(AES_CBC_NO_PADDING);
        // AES/CBC/NoPadding  加密后的字节长度位16   需要自己去补成16位
        int blockSize = cipher.getBlockSize();
        int length = content.getBytes().length;
        if (length % blockSize != 0){
            length = length + blockSize - (length % blockSize);
        }

        byte[] bytes1 = new byte[length];
        System.arraycopy(content.getBytes(), 0, bytes1, 0, content.getBytes().length);


        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
        byte[] bytes = cipher.doFinal(bytes1);

        //base64加密
        return new BASE64Encoder().encode(bytes);

    }

    /**
     * 判断key
     * @param key
     * @throws Exception
     */
    private void checkKey(String key) throws Exception {
        if (key == null || key.length() != KEY_LENGTH){
            throw new Exception("密钥不正确");
        }
    }

    /**
     * 判断iv
     * @param iv
     * @throws Exception
     */
    private void checkIV(String iv) throws Exception {
        if (iv == null || iv.length() != IV_LENGTH){
            throw new Exception("偏移矢量不正确");
        }
    }

    /**
     * 解密
     * @param key  密钥
     * @param iv   偏移矢量
     * @param content 解密内容
     * @return
     * @throws Exception
     */
    public String Decrypt(String key, String iv, String content) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), AES);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
        Cipher cipher = Cipher.getInstance(AES_CBC_NO_PADDING);

        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

        //先解开base64加密
        byte[] bytes1 = new BASE64Decoder().decodeBuffer(content);
        byte[] bytes = cipher.doFinal(bytes1);

        return new String(bytes);

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值