public static void KeyGen(int key_size, out BigInteger oe, out BigInteger od, out BigInteger on)
        {
            RandomBigInteger rand = new RandomBigInteger();
            BigInteger       p    = rand.NextBigInteger(key_size / 2);

            p = p.GetNextPrime();

            int        qBitLen = key_size - p.BitLength();
            BigInteger q, n;

            do
            {
                q = rand.NextBigInteger(qBitLen);
                q = q.GetNextPrime();
                n = p * q;
                qBitLen++;
            } while (n.BitLength() < key_size + 1);

            BigInteger phi = (p - 1) * (q - 1);
            BigInteger e   = 3;

            while (BigInteger.GreatestCommonDivisor(e, phi) != 1)
            {
                e += 2;
            }

            BigInteger d = (e.ModInverse(phi));

            oe = e;
            od = d;
            on = n;
        }
Exemple #2
0
        private static BigInteger createModel(byte[] owner, string properties, string hash)
        {
            StorageContext ctx = Storage.CurrentContext;

            Model model = new Model();

            model.owner      = owner;
            model.properties = properties;
            model.hash       = hash;

            int seed             = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            RandomBigInteger rnd = new RandomBigInteger(seed);
            BigInteger       id  = rnd.NextBigInteger(63);

            while (Storage.Get(ctx, concatKey("models/", id)).Length != 0)
            {
                id = rnd.NextBigInteger(63);
            }
            model.id = id;
            byte[] key = concatKey("models/", id);

            byte[] obj = Model.ToByteArray(model);

            Storage.Put(ctx, key, obj);
            return(id);
        }
        BigInteger RandomWithGCD1(BigInteger min, BigInteger max)
        {
            RandomBigInteger rbi   = new RandomBigInteger();
            BigInteger       value = 0;

            do
            {
                value = rbi.NextBigInteger(min, max);
                BigInteger a = BigInteger.GreatestCommonDivisor(value, max);
            } while (BigInteger.GreatestCommonDivisor(value, max) != 1);
            return(value);
        }