Ejemplo n.º 1
0
        private void btnCalcManual_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtQ.Text) || string.IsNullOrEmpty(txtP.Text))
            {
                MessageBox.Show("Giá trị Q và P không được bỏ trống ", "Lỗi nhập tham sô đầu vào", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Int64  numberP = 0, numberQ = 0;
            string inputP = txtP.Text, inputQ = txtQ.Text;

            if (!Int64.TryParse(inputP, out numberP) || numberP <= 0 || !Rsa.IsPrimeNumber(numberP))
            {
                MessageBox.Show("Giá trị P không hợp lệ. P phải là số nguyên tố.", "Lỗi nhập tham sô đầu vào", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            if (!Int64.TryParse(inputQ, out numberQ) || numberQ <= 0 || !Rsa.IsPrimeNumber(numberQ))
            {
                MessageBox.Show("Giá trị Q không hợp lệ. Q phải là số nguyên tố.", "Lỗi nhập tham sô đầu vào", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            if (numberP == numberQ)
            {
                MessageBox.Show("Giá trị Q và P không được trùng nhau.", "Lỗi nhập tham sô đầu vào", MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                return;
            }
            try
            {
                _rsaObject = new Rsa(numberP, numberQ);
                _rsaObject.GeneralKeys();

                //txtP.Text = _rsaObject.FistPrime.ToString();
                //txtQ.Text = _rsaObject.SecondPrime.ToString();
                txtN.Text            = _rsaObject.Modulus.ToString();
                txtE.Text            = _rsaObject.PublicExponent.ToString();
                txtD.Text            = _rsaObject.PrivateExponent.ToString();
                txtEuler.Text        = _rsaObject.Euler.ToString();
                btnExportKey.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Giá trị tính toán quá lớn hoặc thông sô đầu vào không hợp lệ. " + ex.Message, "Lỗi trong quá trình tính toán", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
Ejemplo n.º 2
0
        private void btnExportKey_Click(object sender, EventArgs e)
        {
            if (_rsaObject.PrivateKey == null || _rsaObject.PublicKey == null)
            {
                _rsaObject.GeneralKeys();
            }
            string privateKey = _rsaObject.ToXmlString(false);
            string publicKey  = _rsaObject.ToXmlString(true);

            if (string.IsNullOrEmpty(privateKey) || string.IsNullOrEmpty(publicKey))
            {
                MessageBox.Show("Không thể xuất file key, xin hay thữ lại.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            FolderBrowserDialog fbd    = new FolderBrowserDialog();
            DialogResult        result = fbd.ShowDialog();
            var folderPath             = fbd.SelectedPath;

            File.WriteAllText(folderPath + "\\privatekey.pvk", privateKey);
            File.WriteAllText(folderPath + "\\publickey.pbk", publicKey);
            MessageBox.Show("Cặp key 'privatekey.pvk','publickey.pbk' được lưu với đường dẫn thư mục:\n" + folderPath);
            Process.Start(folderPath);
        }