title: How to convert JSON to Base64 string in Java?
body: <p>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</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>However it sends malformed json because it cannot convert properly.</p>
  
  <p>EDIT: <strong>This is for Android</strong></p>
  
  <p>My encrypt method:</p>
  
  <pre><code> public String encrypt(String value) throws Exception {
          byte[] encrypted = cipherEnc.doFinal(value.getBytes());
          return Base64.encodeToString(encrypted, Base64.NO_WRAP);
      }
  </code></pre>