请教各为高手Base64编码问题

lindianxuan 2003-10-09 06:12:56
各位高手能否提供一点信息,给个算法或者API只类的东西,谢谢
...全文
75 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
fpwang 2003-10-09
  • 打赏
  • 举报
回复
/*
这个事我实现的Base64编码器,应该有大优化的余地,但是功能没问题,
在各大邮件服务器测试过

支持中英文的Base64编码解码器

开发者:王飞平
jtchina@sohu.com

Base64 字符表
码值 字符 码值 字符 码值 字符 码值 字符
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v
14 O 31 f 48 w (pad) =
15 P 32 g 49 x
16 Q 33 h 50 y
*/
import java.io.*;

public class Base64{
public static char BASETABLE[]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/','='};

public static String encode(String text){
/*编码算法
1.将数据按3个字节一组分成数块;
2.每块将3个8位的数据转换成4个6位数据段;
11111111 00000000 11111111 ---- 111111 110000 000011 111111
3.根据Base64字符表得到4个6位数据段对应的字符;
4.如果最后一块只有两个字节,则添加两个0位,转换成对应Base64字符表的三个字符,并在结尾添一个'='字符;
如果最后一块只有一个字节,则添加四个0位,转换成对应Base64字符表的两个字符,并在结尾添两个'='字符。
*/
StringBuffer code=new StringBuffer();
byte[] textBytes=text.getBytes();
int textLength=textBytes.length;
int blockCount=(int)textLength/3;
//char[] textChars=new char[textLength];
int ch, bits, textChars[]=new int[textLength];
//text.getChars(0, textLength, textChars, 0);

for(int i=0;i<textLength;i++)
textChars[i]=textBytes[i]>=0 ? textBytes[i] : 256+(int)textBytes[i];

for(int i=0;i<blockCount;i++){
ch=(int)textChars[0+i*3];
ch=ch>>>2;
code.append(BASETABLE[ch]);
bits=(int)textChars[0+i*3]-ch*4;
ch=(int)textChars[1+i*3];
ch=ch>>>4;
code.append(BASETABLE[ch+bits*16]);
bits=(int)textChars[1+i*3]-ch*16;
ch=(int)textChars[2+i*3];
ch=ch>>>6;
code.append(BASETABLE[ch+bits*4]);
bits=(int)textChars[2+i*3]-ch*64;
code.append(BASETABLE[bits]);
}
if((textLength % 3)!=0)
{ ch=(int)textChars[blockCount*3];
ch=ch>>>2;
code.append(BASETABLE[ch]);
bits=(int)textChars[blockCount*3]-ch*4;
switch(textLength % 3){
case 1: code.append(BASETABLE[bits*16]);
code.append(BASETABLE[64]);
code.append(BASETABLE[64]);
break;
case 2: ch=(int)textChars[1+blockCount*3];
ch=ch>>>4;
code.append(BASETABLE[ch+bits*16]);
bits=(int)textChars[1+blockCount*3]-ch*16;
code.append(BASETABLE[bits*4]);
code.append(BASETABLE[64]);
break;
}
}

return code.toString();
}

public static String decode(String code){
/*解码算法
1.将数据按4个字节一组分成数块;
2.每块将4个字符去掉最高两位并转换成3个8位的数据段;
注意:数据块中的字符取值不是ASCII集的值,而是Base64字符表中相对应的索引值!
00 111111 00 110000 00 000011 00 111111 ---- 11111111 00000000 11111111
3.根据ASCII字符集得到3个8位数据段对应的字符;
4.如果最后一块只有两个'=',去掉两个'=',并去掉最低两位,转换成对应ASSCII字符集的两个字符;
如果最后一块只有一个'=',去掉'=',并去掉最低四位,转换成对应ASSCII字符集的一个字符。
*/
//StringBuffer text=new StringBuffer();
int codeLength=code.length();
int blockCount=(int)codeLength/4;
char[] codeChars=new char[codeLength];
int ch, bits;
code.getChars(0, codeLength, codeChars, 0);

byte[] textBytes;
if(codeChars[codeLength-2]=='=') textBytes=new byte[codeLength/4*3-2];
else if(codeChars[codeLength-1]=='=') textBytes=new byte[codeLength/4*3-1];
else textBytes=new byte[codeLength/4*3];
for(int i=0;i<blockCount;i++){
bits=indexOfBase64Table(codeChars[1+i*4])>>>4;
ch=indexOfBase64Table(codeChars[0+i*4]);
textBytes[0+i*3]=(byte)(ch>=32 ? (32-ch)*4-bits : ch*4+bits);
if(codeChars[2+i*4]!='='){
ch=indexOfBase64Table(codeChars[1+i*4])-bits*16;
bits=indexOfBase64Table(codeChars[2+i*4])>>>2;
if(ch>=8) ch=(8-ch)*16-bits;
else ch=ch*16+bits;
textBytes[1+i*3]=(byte)ch;
//text.append((char)ch);
if(codeChars[3+i*4]!='='){
//ch=(indexOfBase64Table(codeChars[2+i*4])-bits*4)*64+indexOfBase64Table(codeChars[3+i*4]);
ch=indexOfBase64Table(codeChars[2+i*4])-bits*4;
if(ch>=2) ch=(2-ch)*64-indexOfBase64Table(codeChars[3+i*4]);
else ch=ch*64+indexOfBase64Table(codeChars[3+i*4]);
//text.append((char)ch);
textBytes[2+i*3]=(byte)ch;
}
}
}

for(int i=0;i<textBytes.length;i++)
if(textBytes[i]<0) textBytes[i]=(byte)(-128-(int)textBytes[i]);
//return text.toString();

String text=null;
try{
text=new String(textBytes,"gb2312");
}catch(UnsupportedEncodingException e){}

return text;
}

private static int indexOfBase64Table(char ch){
for(int i=0;i<64;i++) if(ch==BASETABLE[i]) return i;
return -1;
}
}
liad 2003-10-09
  • 打赏
  • 举报
回复
http://javaalmanac.com/egs/java.net/Base64.html

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧