小弟 最近从PHP那传过来经过Base64编码的字串,可是发现我用android的Base64解出来的字串竟然是不一样的。
请问这是为什麼,我android端又该怎麼写呢?
android Base64编码 跟 PHP Base64编码 差在哪?
答案:3 悬赏:20 手机版
解决时间 2021-01-29 12:24
- 提问者网友:温柔港
- 2021-01-29 06:08
最佳答案
- 五星知识达人网友:逃夭
- 2021-01-29 07:36
Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的唯一标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到。
转码过程例子:
3*8=4*6
内存1个字符占8位
转前: s 1 3
先转成ascii:对应 115 49 51
2进制: 01110011 00110001 00110011
6个一组(4组) 011100110011000100110011
然后才有后面的 011100 110011 000100 110011
然后计算机是8位8位的存数 6不够,自动就补两个高位0了
所有有了 高位补0
再来看下代码
android(java)版
import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; // 将 s 进行 BASE64 编码 public static String getBASE64(String s) { if (s == null) return null; return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); } // 将 BASE64 编码的字符串 s 进行解码 public static String getFromBASE64(String s) { if (s == null) return null; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (Exception e) { return null; } }
PHP版
[下列代码仅在GBK中实现,UTF8代码请把 if($button=="某某地址->普通地址") echo substr(base64_decode(str_ireplace("xx://","",$txt1)),2,-2); 这句改为if($button=="某某地址->普通地址") echo substr(mb_convert_encoding(base64_decode(str_ireplace("xx://","",$txt1))),2,-2); 并把charset=gb2312改为charset=utf-8]
转码过程例子:
3*8=4*6
内存1个字符占8位
转前: s 1 3
先转成ascii:对应 115 49 51
2进制: 01110011 00110001 00110011
6个一组(4组) 011100110011000100110011
然后才有后面的 011100 110011 000100 110011
然后计算机是8位8位的存数 6不够,自动就补两个高位0了
所有有了 高位补0
再来看下代码
android(java)版
import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; // 将 s 进行 BASE64 编码 public static String getBASE64(String s) { if (s == null) return null; return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); } // 将 BASE64 编码的字符串 s 进行解码 public static String getFromBASE64(String s) { if (s == null) return null; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (Exception e) { return null; } }
PHP版
[下列代码仅在GBK中实现,UTF8代码请把 if($button=="某某地址->普通地址") echo substr(base64_decode(str_ireplace("xx://","",$txt1)),2,-2); 这句改为if($button=="某某地址->普通地址") echo substr(mb_convert_encoding(base64_decode(str_ireplace("xx://","",$txt1))),2,-2); 并把charset=gb2312改为charset=utf-8]
全部回答
- 1楼网友:詩光轨車
- 2021-01-29 09:36
import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; // 将 s 进行 BASE64 编码 public static String getBASE64(String s) { if (s == null) return null; return (new sun.misc.BASE64Encoder()).encode( s.getBytes() ); } // 将 BASE64 编码的字符串 s 进行解码 public static String getFromBASE64(String s) { if (s == null) return null; BASE64Decoder decoder = new BASE64Decoder(); try { byte[] b = decoder.decodeBuffer(s); return new String(b); } catch (Exception e) { return null; } }
- 2楼网友:野味小生
- 2021-01-29 08:09
参考如下代码:
package com.android20;
import sun.misc.base64decoder;
public class bian {
//将 s 进行 base64 编码
public static string getbase64(string s) {
if (s == null) return null;
return (new sun.misc.base64encoder()).encode( s.getbytes() );
}
//将 base64 编码的字符串 s 进行解码
public static string getfrombase64(string s) {
if (s == null) return null;
base64decoder decoder = new base64decoder();
try {
byte[] b = decoder.decodebuffer(s);
return new string(b);
} catch (exception e) {
return null;
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯