Interface Display:Encryption:
Decrypt:
Code implementation:public string encryptstring (String str)
{
#regionEncryption Program
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 ( 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 ();
#endregion
}
private void Btnencrypt_click (object sender, EventArgs e)
{
#regionEncrypt button
Try
{
Txtmiwen.text = encryptstring (Txtmingwen.text);
}
Catch
{
MessageBox.Show ("Clear text (ciphertext) is wrong!" ", System.Windows.Forms.Application.ProductName);
Txtmiwen.text = "";
Return
}
#endregion
}
public string decryptstring (String str)
{
#regionDecryption Program
if (str. Length% 4)! = 0)
{
throw new ArgumentException ("Not the correct BASE64 code, 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);
#endregion
}
private void Btndecrypt_click (object sender, EventArgs e)
{
#regionDecrypt Button
Try
{
Txtmiwen.text = decryptstring (Txtmingwen.text);
}
Catch
{
MessageBox.Show ("Clear text (ciphertext) is wrong", System.Windows.Forms.Application.ProductName);
Txtmiwen.text = "";
Return
}
#endregion
}