asp.net Base64 coding and decoding program

Source: Internet
Author: User
Tags base64 decimal to binary
I. Coding rules for BASE64
The idea of base64 encoding is to use 64 basic ASCII characters (not ASCII code from 1-64-yard data, which is selected from the code). ) to encode the data again . 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: Base64 encoding of ABC will be performed:

1, first take the corresponding ASCII value of ABC. (a) B (c) (67);
2, then take the binary value A (01000001) b (01000010) c (01000011);
3, then the three bytes of the binary code to connect (010000010100001001000011);
4, then 6-bit units into 4 blocks, and at the highest level of two 0 after the formation of 4 bytes encoded values, (00010000) (00010100) (00001001) (00000011), where the blue part of the real data;
5. Convert these four bytes of data into 10 (16) (20) (9) (3);
6. Finally, according to the 64 basic character tables given by Base64, find the corresponding ASCII code character (q) (U) (j) (d), where the value is actually the index of the data in the character table.
Note: Base64 character chart: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/
Two. 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.
Three. Implementation in C # (2011/03/14)
public class Base64manager
{
<summary>
Coding
</summary>
<param name= "Sstr" > string to encode </param>
<returns> encoded base64 format of the string </returns>
public string Encoder (string sstr)
{
#region get an array of bytes that can be divisible by 3.
byte[] bs = system.text.encoding.utf8.getbytes (Sstr.tochararray ());


if (bs.length% 3 = 0)


{


}


else if (bs.length%3==1)


{


BS = System.text.encoding.utf8.getbytes ((sstr+ "= ="). ToCharArray ());


}


else if (bs.length%3==2)


{


BS = System.text.encoding.utf8.getbytes ((sstr+ "="). ToCharArray ());


}
#endregion
#region Start Base64 encoding
byte[] Bslast = new BYTE[BS.LENGTH/3 * 4];
Char[] C1;


char[] C2;


Char[] C3;


char[] C4 = new Char[8] {' 0 ', ' 0 ', ', ', ', ', ', ', ', ' ', ', ', '};


char[] C5 = new Char[8] {' 0 ', ' 0 ', ', ', ', ', ', ', ', ' ', ', ', '};


char[] C6 = new Char[8] {' 0 ', ' 0 ', ', ', ', ', ', ', ', ' ', ', ', '};


Char[] C7 = new Char[8] {' 0 ', ' 0 ', ', ', ', ', ', ', ', ' ', ', ', '};


Char[] C8=new char[24];


for (int i = 0; i &lt; bs.length i = i + 3)


{


C1 = Change (Bs[i]);


C2 = Change (bs[i + 1]);


C3 = Change (Bs[i + 2]);


for (int j = 0; J &lt; 8, J + +)


{


C8[J] = C1[j];


C8[j + 8] = C2[j];


C8[j +] = c3[j];


}


for (int k = 0; k &lt; 6; k++)


{


C4[k + 2] = c8[k];


C5[k + 2] = c8[k + 6];


C6[k + 2] = c8[k + 12];


C7[k + 2] = c8[k + 18];


}
BSLAST[I/3 * 4 + 0] = (byte) change2to10 (C4);
BSLAST[I/3 * 4 + 1] = (byte) change2to10 (C5);
BSLAST[I/3 * 4 + 2] = (byte) change2to10 (C6);
BSLAST[I/3 * 4 + 3] = (byte) change2to10 (C7);
}
#endregion
Return Changetobase64data (Bslast);
}
&lt;summary&gt;


Decoding


&lt;/summary&gt;


&lt;param name= "Sbase64data" &gt; Encoded base64 Format data &lt;/param&gt;


&lt;returns&gt; Decoded Data &lt;/returns&gt;


public string Decoder (string sbase64data)


{


Byte[] B = changetobytedata (sbase64data);


Char[] C = New CHAR[B.LENGTH/4 * 3 * 8];


int istate = 0;


foreach (byte bb in b)


{


char[] cc = Change (BB);


for (int i = 0; i &lt; 6; i++)


{


c[istate++] = cc[i + 2];


}


}
byte[] bl = new BYTE[B.LENGTH/4 * 3];


int istateother = 0;


for (int i = 0; i &lt; C.LENGTH/8; i++)


{


Char[] cs = new Char[8] {', ', ', ', ', ', ', ', ', ', ', ', ', '};


for (int j = 0; J &lt; 8, J + +)


{


CS[J] = C[i * 8 + j];


}


bl[istateother++] = (byte) change2to10 (CS);


}
#region
            int icount = 0;
            foreach (byte bbb in BL)
             {
                 if (bbb = (byte) 0)
                 {
                     icount++;
               }
           }
byte[] BLL = new Byte[bl.length-icount];
for (int i = 0; i < bll.length; i++)
{
Bll[i] = Bl[i];
}
#endregion
Return system.text.encoding.utf8.getstring (BLL);
}
&lt;summary&gt;


Converts the transformed byte array into the corresponding base64 table data


&lt;/summary&gt;


&lt;param name= "byt" &gt; byte array to convert &lt;/param&gt;


&lt;returns&gt; after the final coding of the data &lt;/returns&gt;


private string Changetobase64data (byte[] byt)


{


char[] sbase64table = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/". ToCharArray ();


String sreturn = "";


foreach (Byte b in byt)


{


Sreturn + + sbase64table[(int32) b];


}


return sreturn;


}
&lt;summary&gt;


The encoded data is converted to a byte array


&lt;/summary&gt;


&lt;param name= "Sbase64data" &gt; Base64 encoded Data &lt;/param&gt;


&lt;returns&gt; encoded byte array &lt;/returns&gt;


Private byte[] Changetobytedata (string sbase64data)


{


String sbase64table = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";


char[] Creturn = new Char[sbase64data.length];


int istate = 0;


foreach (char c in sbase64data)


{


creturn[istate++] = (char) ((Sbase64table.indexof (c) = = 1)? System.text.encoding.utf8.getchars (New byte[] {((byte) 0)} ) [0]: Sbase64table.indexof (c));


}


Return System.text.encoding.utf8.getbytes (Creturn);


}
#region Decimal to binary <256 data
&lt;summary&gt;


Decimal to binary data (incoming data can only be an integer less than 128)


&lt;/summary&gt;


&lt;param name= "i10" &gt; Decimal Data &lt;/param&gt;


&lt;returns&gt; converted binary Array &lt;/returns&gt;


Private char[] Change (int i10)


{


BOOL state = FALSE;


char[] ch = new char[] {' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 ', ' 0 '};


int istate = 0;


while (i10!= 0)


{


istate++;


state = false;


if (i10% 2 = 1)


{


i10 = I10/2;


state = true;


}


Else


{


i10 = I10/2;


}
                if (state)
                {
                     Ch[8- Istate] = ' 1 ';
               }
                Else
                 {
                     Ch[8-istate] = ' 0 ';
               }
           }
return ch;
}
<summary>
Converts a binary eight-bit char array to decimal data
</summary>
<param name= "ch" > Binary Data </param>
<returns> decimal Number </returns>
private int change2to10 (char[] ch)
{
int ireturn = 0;
            for (int i = 0; i < 8; i++)
   ;          {
                 if (ch[i] = = ' 1 ')
                 {
                     Double D = double.parse ((7-i). ToString ());
                     Ireturn + + (Int32) System.math.pow (2.0, Double.Parse (7-i). ToString ());
               }
           }
return ireturn;
}
#endregion
}
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.