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