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.
First, take the ASCII code value corresponding to ABC. A (65) B (66) C (67 ).
Take the binary value A (01000001) B (01000010) C (01000011), and then connect the three bytes of binary code (010000010100001001000011 ), then, the data block is divided into 4 data blocks in 6 bits, and the encoded value (00010000) (00010100) (00001001) is formed after the maximum bit is filled with two zeros ). The blue part is the actual data. Then, convert the four bytes into a decimal number (16) (20) (19) (3 ). Finally, we can find out 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 prepare 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 Code:
Using System;
Using System. IO;
Using System. Data;
Namespace Base64 {
Internal class Base64 {
Public static string Base64Code (string Message)
{
Char [] Base64Code = new char [] {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, 6, 7,
8, 9, +,/, = };
Byte empty = (byte) 0;
System. Collections. ArrayList byteMessage = new System. Collections. ArrayList (System. Text. Encoding. Default. GetBytes (Message ));
System. Text. StringBuilder outmessage;
Int messageLen = byteMessage. Count;
Int page = messageLen/3;
Int use = 0;
If (use = messageLen % 3)> 0)
{
For (int I = 0; I <3-use; I ++)
ByteMessage. Add (empty );
Page ++;
}
Outmessage = new System. Text. StringBuilder (page * 4 );
For (int I = 0; I <page; I ++)
{
Byte [] instr = new byte [3];
Instr [0] = (byte) byteMessage [I * 3];
Instr [1] = (byte) byteMessage [I * 3 + 1];
Instr [2] = (byte) byteMessage [I * 3 + 2];
Int [] outstr = new int [4];
Outstr [0] = instr [0]> 2;
Outstr [1] = (instr [0] & 0x03) <4) ^ (instr [1]> 4 );
If (! Instr [1]. Equals (empty ))
Outstr [2] = (instr [1] & 0x0f) <2) ^ (instr [2]> 6 );
Else
Outstr [2] = 64;
If (! Instr [2]. Equals (empty ))
Outstr [3] = (instr [2] & 0x3f );
Else
Outstr [3] = 64;
Outmessage. Append (Base64Code [outstr [0]);
Outmessage. Append (Base64Code [outstr [1]);
Outmessage. Append (Base64Code [outstr [2]);
Outmessage. Append (Base64Code [outstr [3]);
}
Return outmessage. ToString ();
}
Public static string Base64Decode (string Message ){
If (Message. Length % 4 )! = 0 ){
Throw new ArgumentException ("Incorrect BASE64 encoding. Please check. "," Message ");
}
If (! System. Text. RegularExpressions. Regex. IsMatch (Message, "^ [A-Z0-9/+ =] * $", System. Text. RegularExpressions. RegexOptions. IgnoreCase )){
Throw new ArgumentException ("contains incorrect BASE64 encoding. Please check. "," Message ");
}
String Base64Code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 +/= ";
Int page = Message. Length/4;
System. Collections. ArrayList outMessage = new System. Collections. ArrayList (page * 3 );
Char [] message = Message. ToCharArray ();
For (int I = 0; I <page; I ++)
{
Byte [] instr = new byte [4];
Instr [0] = (byte) Base64Code. IndexOf (message [I * 4]);
Instr [1] = (byte) Base64Code. IndexOf (message [I * 4 + 1]);
Instr [2] = (byte) Base64Code. IndexOf (message [I * 4 + 2]);
Instr [3] = (byte) Base64Code. IndexOf (message [I * 4 + 3]);
Byte [] outstr = new byte [3];
Outstr [0] = (byte) (instr [0] <2) ^ (instr [1] & 0x30)> 4 ));
If (instr [2]! = 64)
{
Outstr [1] = (byte) (instr [1] <4) ^ (instr [2] & 0x3c)> 2 ));
}
Else
{
Outstr [2] = 0;
}
If (instr [3]! = 64)
{
Outstr [2] = (byte) (instr [2] <6) ^ instr [3]);
}
Else
{
Outstr [2] = 0;
}
OutMessage. Add (outstr [0]);
If (outstr [1]! = 0)
OutMessage. Add (outstr [1]);
If (outstr [2]! = 0)
OutMessage. Add (outstr [2]);
}
Byte [] outbyte = (byte []) outMessage. ToArray (Type. GetType ("System. Byte "));
Return System. Text. Encoding. Default. GetString (outbyte );
}
}
}
Author Blog: http://blog.csdn.net/fengyuyan/