1

I'm trying to implement MTProto with .net core and in a part of it, it's using AES256-IGE encryption which is not implemented in .net.

Is there any way to implement it with current Cryptography of .net?

Update: Here is what i implement but i'm not sure is it correct or not?

    public class AesIGEEncrypt
{
    private readonly byte[] _key;
    private readonly byte[] _iv;

    public AesIGEEncrypt(byte[] key, byte[] iv)
    {
        _key = key;
        _iv = iv;
    }

    public Memory<byte> Encrypt(ReadOnlySpan<byte> bytes)
    {
        using var aes = new AesManaged
        {
            Mode = CipherMode.ECB,
            Padding = PaddingMode.None
        };

        var zeroIV = new byte[aes.BlockSize];

        using var encryptor = aes.CreateEncryptor(_key, zeroIV);

        var t = new byte[aes.BlockSize];
        var x = new byte[aes.BlockSize];
        var y = new byte[aes.BlockSize];

        _iv.AsSpan()[..aes.BlockSize].CopyTo(x);
        _iv.AsSpan()[aes.BlockSize..].CopyTo(y);

        var encryptedData = new byte[bytes.Length];

        for (int i = 0; i < bytes.Length; i += aes.BlockSize)
        {
            XOR(x, bytes[i..(i + aes.BlockSize)]);
            encryptor.TransformBlock(x, 0, x.Length, t, 0);
            XOR(t, y);
            (x, y) = (t, bytes[i..(i + aes.BlockSize)].ToArray());
            t.AsSpan().CopyTo(encryptedData[i..]);
        }

        return encryptedData.AsMemory();

    }

    public Memory<byte> Decrypt(ReadOnlySpan<byte> bytes)
    {
        using var aes = new AesManaged
        {
            Mode = CipherMode.ECB,
            Padding = PaddingMode.None
        };

        var zeroIV = new byte[aes.BlockSize];

        using var decryptor = aes.CreateDecryptor(_key, zeroIV);

        var t = new byte[aes.BlockSize];
        var x = new byte[aes.BlockSize];
        var y = new byte[aes.BlockSize];

        _iv.AsSpan()[..aes.BlockSize].CopyTo(x);
        _iv.AsSpan()[aes.BlockSize..].CopyTo(y);

        var decryptedData = new byte[bytes.Length];

        for (int i = 0; i < bytes.Length; i += aes.BlockSize)
        {
            XOR(y, bytes[i..(i + aes.BlockSize)]);
            decryptor.TransformBlock(x, 0, x.Length, t, 0);
            XOR(t, x);
            (y, x) = (t, bytes[i..(i + aes.BlockSize)].ToArray());
            t.AsSpan().CopyTo(decryptedData[i..]);
        }

        return decryptedData.AsMemory();

    }

    private void XOR(Span<byte> dest, ReadOnlySpan<byte> src)
    {
        for (int i = 0; i < dest.Length; i++)
        {
            dest[i] = (byte)(dest[i] ^ src[i]);
        }
    }
}

Actually i'm not sure should i use zero-Iv with aes-ECB or not?

Ali Zeinali
  • 551
  • 4
  • 16
  • Of course there is a way: simply follow the specifications and use .Net AES primitives like AES256 in ECB mode to create an AES256 with IGE mode. – President James K. Polk Nov 22 '19 at 15:02
  • @JamesReinstateMonicaPolk, Can you give me more detail of how to implement it? – Ali Zeinali Nov 22 '19 at 15:28
  • No. There is already enough detail readily available. Your job is come up with more detail about what difficulty you're having. – President James K. Polk Nov 22 '19 at 17:48
  • @JamesReinstateMonicaPolk, Thank you. i update my question with my implementation code. But i'm not sure is it right or not. – Ali Zeinali Nov 22 '19 at 20:15
  • 1
    The IV is fully ignored in ECB mode. Um, did you look for test vectors? I quickly found some [here](https://mgp25.com/AESIGE/) and it includes a link to Java version as well, and Java and C# are not that far apart. [Here](https://stackoverflow.com/q/18171973/589259) is some more IGE stuff on SO. – Maarten Bodewes Nov 23 '19 at 12:56
  • @MaartenBodewes Thanks you.Finally i implement it and it worked well. 'The IV is fully ignored in ECB mode.' was good point. – Ali Zeinali Nov 23 '19 at 16:43

0 Answers0