getUrlEncoder/getUrlDecoder问题中的Java填充

huangapple go评论73阅读模式
英文:

getUrlEncoder/getUrlDecoder proplem padding in Java

问题

我想将字符串编码和解码为不带填充的Base64字符串
以下代码是否正确

```java
    String test = Base64.getUrlEncoder().withoutPadding().encodeToString(test);
    String output = Base64.getUrlDecoder().decode(test);

解码时,不带填充的长度是否始终正确?


<details>
<summary>英文:</summary>

I want to encode and decode a string into base64 without padding.

it&#39;s correct following code?

```java
    String test = Base64.getUrlEncoder().withoutPadding().encodeToString(test);
    String output = Base64.getUrlDecoder().decode(test);

The lenght without padding always be correct in decoding?

答案1

得分: 2

如果您想对一个字符串变量进行编码和解码可以使用以下代码

```java
String s = "test";
String test = Base64.getUrlEncoder().withoutPadding().encodeToString(s.getBytes(StandardCharsets.UTF_8)); // dGVzdA
String output = new String(Base64.getUrlDecoder().decode(test), StandardCharsets.UTF_8); // test

您可以在这个帖子中获取关于Base64的更多信息。还有一个关于填充字符的不错的帖子。我在JavaCodeGeeks上找到了一个简短的教程

> 默认情况下,如果编码后的字符串长度不达到所需长度,编码会使用'='双等号操作符进行填充。
>
> 通常,编码后的字符串应该是3的倍数,否则将会添加'='字符。
>
> 另一方面,在解码时,所有多余的填充字符都将被丢弃。
>
> 如果确实想要解码,则可以使用withoutPadding()进行无填充编码。
>
> withoutPadding() 方法有助于跳过输出的填充部分。
>
> 许多开发人员认为无填充编码字符串无法解码回原始字符串。
>
> 但是,这是错误的,Base64.Decode API 提供了将其解码回原始形式的灵活性。


<details>
<summary>英文:</summary>

If you want to encode and decode a string variable, you can use the following code:

```java
String s = &quot;test&quot;;
String test = Base64.getUrlEncoder().withoutPadding().encodeToString(s.getBytes(StandardCharsets.UTF_8)); // dGVzdA
String output = new String(Base64.getUrlDecoder().decode(test), StandardCharsets.UTF_8); // test

You can get more information about Base64 in this post. There is also a nice post about padding characters. And I found a short tutorial from JavaCodeGeeks:

> By default, encoding pads with the ‘=’ double equal operator if the encoded string length is not met the desired length.
>
> Typically, an encoded string should be multiples of 3 otherwise it will be added with = character.
>
> On the other side, while decoding all extra padded characters will be discarded.
>
> If you do want to be decoded then encode without padding using withoutPadding().
>
> withoutPadding() method helps to skip the padding of the output.
>
> Many developers think that no padding encoded string cannot be decoded back to the original string.
>
> But, it is wrong and Base64.Decode api provides flexibility to decode back.

答案2

得分: 1

The documentation for Base64.Decoder states:

> Base64填充字符&#39;=&#39; 被接受并解释为编码字节数据的结尾,但不是必需的。

因此,任何不带填充的Base64序列都将被Base64.Decoder接受。

英文:

The documentation for Base64.Decoder says:

> The Base64 padding character &#39;=&#39; is accepted and interpreted as the end of the encoded byte data, but is not required.

So any base 64 sequence encoded without padding will be accepted by Base64.Decoder.

huangapple
  • 本文由 发表于 2020年10月1日 19:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64154047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定