Java字符串base64编码算法以匹配openssl实现
作为尝试使用 Google Search Appliance 配置 SAML AuthN 的一部分,我需要将响应转换为 base64。为了进行概念证明,我只是使用 IntelliJ IDEA 中的调试器将字符串替换为 openssl 命令生成的版本:
openssl base64 -in inFileName -out outFileName
现在我需要在没有我直接干预的情况下获得一个可以工作的版本。我正在使用 Apache commons base64 编解码器库(v.1.4),字符集UTF-8,lineLength 64,代码如下所示:
Base64 encoder = new Base64(64);
signedSamlResponse = signedSamlResponse.replaceAll("[\n\r]","");
byte[] bytes = encoder.encode(signedSamlResponse.getBytes("UTF-8"));
signedSamlResponse = new String(bytes,0,bytes.length);
结果非常接近我需要的。如果我在 apache 库执行此操作之前对字符串进行 openssl 加密(在第 3 行执行之前复制signedSamlResponse),然后运行 diff,这两个结果几乎相同。唯一的区别是最后一行中的倒数第二个字符,并且这种区别在我的所有尝试中都是一致的。
Openssl 版本:
dD48L0Fzc2VydGlvbj48L3NhbWxwOlJlc3BvbnNlPgo=
Apache 版本:
dD48L0Fzc2VydGlvbj48L3NhbWxwOlJlc3BvbnNlPg==
我需要对字节数组或其来源的字符串执行什么操作才能使两个结果匹配?
As part of attempting to configure SAML AuthN with a Google Search Appliance, I need to convert my response into base64. For proof of concept, I just used the debugger in IntelliJ IDEA to replace the string with a version generated by the openssl command:
openssl base64 -in inFileName -out outFileName
Now I need to get a version working without my direct intervention. I'm using the Apache commons base64 codec library (v. 1.4), charset UTF-8, lineLength 64, and the code looks like this:
Base64 encoder = new Base64(64);
signedSamlResponse = signedSamlResponse.replaceAll("[\n\r]","");
byte[] bytes = encoder.encode(signedSamlResponse.getBytes("UTF-8"));
signedSamlResponse = new String(bytes,0,bytes.length);
The result is quite close to what I need. If I do an openssl encryption on the string just before the apache library does it's thing (copy signedSamlResponse before line 3 executes), and then run a diff the two results they are nearly identical. The only difference is the second last character in the last line and this difference has been consistant across all my attempts.
Openssl version:
dD48L0Fzc2VydGlvbj48L3NhbWxwOlJlc3BvbnNlPgo=
Apache version:
dD48L0Fzc2VydGlvbj48L3NhbWxwOlJlc3BvbnNlPg==
What do I need to do to the byte array or the String it comes from in order to get the two results to match?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来 openssl 版本以“\n”结尾 - 因此在第二行中将它们全部删除后,您应该在末尾添加一个:)
(基本上,openssl 的字节数组末尾有一个额外的 0x0a;您提供的 Apache 版本没有。)
It looks like the openssl version ends with a "\n" - so having removed them all on your second line, you should add one at the end :)
(Basically, the byte array from openssl has an extra 0x0a at the end; the Apache version you've given doesn't.)