我目前以这种方式设置RijndaelManaged(由于服务器如何处理加密,IV和Key是相同的).服务器还使用CFB8作为模式,我是否正确设置了它?
public static RijndaelManaged GenerateAES(byte[] key)
{
RijndaelManaged cipher = new RijndaelManaged();
cipher.Mode = CipherMode.CFB;
cipher.Padding = PaddingMode.None;
cipher.KeySize = 128;
cipher.Key = key;
cipher.IV = key;
return cipher;
}
Run Code Online (Sandbox Code Playgroud)
我这样写数据:ICryptoTransform e = GenerateAES(key).CreateEncryptor();
using(CryptoStream stream = new CryptoStream(BaseStream, e, CryptoStreamMode.Write))
{
stream.WriteByte(b);
stream.FlushFinalBlock();
}
Run Code Online (Sandbox Code Playgroud)
BaseStream是我打开的NetworkStream,'b'是我发送给我的函数的值.
当我尝试对流进行0x00(作为测试)时,我收到此错误:
System.Security.Cryptography.CryptographicException: Length of the data to encrypt is invalid.
at System.Security.Cryptography.RijndaelManagedTransform.EncryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock() …Run Code Online (Sandbox Code Playgroud)