单表密码的C#实现

具体实施

Form.cs代码(界面部分代码)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.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{

        private char[] chngTable = new char[26];

        public string Decryption(string source){

            string result;

            ChangeTable( Table, chngTable );

            result = DealWith(source, chngTable);

            return result;

        }

        public void 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
{
    partial class 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 = new System.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 += new System.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 += new System.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;
    }
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值