Base64 encode and decode example code

后端 未结 12 995
遥遥无期
遥遥无期 2020-11-22 15:00

Does anyone know how to decode and encode a string in Base64 using the Base64. I am using the following code, but it\'s not working.

String source = \"passwo         


        
12条回答
  •  星月不相逢
    2020-11-22 15:43

    To anyone else who ended up here while searching for info on how to decode a string encoded with Base64.encodeBytes(), here was my solution:

    // encode
    String ps = "techPass";
    String tmp = Base64.encodeBytes(ps.getBytes());
    
    // decode
    String ps2 = "dGVjaFBhC3M=";
    byte[] tmp2 = Base64.decode(ps2); 
    String val2 = new String(tmp2, "UTF-8"); 
    

    Also, I'm supporting older versions of Android so I'm using Robert Harder's Base64 library from http://iharder.net/base64

提交回复
热议问题