Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - The method encodeBase64

I have a String String x = "Sample text"; and I want to print the base64 encryption of it. As numerous examples mention, I use:

byte[] encodedBytes = Base64.encodeBase64(x.getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));

But this gives me The method encodeBase64(byte[]) is undefined for the type Base64... Why is that?

like image 584
Oti Na Nai Avatar asked Apr 27 '15 04:04

Oti Na Nai


People also ask

What is Base64 format in Java?

Base 64 is an encoding scheme that converts binary data into text format so that encoded textual data can be easily transported over network un-corrupted and without any data loss. (Base 64 format reference).

How do I check if a string is Base64 encoded?

In base64 encoding, the character set is [A-Z, a-z, 0-9, and + /] . If the rest length is less than 4, the string is padded with '=' characters. ^([A-Za-z0-9+/]{4})* means the string starts with 0 or more base64 groups.

What is Java Util Base64?

public class Base64 extends Object. This class consists exclusively of static methods for obtaining encoders and decoders for the Base64 encoding scheme. The implementation of this class supports the following types of Base64 as specified in RFC 4648 and RFC 2045.

What is import Java Util Base64?

Java provides a class Base64 to deal with encryption. You can encrypt and decrypt your data by using provided methods. You need to import java. util. Base64 in your source file to use its methods.


2 Answers

The encode method is in the Base64.Encoder class that you can get by running Base64.getEncoder().

byte[] encodedBytes = Base64.getEncoder().encode(x.getBytes());

Similarly, to decode:

String originalString = new String(Base64.getDecoder().decode(encodedBytes));

Check out the Base64 javadocs for more info.

like image 177
Anubian Noob Avatar answered Oct 17 '22 16:10

Anubian Noob


Instead of rewriting the code you should simply include the Apache commons codec module into your classpath. This has the exact method signature, Base64.encodeBase64(byte[]). And yes, just hitting "encodeBase64" in an internet search should be enough.

Note that your project may use other functionality from that same codecs package. So you should always first try and find the modules / libraries used by a project before you start replacing the code. And sometimes there are subtle differences between implementations, which could make the project fail in the right circumstances.

That said, the code in the question does of course not make sense. You could use encodeBase64String to directly convert to String without having to convert to bytes and then to String. And for newer level API's there is indeed the java.util.Base64 class, as mentioned in the other answer. Finally - and more importantly - if x already contains a string, why would you base 64 encode it?

like image 40
Maarten Bodewes Avatar answered Oct 17 '22 15:10

Maarten Bodewes