简体   繁体   中英

How to convert JSON to Base64 string in Java?

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);
    }

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 ;
    }

您可以使用Bouncy Castle和Apache Commons api转换为Base64,但是要确保使用相同的算法进行编码和解码。

The byte[] values like the encrypted value, should be Base64 encoded.

        String aesIv2EncryptedString = Base64.encodeToString(aesIv2Encrypted,
            Base64.NO_WRAP);
        jsonObject.addProperty("asIv", aesIv2EncryptedString);

This makes the entire JSON readable UTF-8 text with text values, as Base64 delivers an ASCII subset of the binary data.


The encrypt method is as good as perfect, just String.getBytes() (seen in many examples) would use the platform encoding; when this software would ever run on Windows, that encoding would not be UTF-8 but some non-Unicode (= 256 char subset) encoding. Hence characters would be unconvertible ("?") and garbled. So the cross-platform could is:

public String encrypt(String value) throws Exception {
    byte[] encrypted = cipherEnc.doFinal(value.getBytes("UTF-8"));
    return Base64.encodeToString(encrypted, Base64.NO_WRAP);
}

On decryption:

byte[] originalValue = ...;
return new String(originalValue, "UTF-8");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM