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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM