【问题标题】:Java Inflater throws a DataFormatException "invalid code length set"Java Inflater 抛出 DataFormatException “无效的代码长度集”
【发布时间】:2020-03-26 17:58:37
【问题描述】:

我正在使用充气机解压缩 SAMLRequest。由于这个值是用 GZIP 压缩的,我设法将“true”传递给 inflater 构造函数,以提供与这种格式的兼容性。但是,膨胀的行会引发 DataFormatException。

    private String decodeMessage(String SAMLContent) {
        try {
            //URLDecode, Base64 and inflate data

            //URLDecode
            SAMLContent = URLDecoder.decode(SAMLContent, "UTF-8");

            //Base64 decoding
            byte[] decode = Base64.getDecoder().decode(SAMLContent);
            SAMLContent = new String(decode, "UTF-8");
            //SAMLContent = new String(Base64.getDecoder().decode(SAMLContent), "UTF-8");

            //Inflating data
            try {
                byte[] inflated = new byte[(10 * SAMLContent.getBytes("UTF-8").length)];
                Inflater i = new Inflater(true);
                i.setInput(SAMLContent.getBytes("UTF-8"), 0, SAMLContent.getBytes("UTF-8").length);

                //The following line throws DFException
                int finalSize = i.inflate(inflated);

                SAMLContent = new String(SAMLContent.getBytes("UTF-8"), 0, finalSize, "UTF-8");
                i.end();

            } catch (DataFormatException ex) {
                JOptionPane.showMessageDialog(null, "DFE: " + ex.getMessage());  //Returns "invalid code length set"
            }

        } catch (UnsupportedEncodingException ex) {
            JOptionPane.showMessageDialog(null, "UEE: " + ex.getMessage());
        }

        return SAMLContent;
    }

在第 20 行引发了异常。我尝试复制的工作流是

  1. 复制请求的值 (This one for instance)
  2. 使用this URL decoder to decode it(左下角的文本框)
  3. 将第二步的结果粘贴到这个Base64decoder + inflater中以获得原始XML,如最后一页的第三个文本框所示。

【问题讨论】:

  • 不要重新发明轮子。使用OpenSAML 或查看他们的code 并提取相关部分。
  • 因为这是一个简单的步骤,而且目前是我唯一需要的函数,所以我想在不使用外部库的情况下编写它。我正在使用工作算法添加答案。但是,如果需要,我一定会记住 OpenSAML。谢谢。

标签: java saml inflate


【解决方案1】:

这是字节数组和字符串之间多次转换的问题,这可能导致某处信息丢失。这是我正在使用的工作代码。

private String decodeHeader(String SAMLContent) {
    try {
        SAMLContent = URLDecoder.decode(SAMLContent, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        JOptionPane.showMessageDialog(null, "UEException: " + ex.getMessage());
    }

    byte[] deflatedBytes = Base64.getDecoder().decode(SAMLContent);
    byte[] inflatedBytes = new byte[100*deflatedBytes.length];
    Inflater compressor = new Inflater(true);

    compressor.setInput(deflatedBytes, 0, deflatedBytes.length);

    try {
        int size = compressor.inflate(inflatedBytes);
    } catch (DataFormatException ex) {
        JOptionPane.showMessageDialog(null, "DFException: " + ex.getMessage());
    }

    try {
        return new String(inflatedBytes, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        JOptionPane.showMessageDialog(null, "UEException: " + ex.getMessage());
    }
    JOptionPane.showConfirmDialog(null, "This shouln't be printed");
    return null;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 2012-10-30
    • 2016-06-13
    • 1970-01-01
    相关资源
    最近更新 更多