Android base64加密中文乱码问题解决记录

1、最近做个react-native 项目,要求有个接口需要base64加密,使用js加密后发现中文会无法base64解密

解决办法

 1、导入2个包,如果没有则请自行下载

import java.io.UnsupportedEncodingException;
import sun.misc.BASE64Decoder;
//base64 encode
public static String encode(String s) { if (s == null) return null; String res = ""; try { res = new sun.misc.BASE64Encoder().encode(s.getBytes("GBK")); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return res; }

  

//base64 decode
public static String decode(String s) { if (s == null) return null; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b,"GBK"); } catch (Exception e) { return null; } }

 如果Android 无法直接使用上面的包 ,请下载下面这个文件,亲测有效 

下载链接:链接: https://pan.baidu.com/s/1UI-cFwK_cfWDPAybI79Ndw 提取码: crh2

猜你喜欢

转载自www.cnblogs.com/pxjbk/p/12585182.html
今日推荐