Exemplo n.º 1
0
        private async void rsaDecryptTxtBtn_Click(object sender, EventArgs e)
        {
            if ((PTxtBox.Text != "") && (QTxtBox.Text != "") && (ETxtBox.Text != "") && (rsaCryptedTxtBox.Text != ""))
            {
                RSA rsa = new RSA();
                rsa.P = BigInteger.Parse(PTxtBox.Text);
                rsa.Q = BigInteger.Parse(QTxtBox.Text);
                rsa.E = BigInteger.Parse(ETxtBox.Text);
                //BigInteger data = BigInteger.Parse(rsaCryptedTxtBox.Text);
                BigInteger data = new BigInteger(Convert.FromBase64String(rsaCryptedTxtBox.Text));

                if (!rsa.MillerRabinTest(rsa.P, 10))
                {
                    MessageBox.Show("P mora biti prost broj. Izgenerisan je prost broj koji je najblizi unetom broju.");
                    rsa.P        = rsa.GenerateNearestPrime(rsa.P);
                    PTxtBox.Text = rsa.P.ToString();
                }
                if (!rsa.MillerRabinTest(rsa.Q, 10))
                {
                    MessageBox.Show("Q mora biti prost broj. Izgenerisan je prost broj koji je najblizi unetom broju.");
                    rsa.Q        = rsa.GenerateNearestPrime(rsa.Q);
                    QTxtBox.Text = rsa.Q.ToString();
                }
                rsa.CalculateN();
                //if (data > rsa.N)
                //{
                //    MessageBox.Show("P i Q moraju biti veci brojevi kako bi N bilo vece ili jednako od podatka.");
                //    return;
                //}
                rsa.CalculatePhi();
                if (BigInteger.GreatestCommonDivisor(rsa.Phi, rsa.E) != 1)
                {
                    MessageBox.Show("E mora biti uzajamno prosto sa Phi. Izgenerisano je najblize E koje ispunjava uslov.");
                    rsa.GeneratePublicKey();    // trazi se prvo E koje ispunjava uslov
                    ETxtBox.Text = rsa.E.ToString();
                }
                EncryptTextMessage msg = new EncryptTextMessage()
                {
                    Data      = data.ToByteArray(),
                    P         = rsa.P.ToByteArray(),
                    Q         = rsa.Q.ToByteArray(),
                    Key       = rsa.E.ToByteArray(),
                    Algorithm = AlgorithmType.RSA
                };
                byte[] response = await Crypto.DecryptText(msg);

                if (response != null)
                {
                    rsaDecryptedTxtBox.Text = Encoding.ASCII.GetString(response);
                    //rsaDecryptedTxtBox.Text = new BigInteger(response).ToString();
                }
            }
            else
            {
                MessageBox.Show("Potrebna polja nisu popunjena.");
            }
        }