string[] txt6 = new string[le2]; for (int i = 0; i < le2; i++) {
txt6[i] = bb[txt5[i]];
}
string tx7 = \;
for (int i = 0; i < le2; i++) {
tx7 += txt6[i]; }
textBox3.Text = tx7; #endregion } catch {
MessageBox.Show(\请输入正确的秘钥及明文\); }
}
private void button2_Click(object sender, EventArgs e) {
#region
textBox6.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] = \;
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 = textBox4.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 = textBox5.Text;
15
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] == bb[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) {
int n = (txt4[j + i * le] - txt2[j]) % 26; if (n < 0) {
txt5[j + i * le] = (n + 26) % 26; } else {
txt5[j + i * le] = n % 26; }
} } }
#endregion
#region 明文显示
string[] txt6 = new string[le2]; for (int i = 0; i < le2; i++) {
txt6[i] = aa[txt5[i]];
}
string tx7 = \;
for (int i = 0; i < le2; i++) {
tx7 += txt6[i]; }
textBox6.Text = tx7;
#endregion } catch {
MessageBox.Show(\请输入正确格式的秘钥及密文\); }
16
} } }
3.2.1运行界面
17
4 Eulid算法
4.1算法原理
设a 为正整数,b、q、r是三个整数,如果整数q满足b?aq?r,则 gcd(b,a)?gcd(a,r)
设r0?a , r1?b , a?b?0 反复利用 gcd(b,a)?gcd(a,r) ,可以得到
ri?ri?1qi?1 + ri?1 , 0 < ri?2 < ri?1 直到 0?i?n?1 , rn?1?0 则 gcd(a,b)等于最大的非零余数rn
例如gcd(25,15)?gcd(15,10)?gcd(10,5)?gcd(5,0)?5
4.2实现过程 4.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 Eliud : Form {
public Eliud() {
InitializeComponent(); }
private void textBox1_TextChanged(object sender, EventArgs e) { }
private void eulid(TextBox textbox1, TextBox textbox2) {
int a, b, r; r = 1; try {
a = Convert.ToInt16(textBox1.Text); b = Convert.ToInt16(textBox2.Text); while (r != 0) {
r = a % b; a = b; b = r; }
textBox3.Text = Convert.ToString(a); } catch {
MessageBox.Show(\请正确输入数字\); } }
18
private void button1_Click(object sender, EventArgs e) {
//r = a / b;这是求解整数商部分
eulid(textBox1,textBox2); } } }
4.2.2运行界面
19