/// Encryption
/// <Summary>
/// Encryption
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public static string encryptstring (string Str)
{
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', '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 (STR ));
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 ();
}
/// Decrypt
/// <Summary>
/// Decrypt
/// </Summary>
/// <Param name = "str"> </param>
/// <Returns> </returns>
Public static string decryptstring (string Str)
{
If (Str. Length % 4 )! = 0)
{
Throw new argumentexception ("Incorrect base64 encoding. Please check. "," Str ");
}
If (! System. Text. regularexpressions. RegEx. ismatch (STR, "^ [A-Z0-9/+ =] * $", system. Text. regularexpressions. regexoptions. ignorecase ))
{
Throw new argumentexception ("contains incorrect base64 encoding. Please check. "," Str ");
}
String base64code = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 +/= ";
Int page = Str. Length/4;
System. Collections. arraylist outmessage = new system. Collections. arraylist (page * 3 );
Char [] Message = Str. 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] <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 );
}