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

發佈了30 篇原創文章 · 獲贊 18 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章