Base64

 1public class Base64Decoder
 2    {
 3        char[] source;
 4        int length, length2, length3;
 5        int blockCount;
 6        int paddingCount;
 7        public Base64Decoder(char[] input)
 8        {
 9            int temp=0;
10            source=input;
11            length=input.Length;
12
13            //find how many padding are there
14            for (int x=0;x<2;x++)
15            {
16                if(input[length-x-1]=='=')
17                    temp++;
18            }

19            paddingCount=temp;
20            //calculate the blockCount;
21            //assuming all whitespace and carriage returns/newline were removed.
22            blockCount=length/4;
23            length2=blockCount*3;
24        }

25
26        public byte[] GetDecoded()
27        {
28            byte[] buffer=new byte[length];//first conversion result
29            byte[] buffer2=new byte[length2];//decoded array with padding
30
31            for(int x=0;x<length;x++)
32            {
33                buffer[x]=char2sixbit(source[x]);
34            }

35
36            byte b, b1,b2,b3;
37            byte temp1, temp2, temp3, temp4;
38
39            for(int x=0;x<blockCount;x++)
40            {
41                temp1=buffer[x*4];
42                temp2=buffer[x*4+1];
43                temp3=buffer[x*4+2];
44                temp4=buffer[x*4+3];                
45
46                b=(byte)(temp1<<2);
47                b1=(byte)((temp2 & 48)>>4);
48                b1+=b;
49
50                b=(byte)((temp2 & 15)<<4);
51                b2=(byte)((temp3 & 60)>>2);
52                b2+=b;
53
54                b=(byte)((temp3 & 3)<<6);
55                b3=temp4;
56                b3+=b;
57
58                buffer2[x*3]=b1;
59                buffer2[x*3+1]=b2;
60                buffer2[x*3+2]=b3;
61            }

62            //remove paddings
63            length3=length2-paddingCount;
64            byte[] result=new byte[length3];
65
66            for(int x=0;x<length3;x++)
67            {
68                result[x]=buffer2[x];
69            }

70
71            return result;
72        }

73
74        private byte char2sixbit(char c)
75        {
76            char[] lookupTable=new char[64]
77                    {    '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',
78                        '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',
79                        '0','1','2','3','4','5','6','7','8','9','+','/'}
;
80            if(c=='=')
81                return 0;
82            else
83            {
84                for (int x=0;x<64;x++)
85                {
86                    if (lookupTable[x]==c)
87                        return (byte)x;
88                }

89                //should not reach here
90                return 0;
91            }

92
93        }

94
95    }

96
  1public class Base64Encoder
  2    {
  3        byte[] source;
  4        int length,length2;
  5        int blockCount;
  6        int paddingCount;
  7        public Base64Encoder(byte[] input)
  8        {
  9            source=input;
 10            length=input.Length;
 11            if((length % 3)==0)
 12            {
 13                paddingCount=0;
 14                blockCount=length/3;
 15            }

 16            else
 17            {
 18                paddingCount=3-(length % 3);//need to add padding
 19                blockCount=(length+paddingCount) / 3;
 20            }

 21            length2=length+paddingCount;//or blockCount *3
 22        }

 23
 24        public char[] GetEncoded()
 25        {
 26            byte[] source2;
 27            source2=new byte[length2];
 28            //copy data over insert padding
 29            for (int x=0; x<length2;x++)
 30            {
 31                if (x<length)
 32                {
 33                    source2[x]=source[x];
 34                }

 35                else
 36                {
 37                    source2[x]=0;
 38                }

 39            }

 40            
 41            byte b1, b2, b3;
 42            byte temp, temp1, temp2, temp3, temp4;
 43            byte[] buffer=new byte[blockCount*4];
 44            char[] result=new char[blockCount*4];
 45            for (int x=0;x<blockCount;x++)
 46            {
 47                b1=source2[x*3];
 48                b2=source2[x*3+1];
 49                b3=source2[x*3+2];
 50
 51                temp1=(byte)((b1 & 252)>>2);//first
 52
 53                temp=(byte)((b1 & 3)<<4);
 54                temp2=(byte)((b2 & 240)>>4);
 55                temp2+=temp; //second
 56
 57                temp=(byte)((b2 & 15)<<2);
 58                temp3=(byte)((b3 & 192)>>6);
 59                temp3+=temp; //third
 60
 61                temp4=(byte)(b3 & 63); //fourth
 62
 63                buffer[x*4]=temp1;
 64                buffer[x*4+1]=temp2;
 65                buffer[x*4+2]=temp3;
 66                buffer[x*4+3]=temp4;
 67
 68            }

 69
 70            for (int x=0; x<blockCount*4;x++)
 71            {
 72                result[x]=sixbit2char(buffer[x]);
 73            }

 74
 75            //covert last "A"s to "=", based on paddingCount
 76            switch (paddingCount)
 77            {
 78                case 0:break;
 79                case 1:result[blockCount*4-1]='=';break;
 80                case 2:result[blockCount*4-1]='=';
 81                    result[blockCount*4-2]='=';
 82                    break;
 83                default:break;
 84            }

 85            return result;
 86        }

 87
 88        private char sixbit2char(byte b)
 89        {
 90            char[] lookupTable=new char[64]
 91                    {    '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',
 92                        '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',
 93                        '0','1','2','3','4','5','6','7','8','9','+','/'}
;
 94
 95            if((b>=0&&(b<=63))
 96            {
 97                return lookupTable[(int)b];
 98            }

 99            else
100            {
101                //should not happen;
102                return ' ';
103            }

104        }

105    }

106
最好是在encode前和decode后都用utf8转换一下,中文就没问题了
posted @ 2005-07-29 10:00  Vincent  阅读(835)  评论(0编辑  收藏  举报