AES 解密 --java

直接先上代码把

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;

Security.addProvider(new BouncyCastleProvider());

String encryptedText = "2ZF0yNIfa9mGD8Tumk2OlEqe8w53aivwL56WuDt17aHsaG4Gy9Ev95pWU4/TTp8B8uAab8rm/n+7LmWpE1t37k3+NmQBo3c2tNF5vtS+plBD5Yslpj2gCH1AwW9QBBGWGAUDimpfut7WPpdx5fLSqQRW9tAwN7zmy3w2it7ALKKwN9LeFAHNws6SkvanxCua9xkOFcuvE28BE54Xc6Lglg=="; // 待解密的密文,请替换为你的密文
String encryptionKey = "QT1B60IRNMFwrAgY"; // 密钥,请替换为你的密钥

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 = "EThHESD0kTEsKeAF".getBytes("utf-8"); // 这里使用全零的初始向量 EThHESD0kTEsKeAF
    IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);

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

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

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

你可能感兴趣的:(java,开发语言)