java - 加密 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);
}
解决方案
//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。
推荐阅读
- ios - 在 App Store Connect 中更改现有 IAP 自动续订订阅
- bash - 如何防止执行 shell 命令?
- powershell - 需要使用 powershell 将文件中的 \x0d\x0a 替换为 \x2c\x0d\x0a
- javascript - 从一个组件对服务进行ajax调用并从另一个组件访问响应
- android - Android - 获取位置(地理编码器/谷歌播放服务)?
- spring - 如何确定我的 Spring/Quartz 配置使用了多少连接?
- android - 将微调器保存到共享首选项的正确方法
- java - 在 OnCreate 中添加另一个类的数组
- mysql - mysql更新表并返回所有更新的记录
- netsuite - Netsuite RESTlet 无法设置 SalesRep