Пример #1
0
        /**
         * Process a single block using the basic RSA algorithm.
         *
         * @param inBuf 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 virtual byte[] ProcessBlock(
            byte[]      inBuf,
            int inOff,
            int inLen)
        {
            if (core == null)
            {
                throw new InvalidOperationException("RSA engine not initialised");
            }

            return(core.ConvertOutput(core.ProcessBlock(core.ConvertInput(inBuf, inOff, inLen))));
        }
Пример #2
0
        /**
         * Process a single block using the basic RSA algorithm.
         *
         * @param inBuf 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 virtual byte[] ProcessBlock(
            byte[] inBuf,
            int inOff,
            int inLen)
        {
            if (key == null)
            {
                throw new InvalidOperationException("RSA engine not initialised");
            }

            BigInteger input = core.ConvertInput(inBuf, inOff, inLen);

            BigInteger result;

            if (key is RsaPrivateCrtKeyParameters)
            {
                RsaPrivateCrtKeyParameters k = (RsaPrivateCrtKeyParameters)key;
                BigInteger e = k.PublicExponent;
                if (e != null)   // can't do blinding without a public exponent
                {
                    BigInteger m = k.Modulus;
                    BigInteger r = BigIntegers.CreateRandomInRange(
                        BigInteger.One, m.Subtract(BigInteger.One), random);

                    BigInteger blindedInput  = r.ModPow(e, m).Multiply(input).Mod(m);
                    BigInteger blindedResult = core.ProcessBlock(blindedInput);

                    BigInteger rInv = r.ModInverse(m);
                    result = blindedResult.Multiply(rInv).Mod(m);

                    // defence against Arjen Lenstra’s CRT attack
                    if (!input.Equals(result.ModPow(e, m)))
                    {
                        throw new InvalidOperationException("RSA engine faulty decryption/signing detected");
                    }
                }
                else
                {
                    result = core.ProcessBlock(input);
                }
            }
            else
            {
                result = core.ProcessBlock(input);
            }

            return(core.ConvertOutput(result));
        }
Пример #3
0
        /**
         * Process a single block using the RSA blinding 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.
         * @throws DataLengthException the input block is too large.
         */
        public virtual byte[] ProcessBlock(
            byte[]  inBuf,
            int inOff,
            int inLen)
        {
            BigInteger msg = core.ConvertInput(inBuf, inOff, inLen);

            if (forEncryption)
            {
                msg = BlindMessage(msg);
            }
            else
            {
                msg = UnblindMessage(msg);
            }

            return(core.ConvertOutput(msg));
        }