简体   繁体   English

如何在Java中将JSON转换为Base64字符串?

[英]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(Gson),我想先使用Aes256加密此json,然后再将其发送到服务器,因此我必须先将其转换为Base64

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. 但是,它发送格式错误的json,因为它无法正确转换。

EDIT: This is for Android 编辑: 这是为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: 然后,您可以使用以下代码对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: 而且,如果您使用的是Java 8,则可以直接在包中使用Base64类:

import java.util.Base64; 

And your code for encoding into Base64 will change to : 并且您用于编码为Base64的代码将更改为:

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. 像加密值一样的byte[]值,应采用Base64编码。

        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. 这使整个JSON可读的UTF-8文本具有文本值,因为Base64提供了二进制数据的ASCII子集。


The encrypt method is as good as perfect, just String.getBytes() (seen in many examples) would use the platform encoding; encrypt方法非常完美,只是String.getBytes() (在许多示例中可以看到)将使用平台编码。 when this software would ever run on Windows, that encoding would not be UTF-8 but some non-Unicode (= 256 char subset) encoding. 当此软件可以在Windows上运行时,该编码将不是UTF-8,而是某些非Unicode(= 256个字符的子集)编码。 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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