Пример #1
0
 //функция вычисления квадратоного корня по модулю простого числа q
 public BInteger ModSqrt(BInteger a, BInteger q)
 {
     BInteger b = new BInteger();
     do
     {
         b.genRandomBits(255, new Random());
     } while (Legendre(b, q) == 1);
     BInteger s = 0;
     BInteger t = q - 1;
     while ((t & 1) != 1)
     {
         s++;
         t = t >> 1;
     }
     BInteger InvA = a.modInverse(q);
     BInteger c = b.modPow(t, q);
     BInteger r = a.modPow(((t + 1) / 2), q);
     BInteger d = new BInteger();
     for (int i = 1; i < s; i++)
     {
         BInteger temp = 2;
         temp = temp.modPow((s - i - 1), q);
         d = (r.modPow(2, q) * InvA).modPow(temp, q);
         if (d == (q - 1))
             r = (r * c) % q;
         c = c.modPow(2, q);
     }
     return r;
 }
Пример #2
0
 //Вычисляем символ Лежандра
 public BInteger Legendre(BInteger a, BInteger q)
 {
     return a.modPow((q - 1) / 2, q);
 }
Пример #3
0
        public static void RSATest2(int rounds)
        {
            Random rand = new Random();
            byte[] val = new byte[64];

            byte[] pseudoPrime1 = {
                        0x85, 0x84, 0x64, 0xFD, 0x70, 0x6A,
                        0x9F, 0xF0, 0x94, 0x0C, 0x3E, 0x2C,
                        0x74, 0x34, 0x05, 0xC9, 0x55, 0xB3,
                        0x85, 0x32, 0x98, 0x71, 0xF9, 0x41,
                        0x21, 0x5F, 0x02, 0x9E, 0xEA, 0x56,
                        0x8D, 0x8C, 0x44, 0xCC, 0xEE, 0xEE,
                        0x3D, 0x2C, 0x9D, 0x2C, 0x12, 0x41,
                        0x1E, 0xF1, 0xC5, 0x32, 0xC3, 0xAA,
                        0x31, 0x4A, 0x52, 0xD8, 0xE8, 0xAF,
                        0x42, 0xF4, 0x72, 0xA1, 0x2A, 0x0D,
                        0x97, 0xB1, 0x31, 0xB3,
                };

            byte[] pseudoPrime2 = {
                        0x99, 0x98, 0xCA, 0xB8, 0x5E, 0xD7,
                        0xE5, 0xDC, 0x28, 0x5C, 0x6F, 0x0E,
                        0x15, 0x09, 0x59, 0x6E, 0x84, 0xF3,
                        0x81, 0xCD, 0xDE, 0x42, 0xDC, 0x93,
                        0xC2, 0x7A, 0x62, 0xAC, 0x6C, 0xAF,
                        0xDE, 0x74, 0xE3, 0xCB, 0x60, 0x20,
                        0x38, 0x9C, 0x21, 0xC3, 0xDC, 0xC8,
                        0xA2, 0x4D, 0xC6, 0x2A, 0x35, 0x7F,
                        0xF3, 0xA9, 0xE8, 0x1D, 0x7B, 0x2C,
                        0x78, 0xFA, 0xB8, 0x02, 0x55, 0x80,
                        0x9B, 0xC2, 0xA5, 0xCB,
                };


            BInteger bi_p = new BInteger(pseudoPrime1);
            BInteger bi_q = new BInteger(pseudoPrime2);
            BInteger bi_pq = (bi_p - 1) * (bi_q - 1);
            BInteger bi_n = bi_p * bi_q;

            for (int count = 0; count < rounds; count++)
            {
                // generate private and public key
                BInteger bi_e = bi_pq.genCoPrime(512, rand);
                BInteger bi_d = bi_e.modInverse(bi_pq);

                Console.WriteLine("\ne =\n" + bi_e.ToString(10));
                Console.WriteLine("\nd =\n" + bi_d.ToString(10));
                Console.WriteLine("\nn =\n" + bi_n.ToString(10) + "\n");

                // generate data of random length
                int t1 = 0;
                while (t1 == 0)
                    t1 = (int)(rand.NextDouble() * 65);

                bool done = false;
                while (!done)
                {
                    for (int i = 0; i < 64; i++)
                    {
                        if (i < t1)
                            val[i] = (byte)(rand.NextDouble() * 256);
                        else
                            val[i] = 0;

                        if (val[i] != 0)
                            done = true;
                    }
                }

                while (val[0] == 0)
                    val[0] = (byte)(rand.NextDouble() * 256);

                Console.Write("Round = " + count);

                // encrypt and decrypt data
                BInteger bi_data = new BInteger(val, t1);
                BInteger bi_encrypted = bi_data.modPow(bi_e, bi_n);
                BInteger bi_decrypted = bi_encrypted.modPow(bi_d, bi_n);

                // compare
                if (bi_decrypted != bi_data)
                {
                    Console.WriteLine("\nError at round " + count);
                    Console.WriteLine(bi_data + "\n");
                    return;
                }
                Console.WriteLine(" <PASSED>.");
            }

        }
Пример #4
0
        public static void RSATest(int rounds)
        {
            Random rand = new Random(1);
            byte[] val = new byte[64];

            // private and public key
            BInteger bi_e = new BInteger("a932b948feed4fb2b692609bd22164fc9edb59fae7880cc1eaff7b3c9626b7e5b241c27a974833b2622ebe09beb451917663d47232488f23a117fc97720f1e7", 16);
            BInteger bi_d = new BInteger("4adf2f7a89da93248509347d2ae506d683dd3a16357e859a980c4f77a4e2f7a01fae289f13a851df6e9db5adaa60bfd2b162bbbe31f7c8f828261a6839311929d2cef4f864dde65e556ce43c89bbbf9f1ac5511315847ce9cc8dc92470a747b8792d6a83b0092d2e5ebaf852c85cacf34278efa99160f2f8aa7ee7214de07b7", 16);
            BInteger bi_n = new BInteger("e8e77781f36a7b3188d711c2190b560f205a52391b3479cdb99fa010745cbeba5f2adc08e1de6bf38398a0487c4a73610d94ec36f17f3f46ad75e17bc1adfec99839589f45f95ccc94cb2a5c500b477eb3323d8cfab0c8458c96f0147a45d27e45a4d11d54d77684f65d48f15fafcc1ba208e71e921b9bd9017c16a5231af7f", 16);

            Console.WriteLine("e =\n" + bi_e.ToString(10));
            Console.WriteLine("\nd =\n" + bi_d.ToString(10));
            Console.WriteLine("\nn =\n" + bi_n.ToString(10) + "\n");

            for (int count = 0; count < rounds; count++)
            {
                // generate data of random length
                int t1 = 0;
                while (t1 == 0)
                    t1 = (int)(rand.NextDouble() * 65);

                bool done = false;
                while (!done)
                {
                    for (int i = 0; i < 64; i++)
                    {
                        if (i < t1)
                            val[i] = (byte)(rand.NextDouble() * 256);
                        else
                            val[i] = 0;

                        if (val[i] != 0)
                            done = true;
                    }
                }

                while (val[0] == 0)
                    val[0] = (byte)(rand.NextDouble() * 256);

                Console.Write("Round = " + count);

                // encrypt and decrypt data
                BInteger bi_data = new BInteger(val, t1);
                BInteger bi_encrypted = bi_data.modPow(bi_e, bi_n);
                BInteger bi_decrypted = bi_encrypted.modPow(bi_d, bi_n);

                // compare
                if (bi_decrypted != bi_data)
                {
                    Console.WriteLine("\nError at round " + count);
                    Console.WriteLine(bi_data + "\n");
                    return;
                }
                Console.WriteLine(" <PASSED>.");
            }

        }
Пример #5
0
        public bool SolovayStrassenTest(int confidence)
        {
            BInteger thisVal;
            if ((this.data[maxLength - 1] & 0x80000000) != 0)        // negative
                thisVal = -this;
            else
                thisVal = this;

            if (thisVal.dataLength == 1)
            {
                // test small numbers
                if (thisVal.data[0] == 0 || thisVal.data[0] == 1)
                    return false;
                else if (thisVal.data[0] == 2 || thisVal.data[0] == 3)
                    return true;
            }

            if ((thisVal.data[0] & 0x1) == 0)     // even numbers
                return false;


            int bits = thisVal.bitCount();
            BInteger a = new BInteger();
            BInteger p_sub1 = thisVal - 1;
            BInteger p_sub1_shift = p_sub1 >> 1;

            Random rand = new Random();

            for (int round = 0; round < confidence; round++)
            {
                bool done = false;

                while (!done)       // generate a < n
                {
                    int testBits = 0;

                    // make sure "a" has at least 2 bits
                    while (testBits < 2)
                        testBits = (int)(rand.NextDouble() * bits);

                    a.genRandomBits(testBits, rand);

                    int byteLen = a.dataLength;

                    // make sure "a" is not 0
                    if (byteLen > 1 || (byteLen == 1 && a.data[0] != 1))
                        done = true;
                }

                // check whether a factor exists (fix for version 1.03)
                BInteger gcdTest = a.gcd(thisVal);
                if (gcdTest.dataLength == 1 && gcdTest.data[0] != 1)
                    return false;

                // calculate a^((p-1)/2) mod p

                BInteger expResult = a.modPow(p_sub1_shift, thisVal);
                if (expResult == p_sub1)
                    expResult = -1;

                // calculate Jacobi symbol
                BInteger jacob = Jacobi(a, thisVal);

                //Console.WriteLine("a = " + a.ToString(10) + " b = " + thisVal.ToString(10));
                //Console.WriteLine("expResult = " + expResult.ToString(10) + " Jacob = " + jacob.ToString(10));

                // if they are different then it is not prime
                if (expResult != jacob)
                    return false;
            }

            return true;
        }
Пример #6
0
        public bool RabinMillerTest(int confidence)
        {
            BInteger thisVal;
            if ((this.data[maxLength - 1] & 0x80000000) != 0)        // negative
                thisVal = -this;
            else
                thisVal = this;

            if (thisVal.dataLength == 1)
            {
                // test small numbers
                if (thisVal.data[0] == 0 || thisVal.data[0] == 1)
                    return false;
                else if (thisVal.data[0] == 2 || thisVal.data[0] == 3)
                    return true;
            }

            if ((thisVal.data[0] & 0x1) == 0)     // even numbers
                return false;


            // calculate values of s and t
            BInteger p_sub1 = thisVal - (new BInteger(1));
            int s = 0;

            for (int index = 0; index < p_sub1.dataLength; index++)
            {
                uint mask = 0x01;

                for (int i = 0; i < 32; i++)
                {
                    if ((p_sub1.data[index] & mask) != 0)
                    {
                        index = p_sub1.dataLength;      // to break the outer loop
                        break;
                    }
                    mask <<= 1;
                    s++;
                }
            }

            BInteger t = p_sub1 >> s;

            int bits = thisVal.bitCount();
            BInteger a = new BInteger();
            Random rand = new Random();

            for (int round = 0; round < confidence; round++)
            {
                bool done = false;

                while (!done)       // generate a < n
                {
                    int testBits = 0;

                    // make sure "a" has at least 2 bits
                    while (testBits < 2)
                        testBits = (int)(rand.NextDouble() * bits);

                    a.genRandomBits(testBits, rand);

                    int byteLen = a.dataLength;

                    // make sure "a" is not 0
                    if (byteLen > 1 || (byteLen == 1 && a.data[0] != 1))
                        done = true;
                }

                // check whether a factor exists (fix for version 1.03)
                BInteger gcdTest = a.gcd(thisVal);
                if (gcdTest.dataLength == 1 && gcdTest.data[0] != 1)
                    return false;

                BInteger b = a.modPow(t, thisVal);


                bool result = false;

                if (b.dataLength == 1 && b.data[0] == 1)         // a^t mod p = 1
                    result = true;

                for (int j = 0; result == false && j < s; j++)
                {
                    if (b == p_sub1)         // a^((2^j)*t) mod p = p-1 for some 0 <= j <= s-1
                    {
                        result = true;
                        break;
                    }

                    b = (b * b) % thisVal;
                }

                if (result == false)
                    return false;
            }
            return true;
        }
Пример #7
0
        public bool FermatLittleTest(int confidence)
        {
            BInteger thisVal;
            if ((this.data[maxLength - 1] & 0x80000000) != 0)        // negative
                thisVal = -this;
            else
                thisVal = this;

            if (thisVal.dataLength == 1)
            {
                // test small numbers
                if (thisVal.data[0] == 0 || thisVal.data[0] == 1)
                    return false;
                else if (thisVal.data[0] == 2 || thisVal.data[0] == 3)
                    return true;
            }

            if ((thisVal.data[0] & 0x1) == 0)     // even numbers
                return false;

            int bits = thisVal.bitCount();
            BInteger a = new BInteger();
            BInteger p_sub1 = thisVal - (new BInteger(1));
            Random rand = new Random();

            for (int round = 0; round < confidence; round++)
            {
                bool done = false;

                while (!done)       // generate a < n
                {
                    int testBits = 0;

                    // make sure "a" has at least 2 bits
                    while (testBits < 2)
                        testBits = (int)(rand.NextDouble() * bits);

                    a.genRandomBits(testBits, rand);

                    int byteLen = a.dataLength;

                    // make sure "a" is not 0
                    if (byteLen > 1 || (byteLen == 1 && a.data[0] != 1))
                        done = true;
                }

                // check whether a factor exists (fix for version 1.03)
                BInteger gcdTest = a.gcd(thisVal);
                if (gcdTest.dataLength == 1 && gcdTest.data[0] != 1)
                    return false;

                // calculate a^(p-1) mod p
                BInteger expResult = a.modPow(p_sub1, thisVal);

                int resultLen = expResult.dataLength;

                // is NOT prime is a^(p-1) mod p != 1

                if (resultLen > 1 || (resultLen == 1 && expResult.data[0] != 1))
                {
                    //Console.WriteLine("a = " + a.ToString());
                    return false;
                }
            }

            return true;
        }