コード例 #1
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.");
            }
        }
コード例 #2
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);
        }
コード例 #3
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]);
         }
     }
 }