当前位置: 首页 > article >正文

【JAVA使用Aes加密报错:Illegal key size or default parameters,如何解决?】

JAVA使用Aes加密报错:Illegal key size or default parameters,如何解决?

遇到“Illegal key size or default parameters”这个错误通常与Java环境中使用的加密算法有关,特别是当你尝试使用较高级别的加密密钥长度(例如AES-256)时。这个问题的根本原因通常是由于Java运行时环境(JRE)默认的安全策略文件限制了加密强度,这主要是出于出口控制法规的历史原因。
重点:key和位数的限制必须要匹配,均为128位。
jdk1.8版本不能超过128位数。

这个报错是由于JDK1.8的版本导致的。网上提供的方法都是通过升级版本,或者修改对应的包来实现加密,如果不想升级JDK1.8版本,那么怎么解决这个问题呢?
只需要将默认key的位数改为128位的字符串即可。
报错信息记录:
jdk1.8版本使用cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(Base64.getDecoder().decode(key), ALGORITHM));,报错Illegal key size or default parameters,如何解决?
Invalid AES key length: 12 bytes
Given final block not properly padded:不匹配

上代码

package com.mxpt.common.security.utils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AesUtils{

    public static String aesKey = "9D7E445B58C6A275777DRR";

    private static final String ALGORITHM = "AES";

    private static final int KEY_SIZE = 128;

    public static void main(String[] args) throws Exception {
        String key = "9D7E445B58C6A275A78164";
        byte[] keyBytes = Base64.getDecoder().decode(key);
        // 检查输出是否为期望位数128位
        System.out.println("Key length: " + keyBytes.length * 8 + " bits");
    }

    /**
     * 根据给定的密钥解密文本
     */
    public static String decrypt(String encryptedData, String key) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);

        byte[] keyBytes = Base64.getDecoder().decode(key);
        System.out.println("Key length: " + keyBytes.length * 8 + " bits"); // 检查输出是否为期望位数


        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(Base64.getDecoder().decode(key), ALGORITHM));
        return new String(cipher.doFinal(Base64.getDecoder().decode(encryptedData)));
    }

    /**
     * 使用给定的密钥加密文本
     */
    public static String encrypt(String data, String key) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(Base64.getDecoder().decode(key), ALGORITHM));
        return Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes()));
    }

    /**
     * 生成一个新的密钥
     */
    public static String generateKey() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
        keyGen.init(KEY_SIZE);
        SecretKey secretKey = keyGen.generateKey();
        return Base64.getEncoder().encodeToString(secretKey.getEncoded());
    }
}

重点:key和位数的限制必须要匹配,均为128位。jdk1.8版本不能超过128位数。
在这里插入图片描述


http://www.kler.cn/a/540310.html

相关文章:

  • BS架构(笔记整理)
  • postgresql 游标(cursor)的使用
  • Photoshop自定义键盘快捷键
  • Redis 集群原理、主从复制和哨兵模式的详细讲解
  • 练习题(2025.2.9)
  • CANoe工具使用技巧 --- 如何使用 “on ethernetPacket “事件处理程序
  • FFmpeg 学习路径
  • VeryReport和FineReport两款报表软件深度分析对比
  • 只需三步!5分钟本地部署deep seek——MAC环境
  • MongoDB 的使用场景
  • Transformers as SVM(2023 NIPS)
  • react概览webpack基础
  • zynq tcp万兆网和ftp协议分析
  • 如何查看用户的详细身份信息
  • 向量数据库简单对比
  • fps动作系统9:动画音频
  • flutter 默认跳转封装
  • Powershell语言的数据库编程
  • Windows 本地部署大模型 OpenWebUI+Ollama
  • 游戏引擎学习第95天
  • 【GIS】本地部署nominatim地理编码服务
  • 【人工智能】python之set集合练习
  • 等级保护2.0|网络安全服务
  • pytorch torch.linalg模块介绍
  • pip3命令全解析:Python3包管理工具的详细使用指南
  • 【LeetCode 热题100】74:搜索二维矩阵(二分、线性两种方式 详细解析)(Go 语言实现)