Beispiel #1
0
 public void Sign(string message)
 {
     BigInteger[] s = RabinSystem.Signature(message, this.pqb);
     this.signedmessage = message;
     this.Signature     = s[0];
     this.n             = s[1];
 }
Beispiel #2
0
        public static void Attack()
        {
            Console.Write("n = ");
            BigInteger n = BigInteger.Parse("0" + Program.ReadLine(), NumberStyles.HexNumber);
            int        number_of_tries = 0;

            while (true)
            {
                number_of_tries++;
                BigInteger t = RabinSystem.RandomBigInteger(n);
                BigInteger y = BigInteger.ModPow(t, 2, n);
                Console.WriteLine("\ny = " + y.ToString("X"));
                Console.Write("\nz = ");
                BigInteger z = BigInteger.Parse("0" + Program.ReadLine(), NumberStyles.HexNumber);
                if ((t == z) || (t == (n - z)))
                {
                    Console.WriteLine("\nDidn't work! I'm trying again...");
                    continue;
                }
                else
                {
                    BigInteger p = RabinSystem.egcd(t + z, n)[0];
                    BigInteger q = n / p;
                    if (p * q == n)
                    {
                        Console.WriteLine("\nVictory!\n\np = {0}\nq = {1}\n\nNumber of tries: {2}", p.ToString("X"), q.ToString("X"), number_of_tries);
                        Console.ReadLine();
                        return;
                    }
                    else
                    {
                        Console.WriteLine("Something went wrong!");
                        continue;
                    }
                }
            }
        }
Beispiel #3
0
        public void Interact()
        {
            Console.WriteLine("Hi! I'm Alice.");

            while (true)
            {
                Console.WriteLine("\nWhat do you want me to do?\n");
                Console.WriteLine("1. Generate private key and publish public key, then decipher a message.");
                Console.WriteLine("2. Get public key and encrypt a message.");
                Console.WriteLine("3. Generate private key and publish public key, then sign a message.");
                Console.WriteLine("4. Get message and public key, then verify the signature.");
                Console.WriteLine("0. Exit.\n");
                Console.Write("Your choice is: ");

                string choice = Console.ReadLine();

                while (choice.Length != 1)
                {
                    Console.WriteLine("Wrong choice! Try again:");
                    choice = Console.ReadLine();
                }

                switch (choice)
                {
                case "1":

                    this.GenKeys();
                    Console.WriteLine("\n\n p = " + this.pqb[0].ToString("X") + "\n\n q = " + this.pqb[1].ToString("X"));
                    Console.WriteLine("n = {0}", this.n.ToString("X"));
                    Console.WriteLine("b = {0}", this.pqb[2].ToString("X"));
                    Console.WriteLine();
                    this.CT = new BigInteger[3];
                    Console.WriteLine("Ciphertext: ");
                    this.CT[0] = BigInteger.Parse("0" + Program.ReadLine(), NumberStyles.HexNumber);
                    Console.WriteLine("Parity: ");
                    this.CT[1] = BigInteger.Parse(Console.ReadLine());
                    Console.WriteLine("Jacobi Symbol: ");
                    this.CT[2]   = BigInteger.Parse(Console.ReadLine());
                    this.message = RabinSystem.Decrypt(this.CT, this.pqb);
                    Console.WriteLine("\nMessage is: {0}", this.message);
                    break;

                case "2":

                    Console.WriteLine("n = ");
                    this.n = BigInteger.Parse("0" + Program.ReadLine(), NumberStyles.HexNumber);
                    Console.WriteLine("b = ");
                    this.b  = BigInteger.Parse("0" + Program.ReadLine(), NumberStyles.HexNumber);
                    this.CT = RabinSystem.Encrypt("Yare, yare daze", this.n, this.b);
                    Console.WriteLine("Encrypted message is: {0}\nRarity is: {1}\nJacobi Symbol is:{2}", this.CT[0].ToString("X"), this.CT[1], this.CT[2]);
                    break;

                case "3":

                    this.GenKeys();
                    Console.Write("\nSigned message is: ");
                    string       message = Console.ReadLine();
                    BigInteger[] sign    = RabinSystem.Signature(message, this.pqb);
                    Console.WriteLine("\nSign is: {0}\n\nModulo is: {1}", sign[0].ToString("X"), sign[1].ToString("X"));
                    break;

                case "4":

                    Console.Write("\nMessage to verify is: ");
                    string signedmessage = Console.ReadLine();
                    Console.Write("\nIts signarute: ");
                    string signarute = Program.ReadLine();
                    Console.Write("\nIts modulo: ");
                    string n = Program.ReadLine();
                    Console.WriteLine("\nSignature is: " + RabinSystem.Verify(signedmessage, signarute, n));
                    break;

                case "0":
                    return;
                }
            }
        }
Beispiel #4
0
 public void ReadMessage()
 {
     this.message = RabinSystem.Decrypt(this.CT, this.pqb);
 }
Beispiel #5
0
 public void SendMessageTo(Users user, string message)
 {
     user.SendPublicKeyTo(this);
     user.CT = RabinSystem.Encrypt(message, this.n, this.b);
 }
Beispiel #6
0
 public void GenKeys()
 {
     this.pqb = RabinSystem.PrimeIntegrsGenerator();
     this.n   = pqb[0] * pqb[1];
 }