【发布时间】:2018-01-02 17:18:59
【问题描述】:
我正在尝试编写客户端软件,该软件使用 c# 对设备执行 AES 加密和解密消息。
使用 System.Security.Cryptography 中的 AES 类,向设备发送加密消息没有问题。设备成功解密。
解密从设备收到的消息时会出现此问题。我们收到消息:“填充无效,无法删除。”
我在网上搜索并尝试了三种不同的方法,但都有相同的错误 - 见下文。在没有设置 KeySize 属性的情况下,我也尝试了这三种方法。
除了用 C# 编写的客户端之外,还编写了一个 python 客户端,其中一切正常 - 使用 python aes 库。 因此,有了一个 python 版本,我能够比较接收到的 cipherText 的长度,该长度为 32 个字节,是一个字节数组。 15 个字节是填充。 非常感谢帮助。
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)
{
....;
}
}
}
}
}
【问题讨论】:
-
那么,你的设备到底使用了什么填充模式?
-
该错误通常与填充有关,但意味着由于某种原因解密失败
-
@Plutonix 不一定;请参阅下面的答案。
标签: c# encryption cryptography aes