Java Base64 encoding gives different results than C base64 encode

I am trying to encode this byte array:

[237, 217, 204, 218, 109, 227, 157, 145, 35, 152, 85, 142, 182, 180, 120, 8]

      

Using the Java library org.apache.commons.codec.binary.Base64.encodeBase64

and org.bouncycastle.util.encoders.Base64.encode

, these are the results:

[55, 100, 110, 77, 50, 109, 51, 106, 110, 90, 69, 106, 109, 70, 87, 79, 116, 114, 82, 52, 67, 65, 61, 61]

      

(notice the double "=" at the end)

Using base64.c Copyright (c) 1995-2001 Kungliga Tekniska Högskolan (Royal Institute of Technology, Stockholm, Sweden)

this output:

[55, 100, 110, 77, 50, 109, 51, 106, 110, 90, 69, 106, 109, 70, 87, 79, 116, 114, 82, 52, 67, 66, 72, 114]

      

Can someone explain why? How can I get the Java / C library to work the same?

+3


source to share


2 answers


Each Base64 ASCII character contains information about 6 bits (2 6 = 64), so 4 Base64 characters contain 3 bytes. You have 16 bytes, so one byte remains at the end, it needs 2 Base64 characters, and for a group of up to 4 characters, two's complement is added =

.

Mind: With JavaSE 8, the Base64 class came along to replace a few older classes.



Base64 has several areas of application, with various small changes: padding can be omitted, line breaks added to limit line length, etc. Java 8 Base64 even has the option for an incompatible url and safeversion file where +

and are replaced /

.

+1


source


Base64 works on blocks of 3 bytes, and the indentation =

must contain the size of the output to a multiple of 3. This addition is optional and if not, you can simply add it manually by checking the length of the array before trying to decode using Java code.



0


source







All Articles