Exemplo n.º 1
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()));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create a Subject Public Key Info object for a given public key.
        /// </summary>
        /// <param name="key">One of ElGammalPublicKeyParameters, DSAPublicKeyParameter, DHPublicKeyParameters, RSAKeyParameters or ECPublicKeyParameters</param>
        /// <returns>A subject public key info object.</returns>
        /// <exception cref="Exception">Throw exception if object provided is not one of the above.</exception>
        public static SubjectPublicKeyInfo CreateSubjectPublicKeyInfo(AsymmetricKeyParameter key)
        {
            if (key.isPrivate())
            {
                throw (new Exception("Private key passed - public key expected."));
            }

            if (key is ElGamalPublicKeyParameters)
            {
                ElGamalPublicKeyParameters _key = (ElGamalPublicKeyParameters)key;
                SubjectPublicKeyInfo       info =
                    new SubjectPublicKeyInfo(
                        new AlgorithmIdentifier(
                            OIWObjectIdentifiers.elGamalAlgorithm,
                            new ElGamalParameter(
                                _key.getParameters().getP(),
                                _key.getParameters().getG()
                                ).toASN1Object()), new DERInteger(_key.getY()));

                return(info);
            }


            if (key is DSAPublicKeyParameters)
            {
                DSAPublicKeyParameters _key = (DSAPublicKeyParameters)key;
                SubjectPublicKeyInfo   info =
                    new SubjectPublicKeyInfo(
                        new AlgorithmIdentifier(
                            X9ObjectIdentifiers.id_dsa,
                            new DSAParameter(_key.getParameters().getP(), _key.getParameters().getQ(), _key.getParameters().getG()).toASN1Object()),
                        new DERInteger(_key.getY())
                        );

                return(info);
            }


            if (key is DHPublicKeyParameters)
            {
                DHPublicKeyParameters _key = (DHPublicKeyParameters)key;
                SubjectPublicKeyInfo  info = new SubjectPublicKeyInfo(
                    new AlgorithmIdentifier(X9ObjectIdentifiers.dhpublicnumber,
                                            new DHParameter(_key.getParameters().getP(),
                                                            _key.getParameters().getG(),
                                                            _key.getParameters().getJ()).toASN1Object()), new DERInteger(_key.getY()));

                return(info);
            } // End of DH

            if (key is RSAKeyParameters)
            {
                RSAKeyParameters _key = (RSAKeyParameters)key;
                if (_key.isPrivate())
                {
                    throw (new Exception("Private RSA Key provided."));
                }

                SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, new DERNull()), new RSAPublicKeyStructure(_key.getModulus(), _key.getExponent()).toASN1Object());
                return(info);
            } // End of RSA.

            if (key is ECPublicKeyParameters)
            {
                ECPublicKeyParameters _key = (ECPublicKeyParameters)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);
                ASN1OctetString      p    = (ASN1OctetString)(new X9ECPoint(_key.getQ()).toASN1Object());
                SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, x962.toASN1Object()), p.getOctets());
                return(info);
            } // End of EC

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