为啥我不能使用 RijndaelManaged 解密数据?
Posted
技术标签:
【中文标题】为啥我不能使用 RijndaelManaged 解密数据?【英文标题】:Why can't I decrypt data using RijndaelManaged?为什么我不能使用 RijndaelManaged 解密数据? 【发布时间】:2011-11-10 15:09:00 【问题描述】:我正在开发一个像信使一样发送和接收消息的程序,我需要在发送按钮上加密消息并在收到消息时解密消息。我正在使用 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;
问题是我在解密时遇到异常加密异常 - 要解密的数据长度无效,我不知道为什么?
【问题讨论】:
【参考方案1】:在设置encrypted
字节数组之前,您可能应该在加密时刷新CryptoStream
:
...
csEncrypt.FlushFinalBlock();
encrypted = msEncrypt.ToArray();
...
CryptoStream.FlushFinalBlock Method 的摘录:
调用 Close 方法将调用 FlushFinalBlock。如果你不打电话 关闭,调用 FlushFinalBlock 完成缓冲区的刷新。称呼 仅在所有流活动完成时才刷新FinalBlock。
显然,如果您调用Close
,所有数据都将丢失,因为它也会关闭底层MemoryStream
。
【讨论】:
以上是关于为啥我不能使用 RijndaelManaged 解密数据?的主要内容,如果未能解决你的问题,请参考以下文章
AesManaged 与 RijndaelManaged [关闭]
RijndaelManaged 与 AesCryptoServiceProvider(AES 加密)
使用 RijndaelManaged 在 C# 中加密/解密流
RijndaelManaged Decryption - 如何优雅地删除填充/0?