Java实现AES解密(BC模式+KEY+IV)

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
import org.apache.commons.codec.binary.Base64;

public class AesDecryptExample {
    public static void main(String[] args) {
        Security.addProvider(new BouncyCastleProvider());

        String encryptedText = "l2VI2I0GL1qPA084yTKMYI8awDGGf6qE7NcI3UPgMSsVZPa8+QYjhxk0VgTls6VxXRe8M/XSq5u6cE1cOWLrMevO9ZZlOGuSkFv0FEoV5I016aTYlxraHpvv4eoNLVDg1j622SNYkfclAmqG0SUfOQ=="; // 待解密的密文,请替换为你的密文
        String encryptionKey = "xxx";   // 密钥,请替换为你的密钥
        String iv = "xxx";             //偏移量字符串必须是16位 当模式是CBC的时候必须设置偏移量
        try {
            // 将密文进行 Base64 解码
            byte[] encryptedBytes = Base64.decodeBase64(encryptedText);

            // 设置 AES 密码及密钥长度
            byte[] keyBytes = encryptionKey.getBytes("UTF-8");
            SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");

            // 设置加密模式和填充方式
            Cipher cipher = Cipher.getInstance("AES/CBC/ZeroBytePadding", "BC");

            // 设置初始向量
            byte[] ivBytes = iv.getBytes("utf-8");
            IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

            // 进行解密操作
            cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

            // 将解密结果转为字符串
            String decryptedText = new String(decryptedBytes);

            // 输出解密结果
            System.out.println("Decrypted Text: " + decryptedText);
        } catch (Exception ex) {
            System.out.println("AES decryption failed: " + ex);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智能体格

你的鼓将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值