I am getting the following error while decrypting the content using AesCryptoServiceProvider. The mode which I need to use is CipherMode.CFB and PaddingMode is PKCS7. I am able to decrypt the same content in iOS with same key, IV, PaddingMode and Cipher Mode.
I tried to use CryptoStream in the following way but not able to decrypt the content.
public byte[] DecryptWithAES(byte[] content, byte[] key, byte[] iv, int mode, int paddingMode)
{
byte[] plainBytes = null;
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.BlockSize = 128;
aes.KeySize = 256;
aes.IV = iv;
aes.Key = key;
aes.Padding = (PaddingMode)paddingMode;
aes.Mode = (CipherMode)mode;
ICryptoTransform crypto = aes.CreateDecryptor(aes.Key, aes.IV);
using (var input = new MemoryStream(content))
{
using (var output = new MemoryStream())
{
using (var cryptStream = new CryptoStream(input, crypto, CryptoStreamMode.Read))
{
var buffer = new byte[1024];
var read = cryptStream.Read(buffer, 0, buffer.Length);
while (read > 0)
{
output.Write(buffer, 0, read);
read = cryptStream.Read(buffer, 0, buffer.Length);
}
}
plainBytes = output.ToArray();
}
}
return plainBytes;
}
}
I also tried following way to decrypt the content but not working,
public byte[] DecryptWithAES(byte[] content, byte[] key, byte[] iv, int mode, int paddingMode)
{
byte[] plainBytes = null;
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.BlockSize = 128;
aes.KeySize = 256;
aes.IV = iv;
aes.Key = key;
aes.Padding = (PaddingMode)paddingMode;
aes.Mode = (CipherMode)mode;
ICryptoTransform crypto = aes.CreateDecryptor(aes.Key, aes.IV);
plainBytes = crypto.TransformFinalBlock(content, 0, content.Length);
}
return plainBytes;
}
In both cases, the Padding Mode is PKCS7 and CipherMode is CFB.
I tried this on the encrypted content of length 22. Key Length: 32, IV Length: 16.
I am stuck with this problem from yesterday. Please help me.