在Java中解码base64Url
https://web.archive.org /web/20110422225659/https://en.wikipedia.org/wiki/Base64#URL_applications
谈论base64Url -解码
存在 URL 变体的修改后的 Base64,其中不会使用填充“=”,并且标准 Base64 的“+”和“/”字符分别替换为“-”和“_”
我创建了以下函数
public static String base64UrlDecode(String input) {
String result = null;
BASE64Decoder decoder = new BASE64Decoder();
try {
result = decoder.decodeBuffer(input.replace('-','+').replace('/','_')).toString();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
return result;
}
:返回非常小的字符集,甚至与预期结果都不相似。 有什么想法吗?
https://web.archive.org/web/20110422225659/https://en.wikipedia.org/wiki/Base64#URL_applications
talks about base64Url - Decode
a modified Base64 for URL variant exists, where no padding '=' will be used, and the '+' and '/' characters of standard Base64 are respectively replaced by '-' and '_'
I created the following function:
public static String base64UrlDecode(String input) {
String result = null;
BASE64Decoder decoder = new BASE64Decoder();
try {
result = decoder.decodeBuffer(input.replace('-','+').replace('/','_')).toString();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
return result;
}
it returns a very small set of characters that don't even resemble to the expected results.
any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Java8+
Java8+
Base64 编码自 Java 8 起成为 JDK 的一部分。 URL 安全编码 也支持 java.util.Base64.getUrlEncoder() 和“
=
" 可以通过另外使用 java.util.Base64.Encoder.withoutPadding() 方法:Base64 encoding is part of the JDK since Java 8. URL safe encoding is also supported with java.util.Base64.getUrlEncoder(), and the "
=
" padding can be skipped by additionally using the java.util.Base64.Encoder.withoutPadding() method:通过使用可配置为 URL 安全的 Apache Commons 的
Base64
,我创建了以下函数:构造函数
Base64(true)
使解码 URL 安全。With the usage of
Base64
from Apache Commons, who can be configured to URL safe, I created the following function:The constructor
Base64(true)
makes the decoding URL-safe.在Android SDK中,
Base64
类中有一个专用标志:Base64.URL_SAFE
,像这样使用它来解码为字符串:In the Android SDK, there's a dedicated flag in the
Base64
class:Base64.URL_SAFE
, use it like so to decode to a String:Guava 现在内置了 Base64 解码。
https://google.github.io/guava/releases/17.0/api/docs/com/google/common/io/BaseEncoding.html
Guava now has Base64 decoding built in.
https://google.github.io/guava/releases/17.0/api/docs/com/google/common/io/BaseEncoding.html
立刻,看起来你的
replace()
是向后的;该方法用第二个字符替换第一个字符的出现,而不是相反。Right off the bat, it looks like your
replace()
is backwards; that method replaces the occurrences of the first character with the second, not the other way around.@ufk 的答案有效,但在解码时实际上不需要设置 urlSafe 标志。
此外,还有一些静态帮助程序可以使其更短、更明确:
文档
@ufk's answer works, but you don't actually need to set the
urlSafe
flag when you're just decoding.Also, there are some static helpers to make it shorter and more explicit:
Docs
本课程可以帮助:
This class can help:
我知道答案已经存在,但是,如果有人想要...
从以下位置获取帮助: https://www.javatpoint.com/java-base64-encode-decode
I know the answer is already there, but still, if someone wants...
Took help from: https://www.javatpoint.com/java-base64-encode-decode
Base64.getUrlEncoder()
已使用-
、_
而不是+
、/
。请参阅:
java-1.8.0/src.zip!/java/util/Base64.java
java.util.Base64
Base64.getUrlEncoder()
already using-
,_
instead of+
,/
.See:
java-1.8.0/src.zip!/java/util/Base64.java
java.util.Base64
在 Java 中,尝试使用 Commons Codec 库中的方法
Base64.encodeBase64URLSafeString()
进行编码。In Java try the method
Base64.encodeBase64URLSafeString()
from Commons Codec library for encoding.