单表密码的C#实现

来源:互联网 发布:淘宝网 服务中心 编辑:程序博客网 时间:2024/05/29 13:30
具体实施

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();
           this.label1 = new System.Windows.Forms.Label();
           this.label2 = new System.Windows.Forms.Label();
           this.SuspendLayout();
           //
           // textBox1
           //
           this.textBox1.Font = new System.Drawing.Font("宋体", 14.25F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point, ((byte)(134)));
           this.textBox1.Location = new System.Drawing.Point(22, 46);
           this.textBox1.Multiline = true;
           this.textBox1.Name = "textBox1";
           this.textBox1.Size = new System.Drawing.Size(375, 101);
           this.textBox1.TabIndex = 0;
           //
           // textBox2
           //
           this.textBox2.Font = new System.Drawing.Font("宋体", 14.25F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point, ((byte)(134)));
           this.textBox2.Location = new System.Drawing.Point(22, 187);
           this.textBox2.Multiline = true;
           this.textBox2.Name = "textBox2";
           this.textBox2.Size = new System.Drawing.Size(375, 111);
           this.textBox2.TabIndex = 1;
           //
           // button1
           //
           this.button1.Location = new System.Drawing.Point(240, 309);
           this.button1.Name = "button1";
           this.button1.Size = new System.Drawing.Size(79, 25);
           this.button1.TabIndex = 2;
           this.button1.Text = "加密[S]";
           this.button1.UseVisualStyleBackColor = true;
           this.button1.Click += newSystem.EventHandler(this.button1_Click);
           //
           // button2
           //
           this.button2.Location = new System.Drawing.Point(346, 309);
           this.button2.Name = "button2";
           this.button2.Size = new System.Drawing.Size(79, 25);
           this.button2.TabIndex = 3;
           this.button2.Text = "脱密[C]";
           this.button2.UseVisualStyleBackColor = true;
           this.button2.Click += newSystem.EventHandler(this.button2_Click);
           //
           // label1
           //
           this.label1.AutoSize = true;
           this.label1.Location = new System.Drawing.Point(17, 17);
           this.label1.Name = "label1";
           this.label1.Size = new System.Drawing.Size(59, 12);
           this.label1.TabIndex = 4;
           this.label1.Text = "输入明文:";
           //
           // label2
           //
           this.label2.AutoSize = true;
           this.label2.Location = new System.Drawing.Point(24, 164);
           this.label2.Name = "label2";
           this.label2.Size = new System.Drawing.Size(59, 12);
           this.label2.TabIndex = 5;
           this.label2.Text = "输出密文:";
           //
           // Form1
           //
           this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
           this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
           this.ClientSize = new System.Drawing.Size(447, 346);
           this.Controls.Add(this.label2);
           this.Controls.Add(this.label1);
           this.Controls.Add(this.button2);
           this.Controls.Add(this.button1);
           this.Controls.Add(this.textBox2);
           this.Controls.Add(this.textBox1);
           this.Icon =((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
           this.MaximizeBox = false;
           this.MaximumSize = new System.Drawing.Size(463, 384);
           this.MinimumSize = new System.Drawing.Size(463, 384);
           this.Name = "Form1";
           this.StartPosition =System.Windows.Forms.FormStartPosition.CenterScreen;
           this.Text = "单表加密";
           this.ResumeLayout(false);
           this.PerformLayout();

       }

       #endregion

       private System.Windows.Forms.TextBox textBox1;
       private System.Windows.Forms.TextBox textBox2;
       private System.Windows.Forms.Button button1;
       private System.Windows.Forms.Button button2;
       private System.Windows.Forms.Label label1;
       private System.Windows.Forms.Label label2;
    }
}


原创粉丝点击