Source: http://www.cnblogs.com/tuyile006/archive/2008/01/17/1043178.html
I. Encoding Rules
Base64 encoding uses 64 basic ASCII characters to re-encode the data. It splits the data to be encoded into byte arrays. Take 3 bytes as a group. Sort the 24-bit data in order and divide the 24-bit data into four groups, that is, 6-bit data in each group. Add two zeros before the highest bits in each group to make up one byte. In this way, a 3-byte data is reencoded into 4 bytes. When the number of bytes of the data to be encoded is not an integer multiple of 3, that is to say, the last group is not three bytes long. At this time, fill in 1 to 2 0 bytes in the last group. Add 1 to 2 "=" at the end after the final encoding ".
For example, base64 encoding will be performed on ABC:
1. First, take the ASCII code value corresponding to ABC. A (65) B (66) C (67 );
2. Obtain the binary value A (01000001) B (01000010) C (01000011 );
3. Connect the three bytes of binary code (010000010100001001000011 );
4. Divide the data block into four data blocks in 6 bits, and fill two 0 bits with the highest bits to form the encoded value of four bytes. (00010000) (00010100) (00001001) (00000011), where the blue part is real data;
5. Convert the four bytes into a hexadecimal number (16) (20) (9) (3 );
6. Check the corresponding ASCII code character (q) (u) (j) (d) Based on the 64 basic sequence tables given by base64 ), the value here is actually the index of the data in the orders table.
Note: base64 sequence table: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 +/
Ii. decoding rules
The decoding process is to restore 4 bytes to 3 bytes, and then reorganize the byte array into data according to different data forms.
Iii. Implementation in C #
Encoding class:
/// <Summary>
/// Base64 encoding class.
/// Convert the byte [] type into a base64 encoded string type.
/// </Summary>
Public class base64encoder
{
Byte [] source;
Int length, leng22;
Int blockcount;
Int paddingcount;
Public static base64encoder encoder = new base64encoder ();
Public base64encoder ()
{
}
Private void Init (byte [] input)
{
Source = input;
Length = input. length;
If (length % 3) = 0)
{
Paddingcount = 0;
Blockcount = length/3;
}
Else
{
Paddingcount = 3-(length % 3 );
Blockcount = (Length + paddingcount)/3;
}
Length1 = Length + paddingcount;
}
Public String getencoded (byte [] input)
{
// Initialization
Init (input );
Byte [] source2;
Source2 = new byte [lengh2];
For (INT x = 0; x <leng2; x ++)
{
If (x <length)
{
Source2 [x] = source [x];
}
Else
{
Source2 [x] = 0;
}
}
byte B1, B2, B3;
byte temp, temp1, temp2, temp3, temp4;
byte [] buffer = new byte [blockcount * 4];
char [] result = new char [blockcount * 4];
for (INT x = 0; x {< br> b1 = source2 [x * 3];
b2 = source2 [x * 3 + 1];
B3 = source2 [x * 3 + 2];
Temp1 = (byte) (B1 & 252)> 2 );
Temp = (byte) (B1 & 3) <4 );
Temp2 = (byte) (B2 & 240)> 4 );
Temp2 + = temp;
Temp = (byte) (B2 & 15) <2 );
Temp3 = (byte) (B3 & 192)> 6 );
Temp3 + = temp;
Temp4 = (byte) (B3 & 63 );
Buffer [x * 4] = temp1;
Buffer [x * 4 + 1] = temp2;
Buffer [x * 4 + 2] = temp3;
Buffer [x * 4 + 3] = temp4;
}
For (INT x = 0; x <blockcount * 4; X ++)
{
Result [x] = sixbit2char (buffer [x]);
}
Switch (paddingcount)
{
Case 0: break;
Case 1: result [blockcount * 4-1] = '; break;
Case 2: result [blockcount * 4-1] = ';
Result [blockcount * 4-2] = ';
Break;
Default: break;
}
Return new string (result );
}
Private char sixbit2char (byte B)
{
Char [] lookuptable = new char [64] {
'A', 'B', 'C', 'D', 'E', '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', '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 ', '+ ','/'};
If (B> = 0) & (B <= 63 ))
{
Return lookuptable [(INT) B];
}
Else
{
Return '';
}
}
}
Decoding class:
/// <Summary>
/// Base64 decoding class
/// Convert the string type encoded in base64 to the byte [] Type
/// </Summary>
Public class base64decoder
{
Char [] source;
Int length, leng2, length3;
Int blockcount;
Int paddingcount;
Public static base64decoder decoder = new base64decoder ();
Public base64decoder ()
{
}
Private void Init (char [] input)
{
Int temp = 0;
Source = input;
Length = input. length;
For (INT x = 0; x <2; X ++)
{
If (input [length-X-1] = ')
Temp ++;
}
Paddingcount = temp;
Blockcount = length/4;
Lengh2 = blockcount * 3;
}
Public byte [] getdecoded (string strinput)
{
// Initialization
Init (strinput. tochararray ());
Byte [] buffer = new byte [length];
Byte [] buffer2 = new byte [lengh2];
For (INT x = 0; x <length; X ++)
{
Buffer [x] = char2sixbit (source [x]);
}
Byte B, B1, B2, B3;
Byte temp1, temp2, temp3, temp4;
For (INT x = 0; x <blockcount; X ++)
{
Temp1 = buffer [x * 4];
Temp2 = buffer [x * 4 + 1];
Temp3 = buffer [x * 4 + 2];
Temp4 = buffer [x * 4 + 3];
B = (byte) (temp1 <2 );
B1 = (byte) (temp2 & 48)> 4 );
B1 + = B;
B = (byte) (temp2 & 15) <4 );
B2 = (byte) (temp3 & 60)> 2 );
B2 + = B;
B = (byte) (temp3 & 3) <6 );
B3 = temp4;
B3 + = B;
Buffer2 [x * 3] = b1;
Buffer2 [x * 3 + 1] = B2;
Buffer2 [x * 3 + 2] = B3;
}
Length3 = leng22-paddingcount;
Byte [] result = new byte [length3];
For (INT x = 0; x <length3; X ++)
{
Result [x] = buffer2 [x];
}
Return result;
}
Private byte char2sixbit (char C)
{
Char [] lookuptable = new char [64] {
'A', 'B', 'C', 'D', 'E', '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', '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 ', '+ ','/'};
If (C = ')
Return 0;
Else
{
For (INT x = 0; x <64; X ++)
{
If (lookuptable [x] = C)
Return (byte) X;
}
Return 0;
}
}
}
// The decoding class ends.
Tip:
The aboveCodeIt only describes the base64 encoding principles for rewriting in more languages. However, in. net, you can use a simpler method:
Encoding:
Byte [] bytes = encoding. Default. getbytes ("string to be converted ");
Convert. tobase64string (bytes );
Decoding:
// "Ztkwrstj" is the base64 encoding of "I love you"
Byte [] outputb = convert. frombase64string ("ztkwrstj ");
String orgstr = encoding. Default. getstring (outputb );
Complete!