C#AES-128 CFB错误 [英] C# AES-128 CFB Error

查看:90
本文介绍了C#AES-128 CFB错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前以这种方式设置RijndaelManaged(由于服务器处理加密的方式,IV和Key相同)。服务器还在模式中使用CFB8,我设置正确吗?

I currently set up RijndaelManaged this way (the IV and Key are the same due to how the server handles the encryption). The server also uses CFB8 for the Mode, did I set this up correctly?

    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();

I write data by doing this: ICryptoTransform e = GenerateAES(key).CreateEncryptor();

        using(CryptoStream stream = new CryptoStream(BaseStream, e, CryptoStreamMode.Write))
        {
            stream.WriteByte(b);
            stream.FlushFinalBlock();
        }

BaseStream是我打开的NetworkStream,而'b'是一个我已发送到函数。

BaseStream is a NetworkStream that I opened, and 'b' is a value that I sent to my function.

当我尝试对流进行0x00(作为测试)时,出现此错误:

When I try to a 0x00 (as a test) to the stream, I get this error:

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()

我只做了此功能测试是否可以在不依赖任何外部库的情况下与服务器通信。

I only made this function test whether or not that if I can communicate to the server without relying on any external libraries.

推荐答案

您已将 PaddingMode 设置为无,并且尝试加密的字节数少于完整数据块的字节数。要么加密更多数据( cipher.BlockSize 的倍数),要么将填充模式设置为None,以使其自动填充为适当的长度。

You've set the PaddingMode to None, and are trying to encrypt fewer bytes than a full block of data. Either encrypt more data (a multiple of the cipher.BlockSize) or set the padding mode to something other than None so that it's automatically padded to the appropriate length.

编辑:

RijndaelManaged的默认FeedbackSize为128位,但是您要使用CFB8 。如果将 cipher.FeedbackSize 设置为8,则无需填充即可将其用作流密码,并且写入CryptoStream的每个字节都将被加密和写入。立即转到输出流。您不应在每次写入后调用 FlushFinalBlock ,因为这会终止加密过程。

The default FeedbackSize for RijndaelManaged is 128 bits, but you want to use CFB8. If you set the cipher.FeedbackSize to 8, you will be able to use it as a stream cipher with no padding, and every byte written to the CryptoStream will be encrypted and written to the output stream immediately. You should not call FlushFinalBlock after each write, as this terminates the encryption process.

这篇关于C#AES-128 CFB错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆