Exemplo n.º 1
0
    public static void Main(string[] args)
    {
      // Known problem -> these two pseudoprimes passes my implementation of
      // primality test but failed in JDK's isProbablePrime test.

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

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

      Console.
      WriteLine("List of primes < 2000\n---------------------");
      int limit = 100, count = 0;
      for (int i = 0; i < 2000; i++) {
        if (i >= limit) {
          Console.Error.WriteLine();
          limit += 100;
        }

        BigInteger p = new BigInteger(-i);

        if (p.isProbablePrime()) {
          Console.Write(i + ", ");
          count++;
        }
      }
      Console.Error.WriteLine("\nCount = " + count);


      BigInteger bi1 = new BigInteger(pseudoPrime1);
      Console.Error.WriteLine("\n\nPrimality testing for\n" +
                        bi1.ToString() + "\n");
      Console.Error.WriteLine("SolovayStrassenTest(5) = " +
                        bi1.SolovayStrassenTest(5));
      Console.Error.WriteLine("RabinMillerTest(5) = " +
                        bi1.RabinMillerTest(5));
      Console.Error.WriteLine("FermatLittleTest(5) = " +
                        bi1.FermatLittleTest(5));
      Console.Error.WriteLine("isProbablePrime() = " +
                        bi1.isProbablePrime());
      /* POB: added the above also for pseudoPrime2 to clear compiler warning */
      bi1 = new BigInteger(pseudoPrime2);
      Console.Error.WriteLine("\n\nPrimality testing for\n" +
                        bi1.ToString() + "\n");
      Console.Error.WriteLine("SolovayStrassenTest(5) = " +
                        bi1.SolovayStrassenTest(5));
      Console.Error.WriteLine("RabinMillerTest(5) = " +
                        bi1.RabinMillerTest(5));
      Console.Error.WriteLine("FermatLittleTest(5) = " +
                        bi1.FermatLittleTest(5));
      Console.Error.WriteLine("isProbablePrime() = " +
                        bi1.isProbablePrime());

      Console.Write("\nGenerating 512-bits random pseudoprime. . .");
      Random rand = new Random();
      BigInteger prime = BigInteger.genPseudoPrime(512, 5, rand);
      Console.Error.WriteLine("\n" + prime);

      //int dwStart = System.Environment.TickCount;
      //BigInteger.MulDivTest(100000);
      //BigInteger.RSATest(10);
      //BigInteger.RSATest2(10);
      //Console.Error.WriteLine(System.Environment.TickCount - dwStart);

    }
Exemplo n.º 2
0
 /**
  * Return a byte[] of length MemSize, which holds the integer % Full
  * as a buffer which is a binary representation of an Address
  */
 static public byte[] ConvertToAddressBuffer(BigInteger num)
 {
   byte[] bi_buf;
   
   BigInteger val = num % Full;
   if( val < 0 ) {
     val = val + Full;
   }
   bi_buf = val.getBytes();
   int missing = (MemSize - bi_buf.Length);
   if( missing > 0 ) {
     //Missing some bytes at the beginning, pad with zero :
     byte[] tmp_bi = new byte[Address.MemSize];
     for (int i = 0; i < missing; i++) {
       tmp_bi[i] = (byte) 0;
     }
     System.Array.Copy(bi_buf, 0, tmp_bi, missing,
                       bi_buf.Length);
     bi_buf = tmp_bi;
   }
   else if (missing < 0) {
     throw new System.ArgumentException(
       "Integer too large to fit in 160 bits: " + num.ToString());
   }
   return bi_buf;
 }
Exemplo n.º 3
0
    //***********************************************************************
    // Tests the correct implementation of the modulo exponential function
    // using RSA encryption and decryption (using pre-computed encryption and
    // decryption keys).
    //***********************************************************************

    public static void RSATest(int rounds)
    {
      Random rand = new Random(1);
      byte[] val = new byte[64];

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

      Console.Error.WriteLine("e =\n" + bi_e.ToString(10));
      Console.Error.WriteLine("\nd =\n" + bi_d.ToString(10));
      Console.Error.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
        BigInteger bi_data = new BigInteger(val, t1);
        BigInteger bi_encrypted = bi_data.modPow(bi_e, bi_n);
        BigInteger bi_decrypted = bi_encrypted.modPow(bi_d, bi_n);

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

    }