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?