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
        /**
         * Process a single block using the basic RSA algorithm.
         *
         * @param in the input array.
         * @param inOff the offset into the input buffer where the data starts.
         * @param inLen the length of the data to be processed.
         * @return the result of the RSA process.
         * @exception DataLengthException the input block is too large.
         */
        public byte[] processBlock(
            byte[]  inBytes,
            int inOff,
            int inLen)
        {
            if (inLen > (getInputBlockSize() + 1))
            {
                throw new DataLengthException("input too large for RSA cipher.\n");
            }
            else if (inLen == (getInputBlockSize() + 1) && (inBytes[inOff] & 0x80) != 0)
            {
                throw new DataLengthException("input too large for RSA cipher.\n");
            }

            byte[] block;

            if (inOff != 0 || inLen != inBytes.Length)
            {
                block = new byte[inLen];

                Array.Copy(inBytes, inOff, block, 0, inLen);
            }
            else
            {
                block = inBytes;
            }

            BigInteger input = new BigInteger(1, block);

            byte[] output;

            if (typeof(RSAPrivateCrtKeyParameters).IsInstanceOfType(key))
            {
                //
                // we have the extra factors, use the Chinese Remainder Theorem - the author
                // wishes to express his thanks to Dirk Bonekaemper at rtsffm.com for
                // advice regarding the expression of this.
                //
                RSAPrivateCrtKeyParameters crtKey = (RSAPrivateCrtKeyParameters)key;

                BigInteger p    = crtKey.getP();
                BigInteger q    = crtKey.getQ();
                BigInteger dP   = crtKey.getDP();
                BigInteger dQ   = crtKey.getDQ();
                BigInteger qInv = crtKey.getQInv();

                BigInteger mP, mQ, h, m;

                // mP = ((input mod p) ^ dP)) mod p
                mP = (input.remainder(p)).modPow(dP, p);

                // mQ = ((input mod q) ^ dQ)) mod q
                mQ = (input.remainder(q)).modPow(dQ, q);

                // h = qInv * (mP - mQ) mod p
                h = mP.subtract(mQ);
                h = h.multiply(qInv);
                h = h.mod(p);                               // mod (in Java) returns the positive residual

                // m = h * q + mQ
                m = h.multiply(q);
                m = m.add(mQ);

                output = m.toByteArray();
            }
            else
            {
                output = input.modPow(
                    key.getExponent(), key.getModulus()).toByteArray();
            }

            if (forEncryption)
            {
                if (output[0] == 0 && output.Length > getOutputBlockSize())                        // have ended up with an extra zero byte, copy down.
                {
                    byte[] tmp = new byte[output.Length - 1];

                    Array.Copy(output, 1, tmp, 0, tmp.Length);

                    return(tmp);
                }

                if (output.Length < getOutputBlockSize())                     // have ended up with less bytes than normal, lengthen
                {
                    byte[] tmp = new byte[getOutputBlockSize()];

                    Array.Copy(output, 0, tmp, tmp.Length - output.Length, output.Length);

                    return(tmp);
                }
            }
            else
            {
                if (output[0] == 0)                        // have ended up with an extra zero byte, copy down.
                {
                    byte[] tmp = new byte[output.Length - 1];

                    Array.Copy(output, 1, tmp, 0, tmp.Length);

                    return(tmp);
                }
            }
            return(output);
        }