public DHPrivateKeyParameters(
     BigInteger		x,
     DHParameters	parameters)
     : base(true, parameters)
 {
     this.x = x;
 }
 public DHPrivateKeyParameters(
     BigInteger			x,
     DHParameters		parameters,
     DerObjectIdentifier	algorithmOid)
     : base(true, parameters, algorithmOid)
 {
     this.x = x;
 }
 protected DHKeyParameters(
     bool				isPrivate,
     DHParameters		parameters,
     DerObjectIdentifier	algorithmOid)
     : base(isPrivate)
 {
     // TODO Should we allow parameters to be null?
     this.parameters = parameters;
     this.algorithmOid = algorithmOid;
 }
        public DHPublicKeyParameters(
            BigInteger		y,
            DHParameters	parameters)
            : base(false, parameters)
        {
            if (y == null)
                throw new ArgumentNullException("y");

            this.y = y;
        }
        public DHPublicKeyParameters(
            BigInteger			y,
            DHParameters		parameters,
            DerObjectIdentifier	algorithmOid)
            : base(false, parameters, algorithmOid)
        {
            if (y == null)
                throw new ArgumentNullException("y");

            this.y = y;
        }
        public AsymmetricCipherKeyPair GenerateKeyPair()
        {
            DHKeyGeneratorHelper helper = DHKeyGeneratorHelper.Instance;
            ElGamalParameters egp = param.Parameters;
            DHParameters dhp = new DHParameters(egp.P, egp.G, null, 0, egp.L);

            BigInteger x = helper.CalculatePrivate(dhp, param.Random);
            BigInteger y = helper.CalculatePublic(dhp, x);

            return new AsymmetricCipherKeyPair(
                new ElGamalPublicKeyParameters(y, egp),
                new ElGamalPrivateKeyParameters(x, egp));
        }
        public virtual void Init(
            ICipherParameters parameters)
        {
            if (parameters is ParametersWithRandom)
            {
                parameters = ((ParametersWithRandom) parameters).Parameters;
            }

            if (!(parameters is DHPrivateKeyParameters))
            {
                throw new ArgumentException("DHEngine expects DHPrivateKeyParameters");
            }

            this.key = (DHPrivateKeyParameters) parameters;
            this.dhParams = key.Parameters;
        }
        internal BigInteger CalculatePrivate(
            DHParameters	dhParams,
            SecureRandom	random)
        {
            int limit = dhParams.L;

            if (limit != 0)
            {
                int minWeight = limit >> 2;
                for (;;)
                {
                    BigInteger x = new BigInteger(limit, random).SetBit(limit - 1);
                    if (WNafUtilities.GetNafWeight(x) >= minWeight)
                    {
                        return x;
                    }
                }
            }

            BigInteger min = BigInteger.Two;
            int m = dhParams.M;
            if (m != 0)
            {
                min = BigInteger.One.ShiftLeft(m - 1);
            }

            BigInteger q = dhParams.Q;
            if (q == null)
            {
                q = dhParams.P;
            }
            BigInteger max = q.Subtract(BigInteger.Two);

            {
                int minWeight = max.BitLength >> 2;
                for (;;)
                {
                    BigInteger x = BigIntegers.CreateRandomInRange(min, max, random);
                    if (WNafUtilities.GetNafWeight(x) >= minWeight)
                    {
                        return x;
                    }
                }
            }
        }
        private static DHPublicKeyParameters ReadPkcsDHParam(DerObjectIdentifier algOid,
            BigInteger y, Asn1Sequence seq)
        {
            DHParameter para = new DHParameter(seq);

            BigInteger lVal = para.L;
            int l = lVal == null ? 0 : lVal.IntValue;
            DHParameters dhParams = new DHParameters(para.P, para.G, null, l);

            return new DHPublicKeyParameters(y, dhParams, algOid);
        }
 internal BigInteger CalculatePublic(
     DHParameters	dhParams,
     BigInteger		x)
 {
     return dhParams.G.ModPow(x, dhParams.P);
 }
예제 #11
0
 protected bool Equals(
     DHParameters other)
 {
     return p.Equals(other.p)
         && g.Equals(other.g)
         && Platform.Equals(q, other.q);
 }
예제 #12
0
        public void Init(
            ICipherParameters parameters)
        {
            AsymmetricKeyParameter kParam;
            if (parameters is ParametersWithRandom)
            {
                ParametersWithRandom rParam = (ParametersWithRandom)parameters;

                this.random = rParam.Random;
                kParam = (AsymmetricKeyParameter)rParam.Parameters;
            }
            else
            {
                this.random = new SecureRandom();
                kParam = (AsymmetricKeyParameter)parameters;
            }

            if (!(kParam is DHPrivateKeyParameters))
            {
                throw new ArgumentException("DHEngine expects DHPrivateKeyParameters");
            }

            this.key = (DHPrivateKeyParameters)kParam;
            this.dhParams = key.Parameters;
        }
 protected DHKeyParameters(
     bool			isPrivate,
     DHParameters	parameters)
     : this(isPrivate, parameters, PkcsObjectIdentifiers.DhKeyAgreement)
 {
 }