String转Base64编码的实现

Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一

Base64要求把每三个8Bit的字节转换为四个6Bit的字节(3*8 = 4*6 = 24),然后把6Bit再添两位高位0,组成四个8Bit的字节,也就是说,转换后的字符串理论上将要比原来的长1/3。

比如某一字符串转成字节码为:
11011001 01101010 00101100
110110010110101000101100
00110110 00010110 00101000 00101100

把8位的字节连成一串110110010110101000101100
然后每次顺序选6个出来之后再把这6二进制数前面再添加两个0,就成了一个新的字节。之后再选出6个来,再添加0,依此类推,直到24个二进制数全部被选完。

当代码量不是3的整数倍时,代码量/3的余数自然就是2或者1。转换的时候,结果不够6位的在后面用0来补上相应的位置,之后再在6位的前面补两个0。转换完空出的结果就用就用“=”来补位。
如果余1,就补“==”
如果余2,就补“=”

下面是具体的代码实现:

import sun.misc.BASE64Decoder;

public class Base64Test {

private final static String CODE_STR = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
private final static int ORGINAL_LEN = 8;
private final static int NEW_LEN = 6;

public static void main(String[] args){
System.out.println(encodeBase64("honda418"));
System.out.println(getBASE64("honda418"));
}

/**
* 把str转换成Base64编码
* @param str
* @return
*/
public static String encodeBase64(String str){
String binStr = getBinStr(str);

int oldByteLen = binStr.length() / ORGINAL_LEN; //原始字节数,数字字母占1个,中文占2个
int newByteLen = oldByteLen * 4 / 3; //新的字节数

String appendStr = ""; //最后面该不该加“=”

switch(oldByteLen % 3){
case 0 : break;
case 1 : newByteLen = newByteLen + 1; appendStr = "=="; break;
case 2 : newByteLen = newByteLen + 1; appendStr = "="; break;
}

StringBuilder base64Str = new StringBuilder("");
for(int k = 0; k < newByteLen; k++){
String bin = "";
// 二进制字符串按照6个一组分隔
if((k+1) * NEW_LEN > binStr.length()){
bin = binStr.substring(k * NEW_LEN, binStr.length());
}else{
bin = binStr.substring(k * NEW_LEN, (k+1) * NEW_LEN);
}

int intval = Integer.parseInt(bin, 2); //二进制转成十进制
if(bin.length() < NEW_LEN){
/**
* 不足6位时右侧补0
* 0101 -- > 010100 (补2个0)
* 10 -- > 100000 (补4个0)
*/
intval <<= NEW_LEN - bin.length();
}
//从码表里面查找当前字符,返回字符在码表中的位置
base64Str = base64Str.append(CODE_STR.charAt(intval));
}
base64Str = base64Str.append(appendStr);
return base64Str.toString();
}

/**
* 把字符串转行成二进制,再把二进制拼接起来
* 返回拼接后的二进制字符串
* @param str
* @return
*/
public static String getBinStr(String str){
// 采用默认语言环境的 character set。
byte[] b = str.getBytes();
StringBuilder sb = new StringBuilder("");
for(byte c : b){
String tmpStr = Integer.toBinaryString(c);
System.out.println(tmpStr);
if(tmpStr.length() > ORGINAL_LEN){
//截取, 如: "1111111111111111111111111010101" to "11010101"
sb.append(tmpStr.substring(tmpStr.length() - ORGINAL_LEN, tmpStr.length()));
}else if(tmpStr.length() < ORGINAL_LEN){
//前补0, 如: "110011" to "00110011"
int i = 0;
while(tmpStr.length() + i < ORGINAL_LEN){
i ++;
sb.append("0");
}
sb.append(tmpStr);
}else{
sb.append(tmpStr);
}
}
return sb.toString();
}


// 用SUN自带的进行BASE 64编码
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个方法是sun自带的编码解码方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值