// SimpleProgram.cs // using System; using System.Collections.Generic; #if (DEBUG) using System.Diagnostics; #endif // #if (DEBUG) using System.Text; using BroccoliProducts.Maths; namespace SimpleProgram { ///////////////////////////////////////////////////////////////// // declaration of Program class class SimpleProgram { ///////////////////////////////////////////////////////////// // main function static void Main(string[] args) { ///////////////////////////////////////////////////////// // A - Choosing two prime numbers, p and q // declare p, a prime number JumboInteger p = new JumboInteger(17); Console.WriteLine("p = {0}", p); // declare q, a prime number JumboInteger q = new JumboInteger(11); Console.WriteLine("q = {0}", q); ///////////////////////////////////////////////////////// // B - Combing p and q to produce another number N // calculate N = p x q JumboInteger N = p * q; Console.WriteLine("N = {0}", N); ///////////////////////////////////////////////////////// // C - Choose e, between 1 and N // declare e JumboInteger e = new JumboInteger(7); ///////////////////////////////////////////////////////// // D - Publishing the public key // show the public key Console.WriteLine( "Public Key [e,N] = [{0},{1}]", e, N ); ///////////////////////////////////////////////////////// // E - Message "X" is encrypted using the public key // declare message 'X' JumboInteger M = new JumboInteger(88); Console.WriteLine("M = {0}", M); // encrypt the message JumboInteger C = M.PowerMod(e, N); ///////////////////////////////////////////////////////// // F - The encrypted message can now be sent to the receipient // display cypher text Console.WriteLine("C = {0}", C); ///////////////////////////////////////////////////////// // G - Using p and q, create the private key, d // calculate d JumboInteger phi = (p - 1) * (q - 1); JumboInteger d = JumboInteger.ModularInverse(e, phi); // show the public key Console.WriteLine("Private Key [d] = [{0}]", d); ///////////////////////////////////////////////////////// // H - Use the private key to decrypt the cypher-text C // decrypt the message JumboInteger M2 = C.PowerMod(d, N); Console.WriteLine("M2 = {0}", M2); ///////////////////////////////////////////////////////// // application finished Console.WriteLine("\nSimpleProgram finished."); Console.WriteLine("Click any key to close..."); while (!Console.KeyAvailable) { } } } }