if (txt[i] == aa[j]) {
txt2[i] = bb[j];
} } }//密文
string tx2 = \;
for (int i = 0; i < txt2.Length; i++) {
tx2 += txt2[i]; }
this.textBox2.Text = tx2;
}//还可以使用list
private void textBox1_TextChanged(object sender, EventArgs e) {
}
private void textBox2_TextChanged(object sender, EventArgs e) {
}
private void button2_Click(object sender, EventArgs e) {
string[] aa = new string[26];
aa[0] = \; aa[1] = \; aa[2] = \; aa[3] = \; aa[4] = \; aa[5] = \; aa[6] = \; aa[7] = \; aa[8] = \; aa[9] = \;
aa[10] = \; aa[11] = \; aa[12] = \; aa[13] = \; aa[14] = \; aa[15] = \; aa[16] = \; aa[17] = \; aa[18] = \; aa[19] = \; aa[20] = \; aa[21] = \; aa[22] = \; aa[23] = \; aa[24] = \; aa[25] = \;
string[] bb = new string[26];
bb[0] = \; bb[1] = \; bb[2] = \; bb[3] = \; bb[4] = \; bb[5] = \; bb[6] = \; bb[7] = \; bb[8] = \; bb[9] = \;
bb[10] = \; bb[11] = \; bb[12] = \; bb[13] = \; bb[14] = \; bb[15] = \; bb[16] = \; bb[17] = \; bb[18] = \; bb[19] = \; bb[20] = \; bb[21] = \; bb[22] = \; bb[23] = \; bb[24] = \; bb[25] = \;
string tx = textBox3.Text; int le = tx.Length;
string[] txt = new string[le]; for (int i = 0; i < le; i++) {
txt[i] = tx[i].ToString(); }//密文
string[] txt2 = new string[le]; for(int j=0;j for (int m = 0; m < 26; m++) { if(txt[j]==bb[m]) { txt2[j] = aa[m]; } } }//获得明文 10 string tx2 = \; for (int n = 0; n < le;n++ ) { tx2 += txt2[n]; } textBox4.Text = tx2; } } } 2.2.2运行界面 11 3 维吉尼亚密码 3.1算法原理 密码体制 设m是一个正整数。定义P定义: ?C?K?(Z26)m。对任意的密钥K?(k1,k2,?,km), eK(x1,x2,?,xm)?(x1?k1,x2?k2,?,xm?km) 和 dK(y1,y2,?ym)?(y1?k1,y2?k2,?,ym?km) 以上所有的运算都是在Z26上进行。 使用前面所述的方法,对应A?0,B?1,?,Z?25,则每个密钥K相当于一个长度为 m的字母串,称为密钥字。维吉尼亚密码一次加密m个明文字母。 例 假设m?6,密钥字为CIPHER,其对应于如下的数字串K=(2,8,15,7,4,17)。要加密的明文为: thiscryptosystemisnotsecure 将明文串转化为对应的数字,每六个为一组,使用密钥字进行模26下的加密运算如下所示: 19 7 8 18 2 17 24 15 19 14 18 24 18 19 2 8 15 7 4 17 2 8 15 7 4 17 2 8 21 15 23 25 6 8 0 23 8 21 22 15 20 1 12 4 12 8 18 13 14 19 18 4 2 20 17 4 15 7 4 17 2 8 15 7 4 17 2 8 15 19 19 12 9 15 22 8 25 8 19 22 25 19 则相应的密文应该为:VPXZGIAXIVWPUBTTMJPWIZITWZT 解密时,使用相同的密钥字,进行逆运算即可。 维吉尼亚密码的密钥空间大小为26,所以即使m的值很小,使用穷尽密钥搜索方法也需要很长的时间。例如,当m?5时,密钥空间大小超过1.1?10,这样的密钥量已经超出了使用手算进行穷尽搜索的能力范围(当然使用计算机另当别论)。 7m3.2实现过程 3.2.1程序代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace mimaxue46.FormsClass { public partial class weijiniya : Form { public weijiniya() { InitializeComponent(); } private void weijiniya_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { } private void button1_Click_1(object sender, EventArgs e) { #region textBox3.Clear(); string[] aa = new string[26]; aa[0] = \; aa[1] = \; aa[2] = \; aa[3] = \; aa[4] = \; aa[5] = \; aa[6] = \; aa[7] = \; aa[8] = \; aa[9] = \; aa[10] = \; aa[11] = \; aa[12] = \; aa[13] = \; aa[14] = \; aa[15] = \; aa[16] = \; aa[17] = \; aa[18] = \; aa[19] = \; aa[20] = \; aa[21] = \; aa[22] = \; aa[23] = \; aa[24] = \; aa[25] = \; string[] bb = new string[26]; bb[0] = \; bb[1] = \; bb[2] = \; bb[3] = \; bb[4] = \; bb[5] = \; bb[6] = \; bb[7] = \; bb[8] = \; bb[9] = \; 13 bb[10] = \; bb[11] = \; bb[12] = \; bb[13] = \; bb[14] = \; bb[15] = \; bb[16] = \; bb[17] = \; bb[18] = \; bb[19] = \; bb[20] = \; bb[21] = \; bb[22] = \; bb[23] = \; bb[24] = \; bb[25] = \; #endregion try { #region 秘钥 string tx = textBox1.Text; int le = tx.Length; string[] txt = new string[le]; int[] txt2 = new int[le]; for (int i = 0; i < le; i++) { txt[i] = tx[i].ToString(); } for (int i = 0; i < le; i++) { for (int j = 0; j < 26; j++) { if (txt[i] == bb[j]) { txt2[i] = j; } } } #endregion #region 明文 string tx2 = textBox2.Text; int le2 = tx2.Length; string[] txt3 = new string[le2]; int[] txt4 = new int[le2]; for (int i = 0; i < le2; i++) { txt3[i] = tx2[i].ToString(); } for (int i = 0; i < le2; i++) { for (int j = 0; j < 26; j++) { if (txt3[i] == aa[j]) { txt4[i] = j; } } } #endregion #region 加密 int[] txt5 = new int[le2]; int m = le2 / le;//得到明文的整数组的个数 for (int i = 0; i < (m + 1); i++) { for (int j = 0; j < le; j++) { if ((j + i * le) < le2) { txt5[j + i * le] = (txt4[j + i * le] + txt2[j]) % 26; } } } #endregion #region 密文显示 14