コード例 #1
0
        /**
         * given a message from a given party and the coresponding public key
         * calculate the next message in the agreement sequence. In this case
         * this will represent the shared secret.
         */
        public BigInteger calculateAgreement(
            DHPublicKeyParameters pub,
            BigInteger message)
        {
            if (!pub.getParameters().Equals(dhParams))
            {
                throw new ArgumentException("Diffie-Hellman public key has wrong parameters.");
            }

            return(message.modPow(key.getX(), dhParams.getP()).multiply(pub.getY().modPow(privateValue, dhParams.getP())).mod(dhParams.getP()));
        }
コード例 #2
0
        /**
         * given a short term public key from a given party calculate the next
         * message in the agreement sequence.
         */
        public BigInteger calculateAgreement(
            CipherParameters pubKey)
        {
            DHPublicKeyParameters pub = (DHPublicKeyParameters)pubKey;

            if (!pub.getParameters().Equals(dhParams))
            {
                throw new ArgumentException("Diffie-Hellman public key has wrong parameters.");
            }

            return(pub.getY().modPow(key.getX(), dhParams.getP()));
        }
コード例 #3
0
        public static PrivateKeyInfo createPrivateKeyInfo(AsymmetricKeyParameter key)
        {
            /*
             *  Process DH private key.
             *  The value for L was set to zero implicitly.
             *  This is the same action as found in JCEDHPrivateKey getEncoded method.
             */

            if (key is ElGamalPrivateKeyParameters)
            {
                ElGamalPrivateKeyParameters _key = (ElGamalPrivateKeyParameters)key;
                PrivateKeyInfo info =
                    new PrivateKeyInfo(
                        new AlgorithmIdentifier(
                            OIWObjectIdentifiers.elGamalAlgorithm,
                            new ElGamalParameter(
                                _key.getParameters().getP(),
                                _key.getParameters().getG()).toASN1Object()), new DERInteger(_key.getX()));
                return(info);
            }


            if (key is DSAPrivateKeyParameters)
            {
                DSAPrivateKeyParameters _key = (DSAPrivateKeyParameters)key;
                PrivateKeyInfo          info =
                    new PrivateKeyInfo(
                        new AlgorithmIdentifier(
                            X9ObjectIdentifiers.id_dsa,
                            new DSAParameter(
                                _key.getParameters().getP(),
                                _key.getParameters().getQ(),
                                _key.getParameters().getG()).toASN1Object()), new DERInteger(_key.getX()));

                return(info);
            }


            if (key is DHPrivateKeyParameters)
            {
                DHPrivateKeyParameters _key = (DHPrivateKeyParameters)key;


                PrivateKeyInfo info = new PrivateKeyInfo(
                    new AlgorithmIdentifier(
                        PKCSObjectIdentifiers.dhKeyAgreement, new DHParameter(
                            _key.getParameters().getP(),
                            _key.getParameters().getG(),
                            0
                            ).toASN1Object()
                        )
                    , new DERInteger(_key.getX()));

                return(info);
            }


            if (key is RSAPrivateCrtKeyParameters)
            {
                RSAPrivateCrtKeyParameters _key = (RSAPrivateCrtKeyParameters)key;
                PrivateKeyInfo             info = new PrivateKeyInfo(
                    new AlgorithmIdentifier(
                        PKCSObjectIdentifiers.rsaEncryption, new DERNull()),
                    new RSAPrivateKeyStructure(
                        _key.getModulus(),
                        _key.getPublicExponent(),
                        _key.getExponent(),
                        _key.getP(),
                        _key.getQ(),
                        _key.getDP(),
                        _key.getDQ(),
                        _key.getQInv()).toASN1Object());

                return(info);
            }

            if (key is ECPrivateKeyParameters)
            {
                ECPrivateKeyParameters _key = (ECPrivateKeyParameters)key;

                X9ECParameters ecP = new X9ECParameters(
                    _key.getParameters().getCurve(),
                    _key.getParameters().getG(),
                    _key.getParameters().getN(),
                    _key.getParameters().getH(),
                    _key.getParameters().getSeed());
                X962Parameters x962 = new X962Parameters(ecP);


                PrivateKeyInfo info = new PrivateKeyInfo(
                    new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, x962.toASN1Object()),
                    new ECPrivateKeyStructure(_key.getD()).toASN1Object());


                return(info);
            }

            throw (new Exception("Class provided is not convertable:" + key.GetType()));
        }