base64url在java中 [英] base64url in java

查看:630
本文介绍了base64url在java中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href=\"http://en.wikipedia.org/wiki/Base64#URL_applications\">http://en.wikipedia.org/wiki/Base64#URL_applications

有关base64Url会谈 - 德code

talks about base64Url - Decode

用于URL变体的修饰的Base64存在,在没有填充'='将被使用,以及标准的Base64的+和/字符被分别改为' - '和'_'

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 '_'

我创建了以下功能:

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?

推荐答案

通过 Base64编码的使用来自Apache下议院,谁可以被配置到URL的安全,我创建了以下功能:

With the usage of Base64 from Apache Commons, who can be configured to URL safe, I created the following function:

import org.apache.commons.codec.binary.Base64;

public static String base64UrlDecode(String input) {
    String result = null;
    Base64 decoder = new Base64(true);
    byte[] decodedBytes = decoder.decode(input);
    result = new String(decodedBytes);
    return result;
}

构造的Base64(真)使得解码URL安全的。

The constructor Base64(true) makes the decoding URL-safe.

这篇关于base64url在java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆