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