示例#1
0
        private void btnMaHoa_Click(object sender, EventArgs e)
        {
            txtMHBanRo.Text = txtMHBanRo.Text.ToUpper();
            string tz = txtMHBanRo.Text;
            string kz = "";

            for (int iz = 0; iz < tz.Length; iz++)
            {
                if (tz[iz] < 'A' || tz[iz] > 'Z')
                {
                    continue;
                }
                kz = kz + tz[iz];
            }
            txtMHBanRo.Text = kz;

            if (txtMHBanRo.Text == "")
            {
                MessageBox.Show("Bản rõ đang trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            /// Kiểm tra khóa
            int k = (int)numRowCount.Value;

            Matrix mt = new Matrix(k, k);

            /// tính lại mt
            foreach (var item in panelGroupKhoa.Controls)
            {
                TextBox z = item as TextBox;

                Point pos = (Point)z.Tag;
                if (pos.X > k || pos.Y > k)
                {
                    continue;
                }

                int gt = 0;
                try
                {
                    gt = Int32.Parse(z.Text);
                }
                catch
                {
                    gt = 0;
                }
                mt[pos.X, pos.Y] = new Complex(gt);
            }

            if (!MaHoa.Crypto.Hill.ok(mt))
            {
                MessageBox.Show("Key chưa thỏa mãn. Không tồn tại ma trận nghịch đảo", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            txtMHBanMa.Text = Hill.encrypt(txtMHBanRo.Text, mt);
        }