AES加解密,Given final block not properly padded异常

该文章描述了一个在Linux系统中遇到的AES解密问题,错误信息为Givenfinalblocknotproperlypadded。问题可能源于加密和解密过程中使用的密钥生成或随机数生成不一致。代码示例展示了加密和解密的方法,以及使用SHA1PRNG和SecureRandom进行随机数生成的过程。
摘要由CSDN通过智能技术生成

Given final block not properly padded异常

在这里插入图片描述
问题描述:windows下运行正常,linux下就解密异常
//防止linux下 随机生成key
SecureRandom secureRandom = SecureRandom.getInstance(“SHA1PRNG” );
secureRandom.setSeed(keycontent.getBytes());
// 根据密钥初始化密钥生成器
kgen.init(keyNum, secureRandom);
具体详情见代码

- 加密方法

public static String getSecretContent(String appSecret, String content) {
        KeyGenerator kg;
        try {
            kg = KeyGenerator.getInstance("AES");

            // kg.init(128);//要生成多少位,只需要修改这里即可128, 192或256
            //SecureRandom是生成安全随机数序列,password.getBytes()是种子,只要种子相同,序列就一样,所以生成的秘钥就一样。
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(appSecret.getBytes());
            kg.init(128,random);
            SecretKey sk = kg.generateKey();
            byte[] format = sk.getEncoded();
            //转秘钥
            SecretKeySpec sKeySpec = new SecretKeySpec(format, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
            byte[] bjiamihou = cipher.doFinal(content.getBytes("utf-8"));
            String sercetContent = byteToHexString(bjiamihou);
            return sercetContent;
        } catch (Exception e) {
            logger.info("参数加密异常,{}", new Object[]{ExceptionUtils.getFullStackTrace(e)});
            throw new ApiReqException("504", "参数加密异常");
        }
    }

解密方法

public static String resolveContet(String appSecret, String content) {
        KeyGenerator kg = null;
        try {
            byte[] miwen = hexStringToByte(content);
            kg = KeyGenerator.getInstance("AES");
            // kg.init(128);//要生成多少位,只需要修改这里即可128, 192或256
            //SecureRandom是生成安全随机数序列,password.getBytes()是种子,只要种子相同,序列就一样,所以生成的秘钥就一样。
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            random.setSeed(appSecret.getBytes());
            kg.init(128, random);
            SecretKey sk = kg.generateKey();
            byte[] keyb = sk.getEncoded();
            SecretKeySpec sKeySpec = new SecretKeySpec(keyb, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, sKeySpec);//初始化为解密模式的密码器
            byte[] reContent = cipher.doFinal(miwen);
            String s = new String(reContent,"utf-8");
            return s;
        } catch (Exception e) {
            logger.info("aes解密出现异常详情,{}",new Object[]{ExceptionUtils.getFullStackTrace(e)});
            throw new ApiReqException(ApiReqErrorCode.StatusCode.ERROR_500.getCode(),
                    ApiReqErrorCode.StatusCode.ERROR_500.getMsg());
        }
    }

十六进制string转二进制byte[]

public static byte[] hexStringToByte(String s) {
        byte[] baKeyword = new byte[s.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {

            }
        }
        return baKeyword;
    }

字节转字符

public static String byteToHexString(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String strHex = Integer.toHexString(bytes[i]);
            if (strHex.length() > 3) {
                sb.append(strHex.substring(6));
            } else {
                if (strHex.length() < 2) {
                    sb.append("0" + strHex);
                } else {
                    sb.append(strHex);
                }
            }
        }
        return sb.toString();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值