private void KeyBtn_Click_1(object sender, EventArgs e) { if (radioBtn1.Checked == true) { if (Encoding.Default.GetBytes(KeyText.Text).Length != 16) { MessageBox.Show("ERROR!密钥个数必须为16个字符或8个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if (radioBtn2.Checked == true) { if (Encoding.Default.GetBytes(KeyText.Text).Length != 24) { MessageBox.Show("ERROR!密钥个数必须为24个字符或12个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if (radioBtn3.Checked == true) { if (Encoding.Default.GetBytes(KeyText.Text).Length != 32) { MessageBox.Show("ERROR!密钥个数必须为32个字符或16个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { if (true) { int[,] key = KeyExpansion.GetKeyExpansion(KeyText.Text.ToString().Trim()); KeyExpansionForm KeyForm = new KeyExpansionForm(); KeyForm.Show(); KeyForm.SetText(key); KeyForm.Settext(); } if (true) { int[,] key = KeyExpansion.GetKeyExpansion(KeyText.Text.ToString().Trim()); int[,] cipherKey = new int[key.GetLength(0), key.GetLength(1)]; int[] key1 = new int[key.GetLength(1)]; for (int i = 0; i < key.GetLength(1); i++) { cipherKey[0, i] = key[0, i]; cipherKey[key.GetLength(0) - 1, i] = key[key.GetLength(0) - 1, i]; } for (int i = 1; i < key.GetLength(0) - 1; i++) { for (int j = 0; j < key.GetLength(1); j++) { key1[j] = key[i, j]; } key1 = MixColumn.InvMixColumn(key1); for (int j = 0; j < key.GetLength(1); j++) { cipherKey[i, j] = key1[j]; } } KeyExpansionForm KeyForm = new KeyExpansionForm(); KeyForm.Show(); KeyForm.SetText(cipherKey); KeyForm.Settext(); } } }
private void EncryptionBtn_Click_1(object sender, EventArgs e) { if (radioBtn1.Checked == true) { if (Encoding.Default.GetBytes(KeyText.Text).Length != 16) { MessageBox.Show("ERROR!密钥个数必须为16个字符或8个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if (radioBtn2.Checked == true) { if (Encoding.Default.GetBytes(KeyText.Text).Length != 24) { MessageBox.Show("ERROR!密钥个数必须为24个字符或12个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if (radioBtn3.Checked == true) { if (Encoding.Default.GetBytes(KeyText.Text).Length != 32) { MessageBox.Show("ERROR!密钥个数必须为32个字符或16个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { if (KeyText.Text.Length == 0) { MessageBox.Show("请输入密钥!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (PlainText.Text.Length == 0 && KeyText.Text.Length != 0) { MessageBox.Show("请输入明文!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } sw4.Start(); int[,] key = KeyExpansion.GetKeyExpansion(KeyText.Text.ToString().Trim()); int[,] cipher = Encryption.Getencryption(PlainText.Text.ToString(), key); StringBuilder strB = new StringBuilder(); for (int i = 0; i < cipher.GetLength(0); i++) { for (int j = 0; j < 16; j++) { StringBuilder s = new StringBuilder(); byte[] buf = new byte[2]; s = SubBytes.GetStr(cipher[i, j]); buf[0] = Convert.ToByte(s.ToString(0, 4), 2); buf[1] = Convert.ToByte(s.ToString(4, 4), 2); strB.Append(alphabet[buf[0]] + "" + alphabet[buf[1]]); } } CipherText.Text = strB.ToString(); sw4.Stop(); } TimeSpan ts3 = sw4.Elapsed; label2.Text = ts3.ToString(); //MessageBox.Show("解密用时" + ts3, "计时结果"); }
private void DecryptionBtn_Click_1(object sender, EventArgs e) { if (radioBtn1.Checked == true) { if (Encoding.Default.GetBytes(KeyText.Text).Length != 16) { MessageBox.Show("密钥个数必须为16个字符或8个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if (radioBtn2.Checked == true) { if (Encoding.Default.GetBytes(KeyText.Text).Length != 24) { MessageBox.Show("密钥个数必须为24个字符或12个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } if (radioBtn3.Checked == true) { if (Encoding.Default.GetBytes(KeyText.Text).Length != 32) { MessageBox.Show("密钥个数必须为32个字符或16个汉字!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } } else { if (KeyText.Text.Length == 0) { MessageBox.Show("请输入密钥!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (CipherText.Text.Length == 0 && KeyText.Text.Length != 0) { MessageBox.Show("请输入密文!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } sw4.Start(); int[,] key = KeyExpansion.GetKeyExpansion(KeyText.Text.ToString().Trim()); int[,] cipherKey = new int[key.GetLength(0), key.GetLength(1)]; int[] key1 = new int[key.GetLength(1)]; for (int i = 0; i < key.GetLength(1); i++) { cipherKey[0, i] = key[0, i]; cipherKey[key.GetLength(0) - 1, i] = key[key.GetLength(0) - 1, i]; } for (int i = 1; i < key.GetLength(0) - 1; i++) { for (int j = 0; j < key.GetLength(1); j++) { key1[j] = key[i, j]; } key1 = MixColumn.InvMixColumn(key1); for (int j = 0; j < key.GetLength(1); j++) { cipherKey[i, j] = key1[j]; } } int[,] plain = Decryption.Getdecryption(CipherText.Text.ToString(), cipherKey); StringBuilder strB = new StringBuilder(); StringBuilder s = new StringBuilder(); byte[] result = new byte[plain.GetLength(0) * 16]; for (int i = 0; i < plain.GetLength(0); i++) { for (int j = 0; j < 16; j++) { for (int h = 7; h >= 0; h--) { s.Append(plain[i, j] >> h & 1); } } } for (int i = 0; i < result.Length; i++) { result[i] = Convert.ToByte(s.ToString(i * 8, 8), 2); } strB.Append(Encoding.Default.GetString(result)); PlainText.Text = strB.ToString(); sw4.Stop(); } TimeSpan ts4 = sw4.Elapsed; label2.Text = ts4.ToString(); //MessageBox.Show("解密用时" + ts4, "计时结果"); }