1、什么是Hill2

什么是Hill密码

  • 希尔密码(Hill)使用每个字母在字母表中的顺序作为其对应的数字,即 A=0,B=1,C=2 等,然后将明文转化为 n 维向量,跟一个 n × n 的矩阵相乘,再将得出的结果模 26。
  • 注意用作加密的矩阵(即密匙)在 必须是可逆的,否则就不可能解码。只有矩阵的行列式和26互质,才是可逆的。

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


举个例子:

  • 明文串 x = EastChinaNormalUniversity,密钥 = [[2, 5],[9,5]] 或 “cjff”。

加密过程:

  • 加密:密文向量 = 明文向量 * 密钥矩阵 (mod 26)

  • 先将明文串对应英文字母编码表进行数字转化:
    4 0 18 19 2 7 8 13 0 13 14 17 12 0 11 20 13 8 21 4 17 18 8 19 24

  • 然后两两一组写成矩阵形式:

    发现少了一个,所以在最后做补0处理(即补字符a)

  • 加密的过程就是矩阵乘法,前行乘后列,记得膜26(不膜会乱码!!)

  • 得到密文矩阵后,按照分组对应的向量转成字母
    ikbxnbdhnnjdyesrobkbujhlw


解密过程:

  • 解密和加密差不多,就是先计算出原本密钥矩阵的逆矩阵,再直接乘密文就可以了。
  • 二阶矩阵的逆矩阵公式为:
  • 求逆矩阵的时候记得用逆元,求出逆矩阵后记得膜26。(逆元记得用拓展欧几里得!不要用快速幂)
    比如 [[2, 5],[9,5]] 会得到 [ [ 11, 5 ], [ 1 , 20 ] ]。
  • 最后用上面一样的方法去乘以密文即可。

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 "";}//两个字母组成向量进行加密,不够就补0int 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 }};//两个字母组成向量进行解密,不够就补0int 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;}}
}

3、参考资料

https://ctf-wiki.org/crypto/classical/polyalphabetic/#hill
https://zhuanlan.zhihu.com/p/328562250
https://wenku.baidu.com/view/f1b150cd51e79b8969022619?pn=2&pu=sl@1,pw@500,sz@224_220,pd@1,fz@2,lp@3,tpl@color,
http://www.go60.top/wenzhang/juzhen/14.html
https://matrix.reshish.com/zh/inverse.php
https://wenku.baidu.com/view/b0525966f5335a8102d22078.html

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

  1. Tesseract OCR——Windows 10 + CMake-GUI + Visual Studio 2019下编译和使用解决方案

    基本概念 Tesseract OCR:Tesseract-OCR 引擎最先由HP实验室于1985年开始研发,至1995年时已经成为OCR业内最准确的三款识别引擎之一.然而,HP不久便决定放弃OCR业务 ...

  2. Visual Studio 2019 下Python的开发环境搭建

    Python的开发环境有各种各样的,在Windows下可以通过各种IDE(I Integrated Development Environment)来进行开发,比如Pycharm,Spyder, Th ...

  3. 在visual studio 2019下安装ipopt并实现非线性规划求解(C++)

    1. 首先安装ipopt 1.1 尝试vcpks包管理器安装(失败) 安装vcpkg: 按照下边网址操作https://github.com/Microsoft/vcpkg#quick-start-w ...

  4. Microsoft Visual Studio 2019介绍之使用入门

    Microsoft Visual Studio 2019介绍之使用入门 目录 1.前言: 2.Visual Studio安装: 3.项目的创建和代码的编译与运行: 4.常见问题及解决方法: 1.前言 ...

  5. PaddleOCR——Visual Studio 2019 环境下C++推理部署 CMake 编译解决方案

    PaddleOCR--Visual Studio 2019 环境下C++推理部署 CMake 编译解决方案 官方文档 环境配置 Step1: 下载PaddlePaddle C++ 预测库 paddle ...

  6. Microsoft Visual Studio 2019 美化之——透明化窗口 Microsoft Visual Studio 2019 C/C++ Windows 下重量级编辑器 Editor

    Microsoft Visual Studio 2019 美化之--透明化窗口 Microsoft Visual Studio 2019 C/C++ Windows 下重量级编辑器 ?Editor? ...

  7. 在Win 11下使用Visual Studio 2019和cygwin编译JBR(Java SDK 17)源码

    很多文章介绍了JDK 8和JDK11源码在Linux编译,很少有人介绍了JDK 17在windows的编译过程,所以写了这篇文章,为什么选用JBR 17版本,因为JBR17 版本集成了HotSwapA ...

  8. Visual Studio 2019 和 qt 5.15.1 下 opengl 的运用 - Lighting - 02 - BasicLighting

    学习learnopengl文章对应地址:https://learnopengl-cn.github.io/02%20Lighting/02%20Basic%20Lighting/ VS.qt版本分别给 ...

  9. Visual Studio 2019 和 qt 5.15.1 下 opengl 的运用 - Lighting - 01 - Colors

    学习learnopengl文章对应地址:https://learnopengl-cn.github.io/02%20Lighting/01%20Colors/ VS.qt版本分别给了多个文件和一个项目 ...

最新文章

  1. 机器学习(十七)——决策树, 推荐系统进阶
  2. 7. 整数反转(C, C++, Python)
  3. oracle账号密码修改后特别容易锁定_Oracle密码过期如何取消密码180天限制及过期,账号锁住的问题...
  4. 一分钟了解自动化测试
  5. linux /dev/null 中有数据
  6. 同样是百度输入法,定制远没有原版好用
  7. 提高网页效率的14条准则
  8. 计算机同步不了计算机策略,修复sysvol netlog共享和组策略不同步组策略丢失等问题...
  9. Pandas和Matplotlib用excel数据画双y轴折线图
  10. win10共享打印机搜索不到计算机,w10共享打印机搜索不到如何处理
  11. 二极管的分类、电路符号及万用表测发光二极管正负极
  12. 技术分享| RTC通讯中常用的音频格式
  13. Dim 和 Redim
  14. Android开发中遇到的坑
  15. Task 编程中的异常处理
  16. PdfSharp -- 根据PDF模板导出PDF
  17. uni-app - 设置最外层容器高度为100%
  18. tl wn322g linux驱动下载,TL-WN322G+驱动
  19. 计算机视觉中的MAP的理解(mean average precision)
  20. 37-工欲善其事必先利其器:学会使用各种工具

热门文章

  1. windows 路径
  2. windows 画图工具 —— mspaint 的使用
  3. Python 数据结构与算法——从某个列表中找出两个彼此最接近但不相等的数
  4. utilities——比较与排序规则(C++)
  5. C++设计模式——从多态到观察者模式到MVC架构
  6. python爬百度新闻_13、web爬虫讲解2—Scrapy框架爬虫—Scrapy爬取百度新闻,爬取Ajax动态生成的信息...
  7. python课程网课-有没有简单易懂的入门级Python辅导书或网络课程?
  8. python新手入门代码-新手零基础入门Python项目实战
  9. python自动化测试-五大自动化测试的Python框架
  10. python单词的含义-python实现单词本功能