小编0xF*_*xFF的帖子

C#AES-128 CFB错误

我目前以这种方式设置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)

c# aes

0
推荐指数
1
解决办法
2417
查看次数

标签 统计

aes ×1

c# ×1