Exemplo n.º 1
0
        /**
         * Initialise the decryptor.
         *
         * @param publicKey      the recipient's/sender's public key parameters
         * @param params         encoding and derivation parameters, may be wrapped to include an IV for an underlying block cipher.
         * @param ephemeralKeyPairGenerator             the ephemeral key pair generator to use.
         */
        public void Init(AsymmetricKeyParameter publicKey, ICipherParameters @params,
                         EphemeralKeyPairGenerator ephemeralKeyPairGenerator)
        {
            ForEncryption    = true;
            PubParam         = publicKey;
            KeyPairGenerator = ephemeralKeyPairGenerator;

            ExtractParams(@params);
        }
Exemplo n.º 2
0
        public byte[] DoFinal(byte[] input, int inOff, int inLen)
        {
            if (inLen != 0)
            {
                _buffer.Write(input, inOff, inLen);
            }

            byte[] @in = _buffer.ToArray();
            _buffer.Flush();
            _buffer.SetLength(0);

            // Convert parameters for use in IESEngine
            ICipherParameters @params = new IesWithCipherParameters(_engineSpec.GetDerivationV(),
                                                                    _engineSpec.GetEncodingV(),
                                                                    _engineSpec.GetMacKeySize(),
                                                                    _engineSpec.GetCipherKeySize());

            if (_engineSpec.GetNonce() != null)
            {
                @params = new ParametersWithIV(@params, _engineSpec.GetNonce());
            }

            ECDomainParameters ecParams = ((ECKeyParameters)_key).Parameters;

            if (_forEncryption)
            {
                // Generate the ephemeral key pair
                ECKeyPairGenerator gen = new ECKeyPairGenerator();
                gen.Init(new ECKeyGenerationParameters(ecParams, _random));

                bool usePointCompression = _engineSpec.GetPointCompression();
                EphemeralKeyPairGenerator
                    kGen = new EphemeralKeyPairGenerator(gen, new KeyEncoder(usePointCompression));

                // Encrypt the buffer
                try
                {
                    _customIesEngine.Init(_key, @params, kGen);

                    return(_customIesEngine.ProcessBlock(@in, 0, @in.Length));
                }
                catch (Exception e)
                {
                    throw new DataException("unable to process block", e);
                }
            }
            else
            {
                // Decrypt the buffer
                try
                {
                    _customIesEngine.Init(_key, @params, new EciesPublicKeyParser(ecParams));

                    return(_customIesEngine.ProcessBlock(@in, 0, @in.Length));
                }
                catch (InvalidCipherTextException e)
                {
                    throw new DataException("unable to process block", e);
                }
            }
        }