Exemplo n.º 1
0
        // Generate RSA Keys function
        public static long[] GenerateKeys(long p, long q, long e)
        {
            long[] result = new long[3];
            long   phi, n, d;

            // calculate n and phi
            n   = p * q;
            phi = (p - 1) * (q - 1);

            long[] euclidOutput = new long[3];
            // find d using extended Euclidean algorithm
            euclidOutput = MathsProcessor.ExtEucAlg(e, phi);
            d            = euclidOutput[1];

            // set results to array
            result[0] = n;
            result[1] = phi;
            result[2] = d;

            // If d is negative it's not suitable, therefore add phi. This will change value of v
            // however the equation d*e - v*Ф = 1 will still hold
            if (d < 0)
            {
                result[2] += phi;
            }

            return(result);
        }
Exemplo n.º 2
0
        private void q2FactorizeBtn_Click(object sender, EventArgs e)
        {
            try
            {
                long inputNum = long.Parse(q2NumEntryTbx.Text);     // error handling here
                // get list of factors
                List <long> resultList = MathsProcessor.PrimeFactorize(inputNum);

                string resultString = "";
                // add each number to string
                foreach (var num in resultList)
                {
                    resultString += num.ToString() + "  ";
                }

                q2ResultRTbx.Text = resultString;
            }
            catch (FormatException frmtExcptn)
            {
                MessageBox.Show("Wrong number format. Please enter a correct number.");
            }
            catch (OverflowException oe)
            {
                MessageBox.Show("Entered number(s) is too big.");
            }
        }
Exemplo n.º 3
0
        private void q4Compute_Click(object sender, EventArgs e)
        {
            try
            {
                long numA = long.Parse(q4EntryATbox.Text);  // error handling
                long numB = long.Parse(q4EntryBTbox.Text);  // -//-

                long[] resultArray = MathsProcessor.ExtEucAlg(numA, numB);

                string equation = string.Format("{0} = {1:+#;-#}a {2:+#;-#;}b", resultArray[0], resultArray[1], resultArray[2]);
                q4EqtnOutTbox.Text = equation;

                string gcd  = resultArray[0].ToString();
                string strX = resultArray[1].ToString();
                string strY = resultArray[2].ToString();

                q4GcdOutTbox.Text = gcd;
                q4XOutTbox.Text   = strX;
                q4YOutTbox.Text   = strY;
            }
            catch (FormatException fe)
            {
                MessageBox.Show("Wrong or missing input. Please enter a and b in a correct format.");
            }
            catch (OverflowException oe)
            {
                MessageBox.Show("Entered number(s) is too big.");
            }
        }
Exemplo n.º 4
0
 private void ApplyEuclidAlg()
 {
     using (StreamWriter sw = new StreamWriter(@"Q3.txt"))
     {
         sw.WriteLine("Q3.\n");
         foreach (var pair in PairList)
         {
             sw.WriteLine("GCD({0}, {1}) = {2}", pair[0], pair[1], MathsProcessor.EuclidAlg(pair[0], pair[1]));
         }
     }
 }
Exemplo n.º 5
0
        private static long GenerateE(long p, long q)
        {
            //calculate Ф(p,q)
            long phi = (p - 1) * (q - 1);

            // start at p-2
            long candidateE = p - 2;

            while (MathsProcessor.EuclidAlg(candidateE, phi) != 1 && MathsProcessor.PrimeCheck(candidateE) != 1)
            {
                --candidateE;
            }

            return(candidateE);
        }
Exemplo n.º 6
0
 private void ApplyExtendedEuclidAlg()
 {
     using (StreamWriter sw = new StreamWriter(@"Q4.txt"))
     {
         sw.WriteLine("Q4.\n");
         foreach (var pair in PairList)
         {
             // display a and b
             sw.WriteLine("a = {0}, b = {1}", pair[0], pair[1]);
             // get gcd, x and y
             long[] result = MathsProcessor.ExtEucAlg(pair[0], pair[1]);
             // write an equation. formatters used to show correct sign
             sw.WriteLine("Equation: {1:+#;-#}a {2:+#;-#;}b = {0}", result[0], result[1], result[2]);
             // display GCD
             sw.WriteLine("GCD({0}, {1}) = {2}", pair[0], pair[1], result[0]);
             // display coefficients
             sw.WriteLine("x = {0}\ny = {1}\n", result[1], result[2]);
         }
     }
 }
Exemplo n.º 7
0
        private void q3CalcGcdBtn_Click(object sender, EventArgs e)
        {
            try
            {
                long numA = long.Parse(q3EntryATbox.Text);  // error handling
                long numB = long.Parse(q3EntryBTbox.Text);  // -//-

                long gcd = MathsProcessor.EuclidAlg(numA, numB);

                q3GcdOutLabel.Text = "The Greatest Common Divisor of " + numA + " and " + numB + " is " + gcd;
            }
            catch (FormatException fe)
            {
                MessageBox.Show("Wrong or missing input. Please enter a and b in a correct format.");
            }
            catch (OverflowException oe)
            {
                MessageBox.Show("Entered number(s) is too big.");
            }
        }
Exemplo n.º 8
0
        private void PrimeFactorizeNums()
        {
            List <long> numsList = GetNumsFromTbox();

            using (StreamWriter sw = new StreamWriter(@"Q2.txt"))        // modify file paths later
            {
                sw.WriteLine("Q2.\n");

                foreach (var num in numsList)
                {
                    List <long> factorList = MathsProcessor.PrimeFactorize(num);
                    sw.Write("The factors of {0} are: ", num);
                    foreach (var factor in factorList)
                    {
                        sw.Write(factor + " ");
                    }
                    sw.Write("\n");
                }
            }
        }
Exemplo n.º 9
0
        private void q6DecryptBtn_Click(object sender, EventArgs e)
        {
            try
            {
                long cyphertext = long.Parse(q6CypherTbox.Text);
                long decExp     = long.Parse(q6DecryptExpTbox.Text);
                long rsaMod     = long.Parse(q6RsaModTbox.Text);

                BigInteger message = MathsProcessor.RSADecrypt(cyphertext, decExp, rsaMod);

                q6PlainTxtTbox.Text = message.ToString();
            }
            catch (FormatException fe)
            {
                MessageBox.Show("Wrong or missing input. Please fill all input fields in a correct number format.");
            }
            catch (OverflowException oe)
            {
                MessageBox.Show("Entered number(s) is too big.");
            }
        }
Exemplo n.º 10
0
        private void q5EncryptBtn_Click(object sender, EventArgs e)
        {
            try
            {
                long msg    = long.Parse(q5MsgEntryTbox.Text); // error handling
                long encExp = long.Parse(q5EncrExpTbox.Text);
                long rsaMod = long.Parse(q5RsaModTbox.Text);

                BigInteger cyphertext = MathsProcessor.RSAEncrypt(msg, encExp, rsaMod);

                q5CypherTbox.Text = cyphertext.ToString();
            }
            catch (FormatException fe)
            {
                MessageBox.Show("Wrong or missing input. Please fill all input fields in a correct number format.");
            }
            catch (OverflowException oe)
            {
                MessageBox.Show("Entered number(s) is too big.");
            }
        }
Exemplo n.º 11
0
        private void PrimeCheckNums()
        {
            List <long> numsList = GetNumsFromTbox();

            using (StreamWriter sw = new StreamWriter(@"Q1.txt"))        // modify file paths later
            {
                sw.WriteLine("Q1.\n");

                foreach (var num in numsList)
                {
                    if (MathsProcessor.PrimeCheck(num) == 1)
                    {
                        sw.WriteLine("The number {0} is prime", num);
                    }
                    else
                    {
                        sw.WriteLine("The number {0} is NOT prime", num);
                    }
                }
            }
        }
Exemplo n.º 12
0
        private void RSAGenerateBtn_Click(object sender, EventArgs e)
        {
            try
            {
                long p = long.Parse(EnterPTbox.Text);   /// ERROR HANDLING HERE
                long q = long.Parse(EnterQTbox.Text);
                long exp;

                // if forgot to input encryption exponent, generate it
                if (EnterETbox.Text == "")
                {
                    exp             = GenerateE(p, q);
                    EnterETbox.Text = exp.ToString();
                }
                else
                {
                    exp = long.Parse(EnterETbox.Text);
                }

                long[] keys = MathsProcessor.GenerateKeys(p, q, exp);

                OutputNTbox.Text   = keys[0].ToString();
                OutputPhiTbox.Text = keys[1].ToString();
                OutputDTbox.Text   = keys[2].ToString();

                parentForm.EncryptionExponent = exp.ToString();
                parentForm.Q5RsaModulus       = keys[0].ToString();
                parentForm.Q6RsaModulus       = keys[0].ToString();
                parentForm.DecryptionExponent = keys[2].ToString();
            }
            catch (FormatException fe)
            {
                MessageBox.Show("Wrong or missing input. Please fill all input fields in a correct number format.");
            }
            catch (OverflowException oe)
            {
                MessageBox.Show("Entered number(s) is too big.");
            }
        }
Exemplo n.º 13
0
        private void q1CheckPrimeBtn_Click(object sender, EventArgs e)
        {
            try
            {
                long inputNum = long.Parse(q1NumEntryTbx.Text);

                if (MathsProcessor.PrimeCheck(inputNum) == 1)
                {
                    q1ResponseLabel.Text = "The number " + inputNum + " is prime";
                }
                else
                {
                    q1ResponseLabel.Text = "The number " + inputNum + " is NOT prime";
                }
            }
            catch (FormatException formatExcptn)
            {
                MessageBox.Show("Wrong number format. Please enter a correct number.");
            }
            catch (OverflowException oe)
            {
                MessageBox.Show("Entered number(s) is too big.");
            }
        }