为什么我无法使用 RijndaelManaged 解密数据?

发布于 2024-12-14 18:40:55 字数 5424 浏览 3 评论 0原文

我正在开发一个像信使一样发送和接收消息的程序,我需要在发送按钮上加密消息并在收到消息时解密消息。我正在使用 RijndaelManaged 类和以下方法来加密/解密

 public byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
    {

        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");
        byte[] encrypted;
        // Create an RijndaelManaged object
        // with the specified key and IV.
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for encryption.
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }
                    encrypted = msEncrypt.ToArray();

                }
            }
        }


        // Return the encrypted bytes from the memory stream.
        return encrypted;

    }

以下

 public string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");

        // Declare the string used to hold
        // the decrypted text.
        string plaintext = null;

        // Create an RijndaelManaged object
        // with the specified key and IV.
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for decryption.
            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {

                        // Read the decrypted bytes from the decrypting stream
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }

        }

        return plaintext;

    }

是我调用前面的方法的方法:

  private void SendMessage()
    {
        string str;

        System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
        str = enc.GetString(this.EncryptStringToBytes(this.txtNewMessage.Text, myRijndael.Key, myRijndael.IV ));

        if ( this.remoteClient.Connected && this.txtNewMessage.Text.Trim() != "")
        {
            this.remoteClient.SendCommand(new Proshot.CommandClient.Command(Proshot.CommandClient.CommandType.Message, this.targetIP,str));
            this.txtMessages.Text += this.remoteClient.NetworkName + ": " + this.txtNewMessage.Text.Trim()  + "//---SENT" + Environment.NewLine;
            this.txtNewMessage.Text = "";
            this.txtNewMessage.Focus();
        }
    }



  private void private_CommandReceived(object sender , CommandEventArgs e)
    {
        string str;
        byte[] byteString;


        str =  e.Command.MetaData;

        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        byteString = encoding.GetBytes(str);


        switch ( e.Command.CommandType )
        {
            case ( CommandType.Message ):
                if ( !e.Command.Target.Equals(IPAddress.Broadcast) && e.Command.SenderIP.Equals(this.targetIP))
                {
                    //myRijndael.Padding = PaddingMode.Zeros;
                    this.txtMessages.Text += e.Command.SenderName + ": " + this.DecryptStringFromBytes(byteString, myRijndael.Key, myRijndael.IV) + "//---Received" + Environment.NewLine;
                    if ( !this.activated)
                    {
                        if(this.WindowState == FormWindowState.Normal || this.WindowState == FormWindowState.Maximized)
                            ShareUtils.PlaySound(ShareUtils.SoundType.NewMessageReceived);
                        else
                            ShareUtils.PlaySound(ShareUtils.SoundType.NewMessageWithPow);
                        this.Flash(this.Handle , FlashMode.FLASHW_ALL , 3);
                    }
                }
                break;
        }    
    }

问题是我在解密加密异常 - 长度时遇到异常要解密的数据无效,我不明白为什么?

I'm working on a program that sends and receives messages just like messengers, and I need to encrypt the message on send button and decrypt the message when received. I'm using the RijndaelManaged class and the following methods to encrypt/decrypt

 public byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
    {

        // Check arguments.
        if (plainText == null || plainText.Length <= 0)
            throw new ArgumentNullException("plainText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");
        byte[] encrypted;
        // Create an RijndaelManaged object
        // with the specified key and IV.
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for encryption.
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {

                        //Write all data to the stream.
                        swEncrypt.Write(plainText);
                    }
                    encrypted = msEncrypt.ToArray();

                }
            }
        }


        // Return the encrypted bytes from the memory stream.
        return encrypted;

    }

And

 public string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
    {
        // Check arguments.
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");

        // Declare the string used to hold
        // the decrypted text.
        string plaintext = null;

        // Create an RijndaelManaged object
        // with the specified key and IV.
        using (RijndaelManaged rijAlg = new RijndaelManaged())
        {
            rijAlg.Key = Key;
            rijAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

            // Create the streams used for decryption.
            using (MemoryStream msDecrypt = new MemoryStream(cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {

                        // Read the decrypted bytes from the decrypting stream
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }

        }

        return plaintext;

    }

Here's how I invoke the previous methods:

  private void SendMessage()
    {
        string str;

        System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
        str = enc.GetString(this.EncryptStringToBytes(this.txtNewMessage.Text, myRijndael.Key, myRijndael.IV ));

        if ( this.remoteClient.Connected && this.txtNewMessage.Text.Trim() != "")
        {
            this.remoteClient.SendCommand(new Proshot.CommandClient.Command(Proshot.CommandClient.CommandType.Message, this.targetIP,str));
            this.txtMessages.Text += this.remoteClient.NetworkName + ": " + this.txtNewMessage.Text.Trim()  + "//---SENT" + Environment.NewLine;
            this.txtNewMessage.Text = "";
            this.txtNewMessage.Focus();
        }
    }



  private void private_CommandReceived(object sender , CommandEventArgs e)
    {
        string str;
        byte[] byteString;


        str =  e.Command.MetaData;

        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        byteString = encoding.GetBytes(str);


        switch ( e.Command.CommandType )
        {
            case ( CommandType.Message ):
                if ( !e.Command.Target.Equals(IPAddress.Broadcast) && e.Command.SenderIP.Equals(this.targetIP))
                {
                    //myRijndael.Padding = PaddingMode.Zeros;
                    this.txtMessages.Text += e.Command.SenderName + ": " + this.DecryptStringFromBytes(byteString, myRijndael.Key, myRijndael.IV) + "//---Received" + Environment.NewLine;
                    if ( !this.activated)
                    {
                        if(this.WindowState == FormWindowState.Normal || this.WindowState == FormWindowState.Maximized)
                            ShareUtils.PlaySound(ShareUtils.SoundType.NewMessageReceived);
                        else
                            ShareUtils.PlaySound(ShareUtils.SoundType.NewMessageWithPow);
                        this.Flash(this.Handle , FlashMode.FLASHW_ALL , 3);
                    }
                }
                break;
        }    
    }

The problem is I'm getting exception when decrypting Cryptographic Exception - Length of the data to decrypt is invalid, I can't figure out why ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小伙你站住 2024-12-21 18:40:55

在设置加密字节数组之前,您可能应该在加密时刷新您的CryptoStream

...
csEncrypt.FlushFinalBlock();
encrypted = msEncrypt.ToArray();
...

摘自CryptoStream.FlushFinalBlock 方法

调用Close方法将会调用FlushFinalBlock。如果你不打电话
关闭,调用FlushFinalBlock完成缓冲区的刷新。称呼
仅当所有流活动完成时才进行 FlushFinalBlock。

显然,如果您调用Close,所有数据都将丢失,因为它也会关闭底层MemoryStream

Probably you should flush your CryptoStream while encrypting before you set the encrypted byte array:

...
csEncrypt.FlushFinalBlock();
encrypted = msEncrypt.ToArray();
...

An extract from CryptoStream.FlushFinalBlock Method:

Calling the Close method will call FlushFinalBlock. If you do not call
Close, call FlushFinalBlock to complete flushing the buffer. Call
FlushFinalBlock only when all stream activity is complete.

Obviously, if you call Close, all data will be lost, as it will close the underlying MemoryStream as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文