【发布时间】:2015-03-14 02:09:04
【问题描述】:
我目前以这种方式设置 RijndaelManaged(由于服务器处理加密的方式,IV 和密钥相同)。服务器也使用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;
}
我这样写数据: ICryptoTransform e = GenerateAES(key).CreateEncryptor();
using(CryptoStream stream = new CryptoStream(BaseStream, e, CryptoStreamMode.Write))
{
stream.WriteByte(b);
stream.FlushFinalBlock();
}
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()
at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
我只是让这个函数测试我是否可以在不依赖任何外部库的情况下与服务器通信。
【问题讨论】: