How to convert JSON to Base64 string in Java?
15,043
You can import the library:
import org.apache.commons.codec.binary.Base64;
Then you can use following code for encoding into Base64:
public byte[] encodeBase64(String encodeMe){
byte[] encodedBytes = Base64.encodeBase64(encodeMe.getBytes());
return encodedBytes ;
}
and for decoding you can use
public String decodeBase64(byte[] encodedBytes){
byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
return new String(decodedBytes)
}
And if you are using Java 8 then you have Base64 class directly available into package:
import java.util.Base64;
And your code for encoding into Base64 will change to :
public String encodeBase64(byte [] encodeMe){
byte[] encodedBytes = Base64.getEncoder().encode(encodeMe);
return new String(encodedBytes) ;
}
and similarly your new decoding will change as
public byte[]decodeBase64(String encodedData){
byte[] decodedBytes = Base64.getDecoder().decode(encodedData.getBytes());
return decodedBytes ;
}
Author by
Ege Kuzubasioglu
Updated on June 04, 2022Comments
-
Ege Kuzubasioglu almost 2 years
I have a JsonObject (Gson) I want to encrypt this json with Aes256 before I send it to the server, so I have to convert it to Base64 first
JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("command", "abc"); String body = Base64.encodeToString(jsonObject.toString().getBytes("UTF-8"), Base64.NO_WRAP); String finalBody = aesKeyIv.encrypt(body);
However it sends malformed json because it cannot convert properly.
EDIT: This is for Android
My encrypt method:
public String encrypt(String value) throws Exception { byte[] encrypted = cipherEnc.doFinal(value.getBytes()); return Base64.encodeToString(encrypted, Base64.NO_WRAP); }
-
Tamas Hegedus over 6 yearsAes is capable of encrypting any array of bytes (thus every utf-8 encoded strings) not just base64. And of course an encrypted value seems to be just a random sequence of bytes, it will never be a valid json. What are you trying to achieve? How do you send and receive the encrypted data? How do you exchange your keys? And most importantly what do you mean by "it cannot convert properly"? Do you get an error?
-
Robert over 6 yearsThe base64 value is correct. Therefore the problem is not in the presented code. I assume that the
encrypt()
method is defect. -
Ege Kuzubasioglu over 6 years@Robert please see my edit. I added the encrypt method
-
-
Ege Kuzubasioglu over 6 yearsSo should I encrypt each property one by one and put them in to the json object and then encrypt it again?
-
Joop Eggen over 6 yearsNo, Base64 is not an encryption, but translates binary bytes into ASCII text using
A-z0-9/+
as 64 digits for a huge "number," every digit storing 6 bits. Hence only the values that werebyte[]
. Those bytes would most likely not have been correct UTF-8. -
Ege Kuzubasioglu over 6 yearscould you please check my encrypt method in my edit. Is that correct?
-
Joop Eggen over 6 yearsYes it would have worked, though I added a small change to make this code (and especially the data) cross-platform. Mind the decryption.