Encoding as Base64 in Java

前端 未结 17 2524
失恋的感觉
失恋的感觉 2020-11-21 13:44

I need to encode some data in the Base64 encoding in Java. How do I do that? What is the name of the class that provides a Base64 encoder?


I tried to use the <

17条回答
  •  梦谈多话
    2020-11-21 13:51

    Apache Commons has a nice implementation of Base64. You can do this as simply as:

    // Encrypt data on your side using BASE64
    byte[] bytesEncoded = Base64.encodeBase64(str .getBytes());
    System.out.println("ecncoded value is " + new String(bytesEncoded));
    
    // Decrypt data on other side, by processing encoded data
    byte[] valueDecoded= Base64.decodeBase64(bytesEncoded );
    System.out.println("Decoded value is " + new String(valueDecoded));
    

    You can find more details about base64 encoding at Base64 encoding using Java and JavaScript.

提交回复
热议问题