6

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?

3
  • What package is your Base64 from? Commented Apr 27, 2015 at 4:46
  • Um? I don't know what that means... I just added import java.util.Base64; as suggested =/ Commented Apr 27, 2015 at 4:50
  • 1
    Base64 is not encryption, it is (binary to text) encoding. Commented Jul 29, 2018 at 1:46

4 Answers 4

14

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.

Sign up to request clarification or add additional context in comments.

3 Comments

If I'm not wrong, the decode statement above calls the variable 'encodedBytes' like a function 'encodedBytes()'. Please remove the brackets at the end.
The issue @Ramraj mentioned was fixed by this edit: stackoverflow.com/revisions/29887426/2
convert encodedBytes to String, just pass in new String() constructor parameter. new String(encodedBytes)
6

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?

Comments

-1

You could also use:

byte[] originalString = Base64.getDecoder().decode(encodedBytes);

to decode (this is what I used in a Base64 encoder/decoder applet I recently wrote)

Comments

-1

I kinda solved it but check if it works

decode.setText(new String(Base64.decode(inputText.getText().toString(), Base64.DEFAULT)));

Tell me if it doesn't work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.