TLSharp.Core.MTProto.Crypto.AES.EncryptIGE C# (CSharp) Method

EncryptIGE() public static method

public static EncryptIGE ( byte originPlaintext, byte key, byte iv ) : byte[]
originPlaintext byte
key byte
iv byte
return byte[]
        public static byte[] EncryptIGE(byte[] originPlaintext, byte[] key, byte[] iv)
        {
            byte[] plaintext;
            using (MemoryStream plaintextBuffer = new MemoryStream(originPlaintext.Length + 40))
            {
                //using(SHA1 hash = new SHA1Managed()) {
                //byte[] hashsum = hash.ComputeHash(originPlaintext);
                //plaintextBuffer.Write(hashsum, 0, hashsum.Length);
                plaintextBuffer.Write(originPlaintext, 0, originPlaintext.Length);
                while (plaintextBuffer.Position % 16 != 0)
                {
                    plaintextBuffer.WriteByte(0); // TODO: random padding
                }
                plaintext = plaintextBuffer.ToArray();
            }

            var iv1 = new byte[iv.Length / 2];
            var iv2 = new byte[iv.Length / 2];

            Array.Copy(iv, 0, iv1, 0, iv1.Length);
            Array.Copy(iv, iv1.Length, iv2, 0, iv2.Length);

            AesEngine aes = new AesEngine();
            aes.Init(true, key);

            int blocksCount = plaintext.Length / 16;
            byte[] ciphertext = new byte[plaintext.Length];

            byte[] ciphertextBlock = new byte[16];
            byte[] plaintextBlock = new byte[16];
            for (int blockIndex = 0; blockIndex < blocksCount; blockIndex++)
            {
                Array.Copy(plaintext, 16 * blockIndex, plaintextBlock, 0, 16);

                //logger.info("plaintext block: {0} xor {1}", BitConverter.ToString(plaintextBlock).Replace("-", ""), BitConverter.ToString(iv1).Replace("-", ""));

                for (int i = 0; i < 16; i++)
                {
                    plaintextBlock[i] ^= iv1[i];
                }

                //logger.info("xored plaintext: {0}", BitConverter.ToString(plaintextBlock).Replace("-", ""));

                aes.ProcessBlock(plaintextBlock, 0, ciphertextBlock, 0);

                //logger.info("encrypted plaintext: {0} xor {1}", BitConverter.ToString(ciphertextBlock).Replace("-", ""), BitConverter.ToString(iv2).Replace("-", ""));

                for (int i = 0; i < 16; i++)
                {
                    ciphertextBlock[i] ^= iv2[i];
                }

                //logger.info("xored ciphertext: {0}", BitConverter.ToString(ciphertextBlock).Replace("-", ""));

                Array.Copy(ciphertextBlock, 0, iv1, 0, 16);
                Array.Copy(plaintext, 16 * blockIndex, iv2, 0, 16);

                Array.Copy(ciphertextBlock, 0, ciphertext, blockIndex * 16, 16);
            }

            return ciphertext;
        }