I am trying to write client software which performs AES encryption and decryption of messages to a device using c#.
Using the AES class from System.Security.Cryptography, there is no problem sending encrypted messages to the device. The device decrypts these successfully.
The problem occurs when decrypting messages received from the device. We get the message: "Padding is invalid and cannot be removed."
I have searched the web and tried three different approaches but all have the same error - see below. I also tried the three approaches without setting the KeySize property.
In addition to the client being written in C#, a python client was also written where there everything works fine - using the python aes library. So, having got a python version I was able to compare the lengths of the received cipherText which is 32 bytes long and is a byte array. 15 bytes are padding. I really appreciate help.
Option 1
byte[] messageBuffer = null;
using (Aes aesAlg = Aes.Create())
{
aesAlg.BlockSize = 128;
aesAlg.KeySize = 128;
aesAlg.Mode = CipherMode.CBC;
aesAlg.Key = encryptionKey; //used by device to encrypt. encryptionKey is a 16 byte array
aesAlg.IV = sentIV; //This agrees with the IV that was used to encrypt the message by the device. sentIV is a 16 byte array
//aesAlg.Padding = PaddingMode.PKCS7; // this makes no difference
byte[] cipherText = encryptedMessagePart; //encryptedMessagePart is byte[] encryptedMessagePart
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
try
{
messageBuffer = decryptor.TransformFinalBlock(cipherText, 0, cipherText.Length); //****fails here ********************
}
catch (Exception ex)
{
....;
}
}
Option 2
byte[] messageBuffer = new byte [1024];
using (Aes aesAlg = Aes.Create())
{
aesAlg.BlockSize = 128;
aesAlg.KeySize = 128;
aesAlg.Mode = CipherMode.CBC;
aesAlg.Key = encryptionKey; //used by device to encrypt. encryptionKey is a 16 byte array
aesAlg.IV = sentIV; //This agrees with the IV that was used to encrypt the message by the device. sentIV is a 16 byte array
//aesAlg.Padding = PaddingMode.PKCS7; // this makes no difference
byte[] cipherText = encryptedMessagePart; //encryptedMessagePart is byte[] encryptedMessagePart
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (var msDecrypt = new MemoryStream(cipherText))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
try
{
var zx = csDecrypt.Read(messageBuffer, 0, cipherText.Length); //****fails here ********************
}
catch (Exception ex)
{
....;
}
}
}
}
Option 3
byte[] messageBuffer = new byte [1024];
using (Aes aesAlg = Aes.Create())
{
aesAlg.BlockSize = 128;
aesAlg.KeySize = 128;
aesAlg.Mode = CipherMode.CBC;
aesAlg.Key = encryptionKey; //used by device to encrypt. encryptionKey is a 16 byte array
aesAlg.IV = sentIV; //This agrees with the IV that was used to encrypt the message by the device. sentIV is a 16 byte array
//aesAlg.Padding = PaddingMode.PKCS7; // this makes no difference
byte[] cipherText = encryptedMessagePart; //encryptedMessagePart is byte[] encryptedMessagePart
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (var msDecrypt = new MemoryStream(cipherText))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var srDecrypt = new StreamReader(csDecrypt))
{
try
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
var pt = srDecrypt.ReadToEnd(); //****fails here ********************
messageBuffer = Utils.GetBytes(pt); //convert to bytes
}
catch (Exception ex)
{
....;
}
}
}
}
}