Пример #1
0
        /**
         * Return the maximum size for an input block to this engine.
         * For RSA this is always one byte less than the key size on
         * encryption, and the same length as the key size on decryption.
         *
         * @return maximum size for an input block.
         */
        public int getInputBlockSize()
        {
            int bitSize = key.getModulus().bitLength();

            if (forEncryption)
            {
                return((bitSize + 7) / 8 - 1);
            }
            else
            {
                return((bitSize + 7) / 8);
            }
        }
Пример #2
0
        public virtual void  init(bool forSigning, CipherParameters param)
        {
            RSAKeyParameters kParam = null;

            if (param is ParametersWithRandom)
            {
                ParametersWithRandom p = (ParametersWithRandom)param;

                kParam = (RSAKeyParameters)p.getParameters();
                random = p.getRandom();
            }
            else
            {
                kParam = (RSAKeyParameters)param;
                if (forSigning)
                {
                    random = new SecureRandom();
                }
            }

            cipher.init(forSigning, kParam);

            emBits = kParam.getModulus().bitLength() - 1;

            block = new byte[(emBits + 7) / 8];

            reset();
        }
Пример #3
0
        public ScrambledKeyPair(AsymmetricCipherKeyPair pPair)
        {
            _pair = pPair;
            AsymmetricKeyParameter publicKey        = pPair.getPublic();
            RSAKeyParameters       rsaKeyParameters = publicKey as RSAKeyParameters;

            if (rsaKeyParameters != null)
            {
                ScrambledModulus = ScrambleModulus(rsaKeyParameters.getModulus());
            }
            PrivateKey = pPair.getPrivate();
        }
Пример #4
0
        public virtual void  init(bool forSigning, CipherParameters param)
        {
            RSAKeyParameters kParam = (RSAKeyParameters)param;

            cipher.init(forSigning, kParam);

            keyBits = kParam.getModulus().bitLength();

            block = new byte[(keyBits + 7) / 8];
            mBuf  = new byte[block.Length - 1];

            reset();
        }
Пример #5
0
        /// <summary> Initialise the signer.
        ///
        /// </summary>
        /// <param name="forSigning">true if for signing, false if for verification.
        /// </param>
        /// <param name="param">parameters for signature generation/verification. If the
        /// parameters are for generation they should be a ParametersWithRandom,
        /// a ParametersWithSalt, or just an RSAKeyParameters object. If RSAKeyParameters
        /// are passed in a SecureRandom will be created.
        /// </param>
        /// <exception cref="ArgumentException"> IllegalArgumentException if wrong parameter type or a fixed
        /// salt is passed in which is the wrong length.
        /// </exception>
        public virtual void  init(bool forSigning, CipherParameters param)
        {
            RSAKeyParameters kParam = null;
            int lengthOfSalt        = saltLength;

            if (param is ParametersWithRandom)
            {
                ParametersWithRandom p = (ParametersWithRandom)param;

                kParam = (RSAKeyParameters)p.getParameters();
                random = p.getRandom();
            }
            else if (param is ParametersWithSalt)
            {
                ParametersWithSalt p = (ParametersWithSalt)param;

                kParam       = (RSAKeyParameters)p.getParameters();
                standardSalt = p.getSalt();
                lengthOfSalt = standardSalt.Length;
            }
            else
            {
                kParam = (RSAKeyParameters)param;
                if (forSigning)
                {
                    random = new SecureRandom();
                }
            }

            cipher.init(forSigning, kParam);

            keyBits = kParam.getModulus().bitLength();

            block = new byte[(keyBits + 7) / 8];

            if (trailer == TRAILER_IMPLICIT)
            {
                mBuf = new byte[block.Length - digest.getDigestSize() - lengthOfSalt - 1 - 1];
            }
            else
            {
                mBuf = new byte[block.Length - digest.getDigestSize() - lengthOfSalt - 1 - 2];
            }

            reset();
        }
Пример #6
0
        public void init(
            bool forEncryption,
            CipherParameters param)
        {
            RSAKeyParameters kParam = null;

            if (typeof(ParametersWithRandom).IsInstanceOfType(param))
            {
                ParametersWithRandom rParam = (ParametersWithRandom)param;

                kParam = (RSAKeyParameters)rParam.getParameters();
            }
            else
            {
                kParam = (RSAKeyParameters)param;
            }

            engine.init(forEncryption, kParam);

            bitSize = kParam.getModulus().bitLength();

            this.forEncryption = forEncryption;
        }
Пример #7
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()));
        }