You need to enable JavaScript to run this app. Java实现兼容OpenSSL的AES-CBC文件解密遇Base64解码错误咨询
优惠活动
大模型
产品
解决方案
定价
更多

Java实现兼容OpenSSL的AES-CBC文件解密遇Base64解码错误咨询

OpenSSL格式AES CBC加密文件Java解密Base64报错修正方案

报错核心原因

  • 你当前代码直接将整个加密文件内容作为Base64字符串解码,但加密文件前16字节(8字节Salted__魔法头 + 8字节盐值)是未编码的明文二进制,其中魔法头包含的下划线(ASCII值0x5f,即报错的非法字符5f)不属于标准Base64字符集,因此触发解码异常。
  • 用字符串读取二进制文件的方式会破坏盐值、密文的原始字节结构,导致后续派生的密钥、IV完全错误。

正确实现逻辑

  • 直接读取加密文件的全量原始二进制字节,禁止先转字符串再处理。
  • 拆分文件结构:前8字节校验Salted__魔法头,紧接着8字节提取盐值,仅后续剩余部分执行Base64解码得到真实密文。
  • 密钥派生采用OpenSSL默认的EVP_BytesToKey算法,默认摘要为MD5,循环派生直到获取48字节(32字节AES-256密钥 + 16字节CBC IV)。
  • 解密后直接写出原始明文字节,无需转字符串二次编码。

修正后完整代码

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Base64;

public class AesDecryptUtil {
    // OpenSSL 加密文件固定魔法头
    private static final byte[] SALTED_MAGIC = "Salted__".getBytes(StandardCharsets.US_ASCII);

    static void decryptNew(String path, String password, String outPath) {
        try (
                FileInputStream fis = new FileInputStream(path);
                FileOutputStream fos = new FileOutputStream(outPath)
        ) {
            // 读取文件全量原始二进制
            byte[] fileBytes = fis.readAllBytes();

            // 校验文件长度合法性
            if (fileBytes.length < SALTED_MAGIC.length + 8) {
                throw new IllegalArgumentException("加密文件格式错误,长度不足");
            }

            // 校验魔法头
            byte[] magic = Arrays.copyOfRange(fileBytes, 0, SALTED_MAGIC.length);
            if (!Arrays.equals(magic, SALTED_MAGIC)) {
                throw new IllegalArgumentException("非OpenSSL格式加密文件,魔法头校验失败");
            }

            // 提取盐值
            byte[] salt = Arrays.copyOfRange(fileBytes, SALTED_MAGIC.length, SALTED_MAGIC.length + 8);

            // 提取Base64编码的密文并解码
            byte[] base64Cipher = Arrays.copyOfRange(fileBytes, SALTED_MAGIC.length + 8, fileBytes.length);
            byte[] cipherBytes = Base64.getDecoder().decode(base64Cipher);

            // 派生AES密钥和IV(对应OpenSSL默认MD5摘要的EVP_BytesToKey逻辑)
            byte[] passBytes = password.getBytes(StandardCharsets.US_ASCII);
            byte[] passAndSalt = arrayConcat(passBytes, salt);
            byte[] hash = new byte[0];
            byte[] keyAndIv = new byte[0];
            while (keyAndIv.length < 48) {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(hash);
                md.update(passAndSalt);
                hash = md.digest();
                keyAndIv = arrayConcat(keyAndIv, hash);
            }

            byte[] keyValue = Arrays.copyOfRange(keyAndIv, 0, 32);
            byte[] iv = Arrays.copyOfRange(keyAndIv, 32, 48);

            // 执行AES-256-CBC解密
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(keyValue, "AES"), new IvParameterSpec(iv));
            byte[] plainBytes = cipher.doFinal(cipherBytes);

            // 写出原始明文
            fos.write(plainBytes);
            System.out.println("解密完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 辅助方法:拼接字节数组
    private static byte[] arrayConcat(byte[] a, byte[] b) {
        byte[] result = new byte[a.length + b.length];
        System.arraycopy(a, 0, result, 0, a.length);
        System.arraycopy(b, 0, result, a.length, b.length);
        return result;
    }

    public static void main(String[] args) {
        // 测试调用
        decryptNew("你的加密文件路径", "你的加密密码", "解密输出文件路径");
    }
}

注意事项

  • 如果加密时指定了自定义摘要算法(例如OpenSSL命令加了-md sha256参数),需要将派生密钥用的MD5改成对应的SHA-256。
  • 全程不要使用字符串中转二进制内容,避免编码差异破坏原始字节。

内容的提问来源于stack exchange,提问作者Anees U

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.21 17:15:00