C# 使用加密资源尝试/捕获/最终
我的网站上有以下代码。它将URL查询作为名为commandencrypted的字符串。我有一个try/catch/finally块,它将加密的命令复制到memorystream,然后将其解密回字符串。如果try块中出现任何错误,将在catch块中处理这些错误。但是,我希望确保在finally块中关闭所有使用的资源。当提供无效的URL查询命令时,我在尝试关闭csencryptedcommand CryptoStream时收到异常。异常详细信息遵循代码C# 使用加密资源尝试/捕获/最终,c#,asp.net,.net,encryption,try-catch,C#,Asp.net,.net,Encryption,Try Catch,我的网站上有以下代码。它将URL查询作为名为commandencrypted的字符串。我有一个try/catch/finally块,它将加密的命令复制到memorystream,然后将其解密回字符串。如果try块中出现任何错误,将在catch块中处理这些错误。但是,我希望确保在finally块中关闭所有使用的资源。当提供无效的URL查询命令时,我在尝试关闭csencryptedcommand CryptoStream时收到异常。异常详细信息遵循代码 // Resources used f
// Resources used for storing and decrypting the encrypted client command
MemoryStream msencryptedcommand = null;
RijndaelManaged rmencryptedcommand = null;
CryptoStream csencryptedcommand = null;
StreamReader srdecryptedcommand = null;
MemoryStream msdecryptedcommand = null;
try
{
// Copy the encrypted client command (where two characters represent a byte) to a memorystream
msencryptedcommand = new MemoryStream();
for (int i = 0; i < encryptedcommand.Length; )
{
msencryptedcommand.WriteByte(Byte.Parse(encryptedcommand.Substring(i, 2), NumberStyles.HexNumber));
i = i + 2;
}
msencryptedcommand.Flush();
msencryptedcommand.Position = 0;
// Define parameters used for decryption
byte[] key = new byte[] { //bytes hidden// };
byte[] iv = new byte[] { //bytes hidden// };
rmencryptedcommand = new RijndaelManaged();
csencryptedcommand = new CryptoStream(msencryptedcommand, rmencryptedcommand.CreateDecryptor(key, iv), CryptoStreamMode.Read);
msdecryptedcommand = new MemoryStream();
// Decrypt the client command
int decrytptedbyte;
while ((decrytptedbyte = csencryptedcommand.ReadByte()) != -1)
{
msdecryptedcommand.WriteByte((byte)decrytptedbyte);
}
// Store the decrypted client command as a string
srdecryptedcommand = new StreamReader(msdecryptedcommand);
srdecryptedcommand.BaseStream.Position = 0;
string decryptedcommand = srdecryptedcommand.ReadToEnd();
}
catch (Exception ex)
{
ErrorResponse("Invalid URL Query", context, ex.ToString());
return;
}
finally
{
// If any resources were used, close or clear them
if (srdecryptedcommand != null)
{
srdecryptedcommand.Close();
}
if (msdecryptedcommand != null)
{
msdecryptedcommand.Close();
}
if (csencryptedcommand != null)
{
csencryptedcommand.Close();
}
if (rmencryptedcommand != null)
{
rmencryptedcommand.Clear();
}
if (msencryptedcommand != null)
{
msencryptedcommand.Close();
}
}
//用于存储和解密加密客户端命令的资源
MemoryStream msencryptedcommand=null;
RijndaelManaged rmencryptedcommand=null;
CryptoStream csencryptedcommand=null;
StreamReader srdecryptedcommand=null;
MemoryStream msdecryptedcommand=null;
尝试
{
//将加密的客户端命令(其中两个字符表示一个字节)复制到memorystream
msencryptedcommand=new MemoryStream();
对于(int i=0;i
发生以下未处理的异常:System.Security.Cryptography.CryptographyException:要解密的数据长度无效。位于System.Security.Cryptography.Cryptography.RijndaelManagedTransform.TransformFinalBlock(字节[]inputBuffer,Int32 inputOffset,Int32 inputCount)的System.Security.Cryptography.CryptoStream.FlushFinalBlock()位于System.Security.Cryptography.CryptoStream.Dispose(布尔处置)位于System.IO.Stream.Close()的dongleupdate.ProcessRequest(HttpContext上下文)在update.ashx:System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()的System.Web.HttpApplication.ExecuteTep(IExecutionStep step,布尔值和同步完成)的第92行中,URL引用者:用户代理:Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/30.0.1599.69 Safari/537.36请求URL:update.ashx?aa
编辑:
我将代码更改为使用语句。这也是确保关闭所有资源的有效方法吗
try
{
// Copy the encrypted client command (where two characters represent a byte) to a memorystream
using (MemoryStream msencryptedcommand = new MemoryStream())
{
for (int i = 0; i < encryptedcommand.Length; )
{
msencryptedcommand.WriteByte(Byte.Parse(encryptedcommand.Substring(i, 2), NumberStyles.HexNumber));
i = i + 2;
}
msencryptedcommand.Flush();
msencryptedcommand.Position = 0;
// Define parameters used for decryption
byte[] key = new byte[] { //bytes hidden// };
byte[] iv = new byte[] { //bytes hidden// };
using (RijndaelManaged rmencryptedcommand = new RijndaelManaged())
{
using (CryptoStream csencryptedcommand = new CryptoStream(msencryptedcommand, rmencryptedcommand.CreateDecryptor(key, iv), CryptoStreamMode.Read))
{
using (MemoryStream msdecryptedcommand = new MemoryStream())
{
// Decrypt the client command
int decrytptedbyte;
while ((decrytptedbyte = csencryptedcommand.ReadByte()) != -1)
{
msdecryptedcommand.WriteByte((byte)decrytptedbyte);
}
// Store the decrypted client command as a string
using (StreamReader srdecryptedcommand = new StreamReader(msdecryptedcommand))
{
srdecryptedcommand.BaseStream.Position = 0;
string decryptedcommand = srdecryptedcommand.ReadToEnd();
}
}
}
}
}
}
catch (Exception ex)
{
ErrorResponse("Invalid URL Query", context, ex.ToString());
return;
}
试试看
{
//将加密的客户端命令(其中两个字符表示一个字节)复制到memorystream
使用(MemoryStream msencryptedcommand=new MemoryStream())
{
对于(int i=0;i
使用块和
using(csencryptedcommand = new CryptoStream(msencryptedcommand,
rmencryptedcommand.CreateDecryptor(key, iv), CryptoStreamMode.Read))
{
// Do things with crypto stream here
}
protected override void Dispose(bool disposing)
{
try
{
if (disposing)
{
if (!this._finalBlockTransformed)
{
this.FlushFinalBlock();
}
this._stream.Close();
}
}
finally
{
try
{
this._finalBlockTransformed = true;
if (this._InputBuffer != null)
{
Array.Clear(this._InputBuffer, 0, this._InputBuffer.Length);
}
if (this._OutputBuffer != null)
{
Array.Clear(this._OutputBuffer, 0, this._OutputBuffer.Length);
}
this._InputBuffer = null;
this._OutputBuffer = null;
this._canRead = false;
this._canWrite = false;
}
finally
{
base.Dispose(disposing);
}
}
}
this._stream.Close();
using(msencryptedcommand = new MemoryStream())
{
....
}