Java Base64 Decode
Since Java 1.8 developers can use the Base64.Decoder
class to decode Base64 values. This class provides several methods for decoding Base64 to byte data using algorithms specified in RFC 4648 and RFC 2045. In addition, Java provides the Base64.Encoder
class for encoding data to Base64.
Example #1 (decode string):
import java.util.Base64;
class Base64Decode {
public static void main(String[] args) {
String b64 = "Z3VydQ==";
byte[] decoder = Base64.getDecoder().decode(b64);
String str = new String(decoder);
System.out.println(str); //-> "guru"
}
}
Comments (36)
I hope you enjoy this discussion. In any case, I ask you to join it.
That’s right, you should use the same method, both for decoding text and for binary data. Just keep in mind that the
decode()
method returns a byte array. By the way, you can see a full example here: Convert Base64 to PDF in Java.Also in the same project, I want to encode the Japanese language character in java and decode in java.
Means to say that I have to encode character from 2 sides there are 2 encoders one on js and other is from java but the decoder for both of them is same.
I am using JSP and Struts2 action.
Please help me I am just stuck in that project.
Thanks in advance.
You did not describe the encountered error, so I suppose it is related to the fact that btoa() cannot encode Unicode characters. To fix it, check Implementing Unicode support for btoa() and atob().
Lcom/github/megatronking/stringfog/xor/StringFogImpl;->decrypt(Ljava/lang/String;)Ljava/lang/String