Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Base64 encode gives different result on linux CentOS terminal and in Java

Tags:

java

linux

base64

I am trying to generate some random password on Linux CentOS and store it in database as base64. Password is 'KQ3h3dEN' and when I convert it with 'echo KQ3h3dEN | base64' as a result I will get 'S1EzaDNkRU4K'. I have function in java:

public static String encode64Base(String stringToEncode)
{
    byte[] encodedBytes = Base64.getEncoder().encode(stringToEncode.getBytes());
    String encodedString = new String(encodedBytes, "UTF-8");

    return encodedString;
}

And result of encode64Base("KQ3h3dEN") is 'S1EzaDNkRU4='.

So, it is adding "K" instead of "=" in this example. How to ensure that I will always get same result when using base64 on linux and base64 encoding in java?

UPDATE: Updated question as I didn't noticed "K" at the end of linux encoded string. Also, here are few more examples:

  • 'echo KQ3h3dENa | base64' => result='S1EzaDNkRU5hCg==', but it should be 'S1EzaDNkRU5h'
  • echo KQ3h3dENaa | base64' => result='S1EzaDNkRU5hYQo=', but it should be 'S1EzaDNkRU5hYQ=='
like image 671
Jonhtra Avatar asked Jun 01 '16 08:06

Jonhtra


People also ask

Is Base64 encoding always the same?

Artjom B. Base64 is not encryption. But yes, different input strings will always encode to different Base64-encoded strings, and the same input string will always encode to the same Base64-encoded string. It's not a hash though, so small changes in the input will only result in small changes in the output.

What is the output of Base64 decode?

The result of decoding base64 is binary data (and likewise the input when encoding it is binary data). To go from binary data to a string or vice versa, you need to apply an encoding such as UTF-8 or UTF-16.

How read Base64 encoded data in Linux?

To decode with base64 you need to use the --decode flag. With encoded string, you can pipe an echo command into base64 as you did to encode it. Using the example encoding shown above, let's decode it back into its original form. Provided your encoding was not corrupted the output should be your original string.

Do Base64 encoding Java?

Java 8 now has inbuilt encoder and decoder for Base64 encoding. In Java 8, we can use three types of Base64 encoding. Simple − Output is mapped to a set of characters lying in A-Za-z0-9+/. The encoder does not add any line feed in output, and the decoder rejects any character other than A-Za-z0-9+/.


2 Answers

Found solution after few hours of experimenting. It seems like new line was added to the string I wanted to encode. Solution would be : echo -n KQ3h3dEN | base64

Result will be the same as with java base64 encode.

like image 142
Jonhtra Avatar answered Oct 09 '22 17:10

Jonhtra


Padding

The '==' sequence indicates that the last group contained only one byte, and '=' indicates that it contained two bytes.

In theory, the padding character is not needed for decoding, since the number of missing bytes can be calculated from the number of Base64 digits. In some implementations, the padding character is mandatory, while for others it is not used.

So it depends on tools and libraries you use. If base64 with padding is the same as without padding for them, there is no problem. As an insurance you can use on linux tool that generates base64 with padding.

like image 32
Abylay Sabirgaliyev Avatar answered Oct 09 '22 18:10

Abylay Sabirgaliyev