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 ());
}
#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 < 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 +] = 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);
}
Return Changetobase64data (Bslast);
}
<summary>
Decoding
</summary>
<param name= "Sbase64data" > Encoded base64 Format 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);
}
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>
Converts the transformed byte array into the corresponding base64 table data
</summary>
<param name= "byt" > byte array to convert </param>
<returns> after the final coding of the 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>
The encoded data is converted 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 data
<summary>
Decimal to binary data (incoming data can only be an integer less 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 ';
}
}
<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 ());
}
}