以下是一段MSDN中的代码 public static void EncryptTextToFile(String Data, String FileName, byte[] Key, byte[] IV)
{
try
{
// Create or open the specified file.
FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate); // Create a new Rijndael object.
Rijndael RijndaelAlg = Rijndael.Create(); // Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(fStream,
RijndaelAlg.CreateEncryptor(Key, IV),
CryptoStreamMode.Write); // Create a StreamWriter using the CryptoStream.
StreamWriter sWriter = new StreamWriter(cStream); try
{
// Write the data to the stream
// to encrypt it.
sWriter.WriteLine(Data);
}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{
// Close the streams and
// close the file.
sWriter.Close();
cStream.Close();
fStream.Close();
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
} }让我郁闷的是,为什么三个Stream对象的Close()调用是放在第二个try块里的,如果在第一个try中的
“StreamWriter sWriter = new StreamWriter(cStream);”发生异常,那么,前两个Stream对象不就无法Close下面这段是自已写的代码,异常测试的结果也很让人郁闷using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Diagnostics;namespace TryTest
{
class DesTest
{
public static string Test()
{
//Key设置
byte[] key = Encoding.UTF8.GetBytes("12345678");
//IV设置
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
//待解密数据
byte[] value = Convert.FromBase64String("Btf5QTYSUiNGimws8DE3n5EIYNwkJZqW"); //创建des对象
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//内存六
MemoryStream mStream = null;
//解密流
CryptoStream cStream = null;
//流Reader
StreamReader sReader = null;
//返回值
string retString = string.Empty;
try
{ mStream = new MemoryStream(value); //创建内存流,并存入待解密数据
//throw new Exception("err"); //在这里抛出一个异常,正常 cStream = new CryptoStream(mStream, des.CreateDecryptor(key, iv), CryptoStreamMode.Read); //将内存流包装到解密流
//throw new Exception("err"); //在这里抛出一个异常,finally出错 sReader = new StreamReader(cStream); //将解密流包装到Reader
//throw new Exception("err"); //在这里抛出一个异常,finally出错 retString = sReader.ReadToEnd(); //读出解密数据
//throw new Exception("err"); //在这里抛出一个异常,正常 }
catch(Exception e)
{
Trace.WriteLine(e.Message);
}
finally
{
if (sReader != null)
sReader.Close(); if (cStream != null)
cStream.Close(); if (mStream != null)
mStream.Close();
} return retString;
}
}
}我用一个try块包住了所有处理代码,然后根据null来Close流,让手动加入throw进行异常测试的结果却让人郁闷,求解!
{
try
{
// Create or open the specified file.
FileStream fStream = File.Open(FileName, FileMode.OpenOrCreate); // Create a new Rijndael object.
Rijndael RijndaelAlg = Rijndael.Create(); // Create a CryptoStream using the FileStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(fStream,
RijndaelAlg.CreateEncryptor(Key, IV),
CryptoStreamMode.Write); // Create a StreamWriter using the CryptoStream.
StreamWriter sWriter = new StreamWriter(cStream); try
{
// Write the data to the stream
// to encrypt it.
sWriter.WriteLine(Data);
}
catch (Exception e)
{
Console.WriteLine("An error occurred: {0}", e.Message);
}
finally
{
// Close the streams and
// close the file.
sWriter.Close();
cStream.Close();
fStream.Close();
}
}
catch (CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("A file error occurred: {0}", e.Message);
} }让我郁闷的是,为什么三个Stream对象的Close()调用是放在第二个try块里的,如果在第一个try中的
“StreamWriter sWriter = new StreamWriter(cStream);”发生异常,那么,前两个Stream对象不就无法Close下面这段是自已写的代码,异常测试的结果也很让人郁闷using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Diagnostics;namespace TryTest
{
class DesTest
{
public static string Test()
{
//Key设置
byte[] key = Encoding.UTF8.GetBytes("12345678");
//IV设置
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
//待解密数据
byte[] value = Convert.FromBase64String("Btf5QTYSUiNGimws8DE3n5EIYNwkJZqW"); //创建des对象
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//内存六
MemoryStream mStream = null;
//解密流
CryptoStream cStream = null;
//流Reader
StreamReader sReader = null;
//返回值
string retString = string.Empty;
try
{ mStream = new MemoryStream(value); //创建内存流,并存入待解密数据
//throw new Exception("err"); //在这里抛出一个异常,正常 cStream = new CryptoStream(mStream, des.CreateDecryptor(key, iv), CryptoStreamMode.Read); //将内存流包装到解密流
//throw new Exception("err"); //在这里抛出一个异常,finally出错 sReader = new StreamReader(cStream); //将解密流包装到Reader
//throw new Exception("err"); //在这里抛出一个异常,finally出错 retString = sReader.ReadToEnd(); //读出解密数据
//throw new Exception("err"); //在这里抛出一个异常,正常 }
catch(Exception e)
{
Trace.WriteLine(e.Message);
}
finally
{
if (sReader != null)
sReader.Close(); if (cStream != null)
cStream.Close(); if (mStream != null)
mStream.Close();
} return retString;
}
}
}我用一个try块包住了所有处理代码,然后根据null来Close流,让手动加入throw进行异常测试的结果却让人郁闷,求解!
解决方案 »
- vs2012怎么没有发布网站
- C#可枚举的接口(IEnumerable)
- 求大神帮助!
- 怎么创建一个HtmlDocument对象
- 求教,关于c#操作EXCEL的问题
- 请问在C#中怎们取得DataGrid中数据的行数
- 高手赐教:奇怪了,c#里面怎么没提供16进制小数转化成double类型的函数,如16进制1AB.12怎么用函数转化成对应的double类型数据。
- 求助,关于excel数据读取的问题
- 思归大哥请进,谢谢你!(http://community.csdn.net/Expert/topic/3725/3725676.xml?temp=.1150324)
- 加密的XMLBase64格式的数据,我怎么还原?
- C#分布式事務.(高手進)
- win form下的DataGridView邦定了某种IEnumerable<T>的数据源之后怎样保持动态更新?
如果CryptoStream的内容没有全部读出前关闭流的话,就会出错,解决办法:
finally
{ if (sReader != null && sReader.BaseStream != null)
{
sReader.ReadToEnd();
sReader.Close();
} if (cStream != null)
{
while (cStream.ReadByte() != -1)
{
cStream.ReadByte();
} cStream.Close();
} if (mStream != null)
{
mStream.Close();
}
}
才会触发异常,其他之前的代码不会触发错误
finally
{
if (sReader != null)
sReader.Close();
if (cStream != null)
{
while (cStream.ReadByte() != -1) ;
cStream.Close();
} if (mStream != null)
mStream.Close();
}只需要把cStream的数据读完就可以了,StreadReader不存在这样的问题