Пример #1
0
            public Key(int bitN, Random generator)
            {
                _modBitN = bitN;
                // The public exponent is 2^16 + 1
                _eBitN  = 17;
                _eBytes = new byte[] { 1, 0, 1 };
                int p1BitN = (bitN + 1) / 2
                , p1ByteN = (p1BitN + 7) / 8
                , p1DigitN = (p1BitN + (Digit.BitN - 1)) / Digit.BitN
                , p2BitN = bitN / 2
                , p2ByteN = (p2BitN + 7) / 8
                , p2DigitN = (p2BitN + (Digit.BitN - 1)) / Digit.BitN
                , longerDigitN = p1DigitN > p2DigitN ? p1DigitN : p2DigitN;
                Digits d1 = new Digits(p1DigitN), d2 = new Digits(p2DigitN);

                _modDigits = new Digits(_modDigitN);
                _eDigits   = new Digits(1);
                Digits gcd = new Digits(longerDigitN)
                , temp     = new Digits(longerDigitN);

                Digits.BytesToDigits(_eBytes, 0, _eDigits, _eBitN);
                int[]  pBitN = new int[] { p1BitN, p2BitN };
                int    nPrimeFound = 0;
                Digits p1 = null, p2 = null;

                while (nPrimeFound != 2)
                {
                    int pNowBitN = pBitN[nPrimeFound]
                    , pNowDigitN
                        = (pNowBitN + (Digit.BitN - 1)) / Digit.BitN;
                    Digits pNow = Prime.NewPrime(pNowBitN, generator);
                    if (nPrimeFound == 0)
                    {
                        p1 = pNow;
                    }
                    else
                    {
                        p2 = pNow;
                    }
                    Digits.Sub(pNow, 1, temp, pNowDigitN);
                    int lgcd;
                    Digits.ExtendedGcd(_eDigits
                                       , _eDigitN
                                       , temp
                                       , pNowDigitN
                                       , nPrimeFound == 0 ? d1 : d2
                                       , null
                                       , gcd
                                       , out lgcd);
                    if (Digits.Compare(gcd, 1, lgcd) != 0)
                    {
                        Debug.Assert(false, "untested code");
                        continue;
                    }
                    if (
                        nPrimeFound == 1 &&
                        Digits.Compare(p1, p1DigitN, p2, p2DigitN) == 0
                        )
                    {
                        Debug.Assert(false, "untested code");
                        continue;
                    }
                    nPrimeFound++;
                }
                Digits.Mul(p1, p1DigitN, p2, p2DigitN, _modDigits);
                int modBitN = Digits.SigBitN(_modDigits, _modDigitN);

                Debug.Assert(modBitN == p1BitN + p2BitN && modBitN == _modBitN
                             , "internal error");
                _primeBitN = new int[2] {
                    p1BitN, p2BitN
                };
                _modBytes = new byte[_modByteN];
                _primeBytes
                    = new byte[2][] { new byte[p1ByteN], new byte[p2ByteN] };
                _dBytes
                    = new byte[2][] { new byte[p1ByteN], new byte[p2ByteN] };
                Digits.DigitsToBytes(_modDigits, _modBytes, 0, _modBitN);
                for (int ip = 0; ip != 2; ip++)
                {
                    Digits.DigitsToBytes(
                        ip == 0 ? p1 : p2, _primeBytes[ip], 0, pBitN[ip]);
                    Digits.DigitsToBytes(
                        ip == 0 ? d1 : d2, _dBytes[ip], 0, pBitN[ip]);
                }
                int moduliCreated = 0;

                Digits.BytesToDigits(_eBytes, 0, _eDigits, _eBitN);
                _modulus        = new Modulus(_modDigits, _modDigitN, true);
                _privateModulus = new Modulus[2];
                _dDigits        = new Digits[2];
                _chineseDigits  = new Digits[2];
                for (int ip = 0; ip != 2; ip++)
                {
                    Digits temp2 = new Digits(_modDigitN);
                    _dDigits[ip]       = new Digits(p1DigitN);
                    _chineseDigits[ip] = new Digits(p1DigitN);
                    Digits.BytesToDigits(
                        _primeBytes[ip], 0, temp2, _primeBitN[ip]);
                    _privateModulus[ip]
                        = new
                          Modulus(temp2
                                  , (_primeBitN[ip] + (Digit.BitN - 1)) / Digit.BitN
                                  , true);
                    moduliCreated++;
                    Digits.BytesToDigits(
                        _dBytes[ip], 0, _dDigits[ip], _primeBitN[ip]);
                }
                int    lgcd2 = 0;
                Digits gcd2  = new Digits(_modDigitN);

                Digits.ExtendedGcd(_privateModulus[0]._mod
                                   , p1DigitN
                                   , _privateModulus[1]._mod
                                   , p2DigitN
                                   , _chineseDigits[1]
                                   , _chineseDigits[0]
                                   , gcd2
                                   , out lgcd2);
                if (Digits.Compare(gcd2, 1, lgcd2) != 0)
                {
                    throw new ArgumentException();
                }
            }