Exemplo n.º 1
0
        public void Option2()
        {
            RSA  client     = new RSA();
            bool canDoBlock = false;

            if (Convert.ToBoolean(RSA_Block.IsChecked))
            {
                if (Alphabet.Items.Count == 0)
                {
                    Message("No hay alfabeto definido para cifrar", 3);
                }
                else
                {
                    for (int i = 0; i < Alphabet.Items.Count; i++)
                    {
                        client.Alphabet.Add(Alphabet.Items[i].ToString());
                    }
                    canDoBlock = true;
                }
            }

            // Check common variables
            if (Convert.ToBoolean(encryptOption.IsChecked))
            {
                if (!String.IsNullOrWhiteSpace(NumberP.Text) && !String.IsNullOrWhiteSpace(NumberQ.Text) && !String.IsNullOrWhiteSpace(NumberE.Text))
                {
                    client.P     = BigInteger.Parse(NumberP.Text);
                    client.Q     = BigInteger.Parse(NumberQ.Text);
                    NumberN.Text = "" + client.N();
                    client.E     = BigInteger.Parse(NumberE.Text);

                    if (Convert.ToBoolean(RSA_Plane.IsChecked))
                    {
                        if (!String.IsNullOrWhiteSpace(PlainText.Text))
                        {
                            if (BigInteger.Parse(PlainText.Text).CompareTo(client.N()) < 0)
                            {
                                //TODO: Cifrar texto plano
                                TextEncrypt.Text = "" + client.Encrypt(BigInteger.Parse(PlainText.Text));
                            }
                            else
                            {
                                Message("El texto plano debe estar entre 0 y " + BigInteger.Subtract(client.N(), 1), 2);
                            }
                        }
                        else
                        {
                            Message("El texto a cifrar no puede ser vacío", 2);
                        }
                    }
                    else
                    {
                        //Block coding

                        if (!String.IsNullOrWhiteSpace(PlainBlock.Text) && canDoBlock)
                        {
                            if (client.CheckText(PlainBlock.Text))
                            {
                                //TODO Encrypt Block
                                BlockEncrypt.Text = client.Encrypt(PlainBlock.Text);
                            }
                            else
                            {
                                Message("El bloque a cifrar contiene caracteres que no estan en el alfabeto", 2);
                            }
                        }
                        else
                        {
                            Message("El bloque a cifrar no puede ser vacío", 2);
                        }
                    }
                }
            }

            if (Convert.ToBoolean(decryptOption.IsChecked))
            {
                if (!String.IsNullOrWhiteSpace(NumberE.Text) && !String.IsNullOrWhiteSpace(NumberN.Text))
                {
                    client.E = BigInteger.Parse(NumberE.Text);
                    List <BigInteger> factors = PrimeHelper.FindFactors(BigInteger.Parse(NumberN.Text));
                    client.P = factors[0];
                    client.Q = factors[1];

                    if (Convert.ToBoolean(RSA_Plane.IsChecked))
                    {
                        if (!String.IsNullOrWhiteSpace(CipherText.Text))
                        {
                            if (BigInteger.Parse(CipherText.Text).CompareTo(client.N()) < 0)
                            {
                                PlainDecrypt.Text = "" + client.Decrypt(BigInteger.Parse(CipherText.Text));
                            }
                            else
                            {
                                Message("El texto cifrado debe estar entre 0 y " + BigInteger.Subtract(client.N(), 1), 2);
                            }
                        }
                        else
                        {
                            Message("El texto a descifrar no puede ser vacío", 2);
                        }
                    }
                    else
                    {
                        //Block coding

                        if (!String.IsNullOrWhiteSpace(CipherBlock.Text) && canDoBlock)
                        {
                            if (client.CheckText(CipherBlock.Text))
                            {
                                //TODO Decrypt Block
                                BlockDecrypt.Text = client.Decrypt(CipherBlock.Text);
                            }
                            else
                            {
                                Message("El bloque cifrado contiene caracteres que no estan en el alfabeto", 2);
                            }
                        }
                        else
                        {
                            Message("El bloque a descifrar no puede ser vacío", 2);
                        }
                    }
                }
                else
                {
                    Message("Faltan datos para poder descifrar bloques o palabras", 2);
                }
            }
        }