BASE64 Coding Rules and C # implementation

Source: Internet
Author: User
Tags base64 empty
Coding

I. Coding rules
The idea of BASE64 encoding is to 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. Arrange the 24-bit data in order, then divide the 24-bit data into 4 groups, that is, 6 bits per group. Then fill a byte with two 0 in front of each group's top digit. This encodes a 3-byte-A-group of data back into 4 bytes. When the number of bytes of data to be encoded is not an integral multiple of 3, that is, the last group is not 3 bytes when grouped. This is populated with 1 to 2 0 bytes in the last group. and add 1 to 2 "=" At the end after the final encoding is complete.
Example: The BASE64 encoding of ABC will be
First, take the ASCII value corresponding to ABC. (A) B (A) C (67).
Then take the binary value A (01000001) B (01000010) C (01000011), and then connect the binary code of the three bytes (010000010100001001000011), The 6-bit unit is divided into 4 blocks of data and a 4 byte encoded value (00010000) (00010100) (00001001) (00000011) is formed after the highest bit fills two 0. The blue part is real data. The four-byte data is then converted to 10-digit (16) (20) (19) (3). Finally, according to the 64 basic character chart given by BASE64, the corresponding ASCII character (Q) (U) (J) (D) is detected. The value here is actually the index of the data in the character table.
Note BASE64 character tabulation: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/
Second, decoding rules
The decoding process is to restore 4 bytes to 3 bytes and then rearrange the byte array into data according to different data forms.
Third, the 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 ', ' 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 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]&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);
}
}

}




Related Article

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.