编程语言
首页 > 编程语言> > Visual Studio 2019下用 C# 实现 Hill2 二阶希尔密码 的加密、解密 GUI界面

Visual Studio 2019下用 C# 实现 Hill2 二阶希尔密码 的加密、解密 GUI界面

作者:互联网

1、什么是Hill2

什么是Hill密码

Hill2指的是n=2时,即加密矩阵为2 × 2,明文转为2维向量时候的加密。是Hill最简单的情况。


举个例子:

加密过程:


解密过程:

2、C#代码实现

在这里插入图片描述

using System;
using System.Windows.Forms;

namespace 古典加密系列
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        //数学函数,快速幂,逆元,公约数
        //public static int pows(int a, int x, int p) { if (x == 0) return 1; int t = pows(a, x >> 1, p); if (x % 2 == 0) return t * t % p; return t * t % p * a % p; }
        //public static int inv(int x, int p) { return pows(x, p - 2, p); }
        public static void EXGCD(int a, int b, ref int d, ref int x, ref int y, int MOD) { if (b==0) { d = a; x = 1; y = 0; } else { EXGCD(b, a % b,ref d,ref y,ref x, MOD); y -= x * (a / b); } }
        public static int inv(int a, int MOD) { int d=0, x=0, y=0; EXGCD(a, MOD, ref d, ref x, ref y, MOD); return d == 1 ? (x + MOD) % MOD : -1; }
        public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }


        //字符串函数,判断是否只由字母组成,字符串大写转小写
        public static bool check(string str)
        {
            bool ok = true;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] >= 'a' && str[i] <= 'z') continue;
                if (str[i] >= 'A' && str[i] <= 'Z') continue;
                ok = false; break;
            }
            return ok;
        }
        public static string xiaoxie(string str)
        {
            string res = "";
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] >= 'a' && str[i] <= 'z') res = res + str[i];
                if (str[i] >= 'A' && str[i] <= 'Z') res = res + Convert.ToChar(str[i] + ('a' - 'A'));
            }
            return res;
        }

        //2阶Hill密码,加密和解密
        public static string jiami(string plaintext, string key)
        {
            key = xiaoxie(key);
            plaintext = xiaoxie(plaintext);

            int[,] A = new int[2, 2]{
                {key[0]-'a', key[2]-'a'},
                {key[1]-'a', key[3]-'a'}
            };

            int t = A[1, 1] * A[0, 0] - A[1, 0] * A[0, 1];
            t = (t % 26 + 26) % 26;
            if (gcd(t, 26) != 1)
            {
                MessageBox.Show("矩阵的行列式和26需要互质,否则没有逆矩阵");
                return "";
            }

            //两个字母组成向量进行加密,不够就补0
            int flag = 0;
            if (plaintext.Length % 2 == 1)
            {
                flag = 1;
                plaintext = plaintext + 'a';
            }
            //两两一组写成矩阵形式
            int[,] X = new int[2, plaintext.Length / 2];
            int cnt = 0;
            for (int i = 0; i < plaintext.Length; i++)
            {
                if (i % 2 == 0) X[0, cnt] = plaintext[i] - 'a';
                else
                {
                    X[1, cnt] = plaintext[i] - 'a';
                    cnt++;
                }
            }
            //矩阵乘法
            int[,] Y = new int[2, plaintext.Length / 2];
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < plaintext.Length / 2; j++)
                {
                    int tmp = 0;
                    for (int k = 0; k < 2; k++)
                    {
                        tmp += A[i, k] * X[k, j];
                    }
                    Y[i, j] = tmp%26;
                }
            }
            string res = "";
            for (int i = 0; i < plaintext.Length / 2; i++)
            {
                char ch = (char)(Y[0, i]+'a');
                res = res + ch;
                ch = (char)(Y[1, i]+'a');
                res = res + ch;
            }
            if(flag == 1)
            {
                res = res.Substring(0, res.Length - 1);
            }
            return res;
        }

        public static string jiemi(string plaintext, string key)
        {
            key = xiaoxie(key);
            plaintext = xiaoxie(plaintext);

            //逆矩阵
            int a = key[0] - 'a', b = key[2] - 'a', c = key[1] - 'a', d = key[3] - 'a';

            int t = ((a * d - b * c) % 26+26)%26;
            if (gcd(t, 26) != 1)
            {
                MessageBox.Show("矩阵的行列式和26需要互质,否则没有逆矩阵");
                return "";
            }
            t = inv(t,26)%26;
            int aa = d * t % 26, bb = (-b * t % 26 + 26) % 26, cc = (-c * t % 26 + 26) % 26, dd = a * t % 26;

            int[,] A = new int[2, 2]{
                {aa,bb },
                {cc,dd }
            };


            //两个字母组成向量进行解密,不够就补0
            int flag = 0;
            if (plaintext.Length % 2 == 1)
            {
                flag = 1;
                plaintext = plaintext + 'a';
            }
            //两两一组写成矩阵形式
            int[,] X = new int[2, plaintext.Length / 2];
            int cnt = 0;
            for (int i = 0; i < plaintext.Length; i++)
            {
                if (i % 2 == 0) X[0, cnt] = plaintext[i] - 'a';
                else
                {
                    X[1, cnt] = plaintext[i] - 'a';
                    cnt++;
                }
            }
            //矩阵乘法
            int[,] Y = new int[2, plaintext.Length / 2];
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < plaintext.Length / 2; j++)
                {
                    int tmp = 0;
                    for (int k = 0; k < 2; k++)
                    {
                        tmp += A[i, k] * X[k, j];
                    }
                    Y[i, j] = tmp % 26;
                }
            }
            string res = "";
            for (int i = 0; i < plaintext.Length / 2; i++)
            {
                char ch = (char)(Y[0, i] + 'a');
                res = res + ch;
                ch = (char)(Y[1, i] + 'a');
                res = res + ch;
            }
            if (flag == 1)
            {
                res = res.Substring(0, res.Length - 1);
            }
            return res;
        }

		//GUI按钮
        private void button1_Click(object sender, EventArgs e)
        {
            string key = textBox3.Text;
            if (key.Length != 4 || check(key) == false)
            {
                MessageBox.Show("密钥输入错误,请重新输入密钥");
                return;
            }

            string plaintext = textBox1.Text;
            if (check(plaintext) == false)
            {
                MessageBox.Show("明文输入错误,请重新输入明文");
                return;
            }

            string ciphertext = jiami(plaintext, key);
            textBox2.Text = ciphertext;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string key = textBox3.Text;
            if (key.Length != 4 || check(key) == false)
            {
                MessageBox.Show("密钥输入错误,请重新输入密钥");
                return;
            }

            string plaintext = textBox2.Text;
            if (check(plaintext) == false)
            {
                MessageBox.Show("明文输入错误,请重新输入明文");
                return;
            }

            string ciphertext = jiemi(plaintext, key);
            textBox1.Text = ciphertext;
        }
    }
}


标签:26,Hill2,C#,res,GUI,Length,int,plaintext,key
来源: https://blog.csdn.net/qq_33957603/article/details/120953338