开发报错误java.lang.IllegalArgumentException: Last encoded character (before the paddings if any)

企业微信回调时服务器报java.lang.IllegalArgumentException: Last encoded character (before the paddings if any) 错误。
找了好久都没有找到解决的办法,有可能跟加解密有关,主要问题说是升级 commons-codec 至 1.13 版本后,加解密时出现 java.lang.IllegalArgumentException: Last encoded character (before the paddings if any) is a valid base 64 alphabet but not a possible value. Expected the discarded bits to be zero. 异常

问题原因

commons-codec 在 1.13 版本引入了 validateCharacter 方法,来验证在上下文中是否可以解码最后的尾随字符,微信生成的 EncodingAESKey 无法通过校验

/**
     * Validates whether decoding the final trailing character is possible in the context
     * of the set of possible base 64 values.
     *
     * <p>The character is valid if the lower bits within the provided mask are zero. This
     * is used to test the final trailing base-64 digit is zero in the bits that will be discarded.
     *
     * @param emptyBitsMask The mask of the lower bits that should be empty
     * @param context the context to be used
     *
     * @throws IllegalArgumentException if the bits being checked contain any non-zero value
     */
    private static void validateCharacter(final int emptyBitsMask, final Context context) {
        if ((context.ibitWorkArea & emptyBitsMask) != 0) {
            throw new IllegalArgumentException(
                "Last encoded character (before the paddings if any) is a valid base 64 alphabet but not a possible value. " +
                "Expected the discarded bits to be zero.");
        }
    }

解决办法

使用 commons-codec 加密 32 位字符(我是用的 UUID 生成 ID 后去除 -) 生成一个 EncodingAESKey 替换微信生成的 EncodingAESKey

Base64.encodeBase64String(UUID.randomUUID().toString().replaceAll("-","").getBytes());  

commons-codec 加密 32 位字符串得到一个 44 位字符串,去除末尾等号得到 43 位EncodingAESKey
补充说明:我直接使用企业微信重新自动生成EncodingAESKey也可以加解密了

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值