public virtual byte[] ProcessBlock(
            byte[] @in,
            int inOff,
            int inLen)
        {
            if (ForEncryption)
            {
                if (KeyPairGenerator != null)
                {
                    EphemeralKeyPair ephKeyPair = KeyPairGenerator.Generate();

                    PrivParam = ephKeyPair.GetKeyPair().Private;
                    V         = ephKeyPair.GetEncodedPublicKey();
                }
            }
            else
            {
                if (KeyParser != null)
                {
                    MemoryStream bIn = new MemoryStream(@in, inOff, inLen)
                    {
                        Position = 0
                    };
                    try
                    {
                        PubParam = KeyParser.ReadKey(bIn);
                    }
                    catch (IOException e)
                    {
                        throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.Message, e);
                    }
                    catch (ArgumentException e)
                    {
                        throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.Message, e);
                    }

                    int encLength = (inLen - (int)(bIn.Length - bIn.Position));
                    V = Arrays.CopyOfRange(@in, inOff, inOff + encLength);
                }
            }

            // Compute the common value and convert to byte array.
            Agree.Init(PrivParam);
            BigInteger z = Agree.CalculateAgreement(PubParam);

            byte[] bigZ = BigIntegers.AsUnsignedByteArray(Agree.GetFieldSize(), z);

            // Create input to KDF.
            if (V.Length != 0)
            {
                byte[] vz = Arrays.Concatenate(V, bigZ);
                Arrays.Fill(bigZ, 0);
                bigZ = vz;
            }

            try
            {
                // Initialise the KDF.
                KdfParameters kdfParam = new KdfParameters(bigZ, _param.GetDerivationV());
                Kdf.Init(kdfParam);

                return(ForEncryption
                    ? EncryptBlock(@in, inOff, inLen)
                    : DecryptBlock(@in, inOff, inLen));
            }
            finally
            {
                Arrays.Fill(bigZ, 0);
            }
        }
Beispiel #2
0
        public override byte[] ProcessBlock(
            byte[] @in,
            int inOff,
            int inLen)
        {
            if (ForEncryption)
            {
                if (KeyPairGenerator != null)
                {
                    EphemeralKeyPair ephKeyPair = KeyPairGenerator.Generate();

                    PrivParam = ephKeyPair.GetKeyPair().Private;
                    V         = ephKeyPair.GetEncodedPublicKey();
                }
            }
            else
            {
                if (KeyParser != null)
                {
                    MemoryStream bIn = new MemoryStream(@in, inOff, inLen)
                    {
                        Position = SecureHeadSize
                    };
                    try
                    {
                        PubParam = KeyParser.ReadKey(bIn);
                    }
                    catch (IOException e)
                    {
                        throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.Message, e);
                    }
                    catch (ArgumentException e)
                    {
                        throw new InvalidCipherTextException("unable to recover ephemeral public key: " + e.Message, e);
                    }

                    int encLength = (inLen - (int)(bIn.Length - bIn.Position));
                    V = Arrays.CopyOfRange(@in, inOff + SecureHeadSize, inOff + encLength);
                }
            }

            // Compute the common value and convert to byte array.
            Agree.Init(PrivParam);
            BigInteger z = Agree.CalculateAgreement(PubParam);

            byte[] bigZ = BigIntegers.AsUnsignedByteArray(Agree.GetFieldSize(), z);

            try
            {
                // Initialise the KDF.
                KdfParameters kdfParam = new KdfParameters(bigZ, null);
                Kdf.Init(kdfParam);

                if (ForEncryption)
                {
                    return(EncryptBlock(@in, inOff, inLen));
                }
                else
                {
                    byte[] temp = new byte[inLen - SecureHeadSize];
                    Array.Copy(@in, inOff + SecureHeadSize, temp, 0, temp.Length);
                    return(DecryptBlock(temp, inOff, temp.Length));
                }
            }
            finally
            {
                Arrays.Fill(bigZ, 0);
            }
        }