JAVA之AES_CBC模式解密

AES_CBC模式解密 填充 PKCS7Padding


import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.stereotype.Component;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.security.Security;
import java.util.Arrays;

 
@Slf4j
@Component
public class AesCbcUtils {



    // 算法名称
    final String KEY_ALGORITHM = "AES";
    // 加解密算法/模式/填充方式
    final String algorithmStr = "AES/CBC/PKCS7Padding";
    
     private Key key;

    private Cipher cipher;

    

    /**
     * 解密方法
     *
     * @param encryptedData
     *            要解密的字符串
     * @param keyBytes
     *            解密密钥
     *  @param iv
     * 		     偏移量
     * @return
     */
    public byte[] decrypt(byte[] encryptedData, byte[] keyBytes,byte[] iv) {
        byte[] encryptedText = null;
        init(keyBytes);
        System.out.println("IV:" + new String(iv));
        try {
            cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
            encryptedText = cipher.doFinal(encryptedData);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return encryptedText;
    }

	public void init(byte[] keyBytes) {
        // 如果密钥不足16位,那么就补足.  这个if 中的内容很重要
        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());
        // 转化成JAVA的密钥格式
        key = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
        try {
            // 初始化cipher
            cipher = Cipher.getInstance(algorithmStr, "BC");
        } catch ( Exception e) {
        	e.printStackTrace();
        }
    }
     
}
您好,针对您的问题,我可以简单地给出以下代码示例,展示如何使用 JavaAES解密算法。 首先,我们需要引入以下包: ```java import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; ``` 然后,我们需要定义一个加解密的通用函数: ```java public static String encrypt(String data, String key, String iv) throws Exception { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); int blockSize = cipher.getBlockSize(); byte[] dataBytes = data.getBytes(); int plaintextLength = dataBytes.length; if (plaintextLength % blockSize != 0) { plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); } byte[] plaintext = new byte[plaintextLength]; System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); return new String(Base64.getEncoder().encode(encrypted)); } public static String decrypt(String data, String key, String iv) throws Exception { byte[] encrypted = Base64.getDecoder().decode(data.getBytes()); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); byte[] decrypted = cipher.doFinal(encrypted); return new String(decrypted).trim(); } ``` 在上述函数中,我们使用了 AES解密算法,采用了 CBC 模式和 NoPadding 填充方式。同时,我们需要传入三个参数: - data:待加解密的数据 - key:AES 密钥 - iv:AES 偏移量 最后,我们可以调用上述函数进行加解密操作: ```java String key = "0123456789abcdef"; String iv = "fedcba9876543210"; String data = "Hello, world!"; String encrypted = encrypt(data, key, iv); String decrypted = decrypt(encrypted, key, iv); System.out.println("Encrypted: " + encrypted); System.out.println("Decrypted: " + decrypted); ``` 以上代码展示了如何使用 JavaAES解密算法进行加解密操作。需要注意的是,为了确保加解密的正确性,我们需要在加密和解密时使用相同的密钥和偏移量。同时,由于使用了 NoPadding 填充方式,因此我们需要手动对待加密的数据进行填充。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值