BASE64 Coding Rules and C # implementation

Source: Internet
Author: User

I. Coding rules
The idea of BASE64 encoding is to re-encode the data using 64 basic ASCII characters. It splits the data that needs to be encoded into a byte array. A group of 3 bytes. The 24-bit data is sorted sequentially, and the 24 bits of data are divided into 4 groups, 6 bits per group. Then fill in a byte with two 0 in front of the highest bit of each group. This re-encodes a 3-byte set of data into 4 bytes. When the number of bytes of data to encode is not an integral multiple of 3, that is, the last group is less than 3 bytes at the time of grouping. At this point the last group is populated with 1 to 2 0 bytes. and add 1 to 2 "=" At the end after the last encoding is complete.
Example: ABC will be BASE64 encoded
First, the ASCII value corresponding to the ABC code is taken. A (+) B (+) C (67).
Then take the binary value A (01000001) B (01000010) C (01000011) and then connect the three bytes of binary code (010000010100001001000011). The encoded value (4) (00010000) (00010100) (00001001) of 00000011 bytes is then divided into 4 data blocks in 6-bit units and the highest bits are populated with two 0. The blue part is the real data. The four bytes of data are then converted to 10 binary numbers (16) (20) (19) (3). Finally, the corresponding ASCII character (Q) (U) (J) (D) is detected according to the 64 basic character BASE64. The value here is actually the index of the data in the character table.
Note BASE64 character: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/
Second, decoding rules
The decoding process is to restore 4 bytes to 3 bytes and then rearrange the byte arrays into data according to different data forms.
Third, the implementation of 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,0,1,2,3,4,5,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 ("Not the correct BASE64 code, 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]&LT;&LT;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);
}
}

}

BASE64 Coding Rules and C # implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.