具体实施Form.cs代码(界面部分代码)
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
usingSystem.Windows.Forms;
namespace SingleTable
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Encry encry = new Encry();
textBox2.Text = encry.Encryption(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
Decry decry = new Decry();
textBox2.Text = decry.Decryption(textBox1.Text);
}
}
}
Deal.cs逻辑处理部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SingleTable
{
public class Deal
{
public char[] Table = { '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' };
public string DealWith(string source, char[] newTable)
{
string ciphertext = "";
int index = 0;
for (int i = 0; i < source.Length; i++){
char ch = char.Parse(source.Substring(i, 1));
if (ch == 13){
ciphertext += Convert.ToString(ch);
i++;
ciphertext += char.Parse(source.Substring(i, 1));
continue;
}
else if (ch == 32) {
ciphertext += Convert.ToString(ch);
continue;
}
else {
index = ch - 'a';
ciphertext += Convert.ToString(newTable[index]);
}
} //end_for(int i = 0)
return ciphertext;
}
}
public class Encry : Deal{
public string Encryption(string source) {
string result = DealWith(source, Table);
return result;
}
}//end_Class Encry
public class Decry : Deal{
privatechar[] chngTable = new char[26];
public string Decryption(string source){
string result;
ChangeTable( Table, chngTable );
result = DealWith(source, chngTable);
return result;
}
publicvoid ChangeTable( char[] Table, char[] chngTable ) {
int oldindex = 0;
int newindex = 0;
for (; oldindex < 26; oldindex++) {
char ch = Table[oldindex];
newindex = ch - 'a';
ch = Convert.ToChar('a' + oldindex);
chngTable[newindex] = ch;
}
}//end_ChangeTable()
}//end_Class Decry
}
附带界面设计的代码:
namespace SingleTable
{
partialclass Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components !=null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = newSystem.ComponentModel.ComponentResourceManager(typeof(Form1));
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();