Asp.net Base64 encoding and decoding program

Source: Internet
Author: User

I. base64 encoding rulesBase64 encoding uses 64 basic ascii characters (instead of 1-64 ascii data .) PerformRecode. 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. After the final encoding is completedAdd 1 to 2 "=" at the end ".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 character table: abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 +/ Ii. decoding rulesThe decoding process is to restore 4 bytes to 3 bytes, and then reorganize the byte array into data according to different data forms. Implementation in c)
Public class base64manager
{
/// <Summary>
/// Encoding
/// </Summary>
/// <Param name = "sstr"> string to be encoded </param>
/// <Returns> encoded base64 string </returns>
Public string encoder (string sstr)
{
# Region get the byte array byte [] bs = system. text. encoding. utf8.getbytes (sstr. tochararray () that can be divisible by 3 ());
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 starts 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 <bs. length; I = I + 3)
{
C1 = change (bs [I]);
C2 = change (bs [I + 1]);
C3 = change (bs [I + 2]);
For (int j = 0; j <8; j ++)
{
C8 [j] = c1 [j];
C8 [j + 8] = c2 [j];
C8 [j + 16] = c3 [j];
}
For (int k = 0; k <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 );
} // <Summary>
/// Decoding
/// </Summary>
/// <Param name = "sbase64data"> encoded base64 data </param>
/// <Returns> decoded data </returns>
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 <6; I ++)
{
C [istate ++] = cc [I + 2];
}
} Byte [] bl = new byte [B. length/4*3];
Int istateother = 0;
For (int I = 0; I <c. length/8; I ++)
{
Char [] cs = new char [8] {'',''};
For (int j = 0; j <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 );
} // <Summary>
/// Convert the converted byte array to the data in the corresponding base64 table
/// </Summary>
/// <Param name = "byt"> byte array to be converted </param>
/// <Returns> final encoded data </returns>
Private string changetobase64data (byte [] byt)
{
Char [] sbase64table = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 +/". tochararray ();
String sreturn = "";
Foreach (byte B in byt)
{
Sreturn + = sbase64table [(int32) B];
}
Return sreturn;
} // <Summary>
/// Convert the encoded data to a byte array
/// </Summary>
/// <Param name = "sbase64data"> base64-encoded data </param>
/// <Returns> encoded byte array </returns>
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 of Data // <summary>
/// Convert decimal to binary data (the input data can only be an integer smaller than 128)
/// </Summary>
/// <Param name = "i10"> decimal data </param>
/// <Returns> converted binary array </returns>
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>
/// Convert the binary eight-bit char array into 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
}

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.