⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form5.cs

📁 C#平台下的希尔密码加密解密及破译程序
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 加密解密程序
{
    public partial class Form5 : Form
    {
        String text1;
        String text2;
        String text3;
        String text4;
        public int[,] P = new int[2, 2] { { 0, 0 }, { 0, 0 } };
        public int[,] Q = new int[2, 2] { { 0, 0 }, { 0, 0 } };
        public int[,] QQ = new int[2, 2] { { 0, 0 }, { 0, 0 } };
        public int[,] key = new int[2, 2] { { 0, 0 }, { 0, 0 } };
        public int[,] XL = new int[2, 1] { { 0 }, { 0 } };
        public Form5()
        {
            InitializeComponent();
        }

        private void Form5_Closed(object sender, System.EventArgs e)
        {
            Form1.cFormCreated = false;
            Program.form1.Activate();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool sign = true;
            text1 = textBox1.Text;
            text2 = textBox2.Text;
            text3 = textBox3.Text;
            text4 = textBox4.Text;
            int length1 = text1.Length;
            int length2 = text2.Length;
            int length = text3.Length;
            if (length1 != 4 && length2 != 4)
            {
                MessageBox.Show("请输入已破译字母4个");
                sign = false;
            }
            else
            {
                Q[0, 0] = turnCToI(text1[0]);       //密文部分字母
                Q[1, 0] = turnCToI(text1[1]);
                Q[0, 1] = turnCToI(text1[2]);
                Q[1, 1] = turnCToI(text1[3]);

                P[0, 0] = turnCToI(text2[0]);       //明文部分字母
                P[1, 0] = turnCToI(text2[1]);
                P[0, 1] = turnCToI(text2[2]);
                P[1, 1] = turnCToI(text2[3]);

                if (P[0, 0] == -1 || P[1, 0] == -1 || P[1, 1] == -1 || P[0, 1] == -1)
                {
                    MessageBox.Show("只能输入大写字母");
                    sign = false;
                }

                if (Q[0, 0] == -1 || Q[1, 0] == -1 || Q[1, 1] == -1 || Q[0, 1] == -1)
                {
                    MessageBox.Show("只能输入大写字母");
                    sign = false;
                }

                int temp = (Q[0, 0] * Q[1, 1] - Q[0, 1] * Q[1, 0]) % 26;
                if (temp == 1)
                    temp = 1;
                else if (temp == 3)
                    temp = 9;
                else if (temp == 5)
                    temp = 21;
                else if (temp == 7)
                    temp = 15;
                else if (temp == 9)
                    temp = 3;
                else if (temp == 11)
                    temp = 19;
                else if (temp == 15)
                    temp = 7;
                else if (temp == 17)
                    temp = 23;
                else if (temp == 19)
                    temp = 11;
                else if (temp == 21)
                    temp = 5;
                else if (temp == 23)
                    temp = 17;
                else if (temp == 25)
                    temp = 25;
                else
                {
                    MessageBox.Show("所选已破译字母非线性无关,无法破译");
                    sign = false;
                }
                QQ[0, 0] = (Q[1, 1] * temp) % 26;
                QQ[0, 1] = ((-1) * Q[0, 1] * temp) % 26;
                QQ[1, 0] = ((-1) * Q[1, 0] * temp) % 26;
                QQ[1, 1] = (Q[0, 0] * temp) % 26;

                if (QQ[0, 0] < 0)
                    QQ[0, 0] += 26;
                if (QQ[1, 0] < 0)
                    QQ[1, 0] += 26;
                if (QQ[0, 1] < 0)
                    QQ[0, 1] += 26;
                if (QQ[1, 1] < 0)
                    QQ[1, 1] += 26;
                
                key[0, 0] = P[0, 0] * QQ[0, 0] + P[0, 1] * QQ[1, 0];
                key[1, 0] = P[1, 0] * QQ[0, 0] + P[1, 1] * QQ[1, 0];
                key[0, 1] = P[0, 0] * QQ[0, 1] + P[0, 1] * QQ[1, 1];
                key[1, 1] = P[1, 0] * QQ[0, 1] + P[1, 1] * QQ[1, 1];

                key[0, 0] = key[0, 0] % 26;
                key[1, 0] = key[1, 0] % 26;
                key[0, 1] = key[0, 1] % 26;
                key[1, 1] = key[1, 1] % 26;

                if (key[0, 0] < 0)
                    key[0, 0] += 26;
                if (key[1, 0] < 0)
                    key[1, 0] += 26;
                if (key[0, 1] < 0)
                    key[0, 1] += 26;
                if (key[1, 1] < 0)
                    key[1, 1] += 26;

                if (length % 2 != 0)
                {
                    MessageBox.Show("密文输入错误,输入大写字母应为偶数个");
                    sign = false;
                }
                else
                {
                    for (int i = 0; i < length; i = i + 2)
                    {
                        XL[0, 0] = turnCToI(text3[i]);
                        XL[1, 0] = turnCToI(text3[i + 1]);
                        if (XL[0, 0] == -1 || XL[1, 0] == -1)
                        {
                            MessageBox.Show("密文输入错误,只能输入大写字母");
                            sign = false;
                            break;
                        }
                        
                        XL[0, 0] = XL[0, 0] * key[0, 0] + XL[1, 0] * key[0, 1];
                        XL[1, 0] = XL[0, 0] * key[1, 0] + XL[1, 0] * key[1, 1];

                        XL[0, 0] = XL[0, 0] % 26;
                        XL[1, 0] = XL[1, 0] % 26;
                        if (XL[0, 0] < 0)
                            XL[0, 0] += 26;
                        if (XL[1, 0] < 0)
                            XL[0, 0] += 26;
                        text4 += turnIToC(XL[0, 0]);
                        text4 += turnIToC(XL[1, 0]);


                    }
                }

            }

            if (sign == true)
            {
                textBox4.ReadOnly = false;
                textBox4.Text = text4;
            }
            
        }
        private int turnCToI(char x)
        {
            if (x == 'A')
                return 1;
            else if (x == 'B')
                return 2;
            else if (x == 'C')
                return 3;
            else if (x == 'D')
                return 4;
            else if (x == 'E')
                return 5;
            else if (x == 'F')
                return 6;
            else if (x == 'G')
                return 7;
            else if (x == 'H')
                return 8;
            else if (x == 'I')
                return 9;
            else if (x == 'J')
                return 10;
            else if (x == 'K')
                return 11;
            else if (x == 'L')
                return 12;
            else if (x == 'M')
                return 13;
            else if (x == 'N')
                return 14;
            else if (x == 'O')
                return 15;
            else if (x == 'P')
                return 16;
            else if (x == 'Q')
                return 17;
            else if (x == 'R')
                return 18;
            else if (x == 'S')
                return 19;
            else if (x == 'T')
                return 20;
            else if (x == 'U')
                return 21;
            else if (x == 'V')
                return 22;
            else if (x == 'W')
                return 23;
            else if (x == 'X')
                return 24;
            else if (x == 'Y')
                return 25;
            else if (x == 'Z')
                return 0;
            else
                return -1;

        }

        private char turnIToC(int x)
        {
            if (x == 1)
                return 'A';
            else if (x == 2)
                return 'B';
            else if (x == 3)
                return 'C';
            else if (x == 4)
                return 'D';
            else if (x == 5)
                return 'E';
            else if (x == 6)
                return 'F';
            else if (x == 7)
                return 'G';
            else if (x == 8)
                return 'H';
            else if (x == 9)
                return 'I';
            else if (x == 10)
                return 'J';
            else if (x == 11)
                return 'K';
            else if (x == 12)
                return 'L';
            else if (x == 13)
                return 'M';
            else if (x == 14)
                return 'N';
            else if (x == 15)
                return 'O';
            else if (x == 16)
                return 'P';
            else if (x == 17)
                return 'Q';
            else if (x == 18)
                return 'R';
            else if (x == 19)
                return 'S';
            else if (x == 20)
                return 'T';
            else if (x == 21)
                return 'U';
            else if (x == 22)
                return 'V';
            else if (x == 23)
                return 'W';
            else if (x == 24)
                return 'X';
            else if (x == 25)
                return 'Y';
            else if (x == 0)
                return 'Z';
            else
                return 'e';


        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -