Aes加密及向量补码,InvalidAlgorithmParameterException异常expected IV length of 16 but was 8

前提:AES加密向量是16位,DES加密向量是8位。
由于Android端项目落后,后台(python)和iOS已经调试完毕,用aes加密,向量8位。我在调试的时候怎么都过不了,总是出现InvalidAlgorithmParameterException异常,后来查阅资料加上分析应该是python和ios有自动补码功能,而且补码规则是统一的,很有可能Java没有所以才会出现这个异常。
这里经过分析尝试,最终给出补码方式:

	/**
     * 补码
     * aes加密的向量为16位,des加密的向量为8位,当不足时需要补位,否则抛异常。
     * 补码 这里不能补"0",要补 "\0",因为在ASCII码中"0"为48,所以需要把0转译,故"\0"才为0。
     * @param iv
     * @return
     */
    private static byte[] getIv(String iv){
        StringBuffer buffer = new StringBuffer(16);
        buffer.append(iv);
        while (buffer.length() < 16) {
            buffer.append("\0");
        }
        if (buffer.length() > 16) {
            buffer.setLength(16);
        }
        return buffer.toString().getBytes();
    }

aes加密/解密方式

	private static final String KEY = "0000000000000000";
    private static final String IV = "00000000";
    private static final String AES = "AES";

/**
     * aes加密
     * @param content
     * @return
     */
    public static String encryptIv(String content) {
        try {
            byte[] raw = KEY.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv1 = new IvParameterSpec(getIv(IV));
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv1);
            byte[] encrypted = cipher.doFinal(content.getBytes());
            return new BASE64Encoder().encode(encrypted);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * aes解密
     * @param content
     * @return
     */
    public static String decryptIv(String content) {
        try {
            byte[] byte_content = new BASE64Decoder().decodeBuffer(content);
            IvParameterSpec ips = new IvParameterSpec(getIv(IV));
            SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(), AES);
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ips);
            byte[] bytes = cipher.doFinal(byte_content);
            return new String(bytes, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return "";
    }

下载:BASE64.jar

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值