首页 > 解决方案 > 加密 Deflate 并编码为 Base64 Xml

问题描述

// XML 需要使用 BASE64 进行加密、放气和编码?我已经为此使用了此代码,但我没有得到确切的输出。

public String encryptDeflateAndBase64(String decryptedString) throws IOException {
        byte[] b=decryptedString.getBytes("UTF-8");
        byte[] c = new byte[550];
        Deflater compresser=new Deflater();
        compresser.setInput(b);
        compresser.finish();
        System.out.println(compresser.deflate(c));
        compresser.end();
        /*ByteArrayOutputStream bos = new ByteArrayOutputStream(decryptedString.length());
        byte[] buf = new byte[4096];
        while (!compresser.finished()) {
            int count = compresser.deflate(buf);
            bos.write(buf, 0, count);
        }
        bos.close();
        byte[] compressedData = bos.toByteArray();
*/       byte[] output = Base64.encodeBase64(c);
        return new String(output);
    }

标签: javaencodingautomationbase64inflate

解决方案


    //Remove: System.out.println(compresser.deflate(c));
    int count = compresser.deflate(c);
    ByteBuffer bb = ByteBuffer.wrap(c, 0, count);
    byte[] output = Base64.getEncoder().encode(bb);

我使用 java.util.Base64 的地方。为了不复制字节,我使用了 ByteBuffer。


推荐阅读