what mea开发者_Go百科n this exception please ?
Unhandled Exception: System.ObjectDisposedException: The object was used after being disposed. at System.IO.StreamWriter.Write (System.String value) [0x00000] in :0 at System.IO.TextWriter.WriteLine (System.String value) [0x00000] in :0 at fichier.MainClass.Main (System.String[] args) [0x000bd] in /Users/mediatun1/Projects/fichier/fichier/Main.cs:122
System.Security.Cryptography.MD5 alg = System.Security.Cryptography.MD5.Create();
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
byte[] md5val = alg.ComputeHash(enc.GetBytes("TESTTESTTESTTESTTESTTEST"));
string output = Convert.ToBase64String(md5val);
string pathPlainTextFile="/Users/mediatun1/Desktop/IAAT.xml";
string pathCypheredTextFile="/Users/mediatun1/Desktop/IAA.xml";
StreamReader fsPlainTextFile = File.OpenText(pathPlainTextFile);
FileInfo t = new FileInfo(pathCypheredTextFile);
StreamWriter Tex =t.CreateText();
string input = null;
while ((input = fsPlainTextFile.ReadLine()) != null)
{
byte[] plainText = Encoding.UTF8.GetBytes(input);
RijndaelManaged rijndael = new RijndaelManaged();
// Définit le mode utilisé
rijndael.Mode = CipherMode.ECB;
// Crée le chiffreur AES - Rijndael
ICryptoTransform aesEncryptor = rijndael.CreateEncryptor(md5val,null);
MemoryStream ms = new MemoryStream();
// Ecris les données chiffrées dans le MemoryStream
CryptoStream cs = new CryptoStream(ms, aesEncryptor, CryptoStreamMode.Write);
cs.Write(plainText, 0, plainText.Length);
cs.FlushFinalBlock();
// Place les données chiffrées dans un tableau d'octet
byte[] CipherBytes = ms.ToArray();
ms.Close();
cs.Close();
// Place les données chiffrées dans une chaine encodée en Base64
Tex.WriteLine (Convert.ToBase64String(CipherBytes));
Console.WriteLine (Convert.ToBase64String(CipherBytes));
Tex.Close();
}
You have Tex.Close();
inside your loop. So after iteration 1 the StreamWriter is closed.
Generally Streams
that are opened outside the loop should be closed outside the loop.
Sounds like an issue with closing your streams to early.I cannot quite work it out on your code, perhaps if you change order of
ms.Close();
cs.Close();
to...
cs.Close();
ms.Close();
it may work
EDIT: As jaywayco has pointed out, the failing code is definitely going to be due to Tex being closed inside the loop. I had completely missed this whilst trying to understand the code
However, you really need to tidy all this up. You should use "using" statements for your streams as this will close them automatically when finished with. Something like....
using(MemoryStream ms = new MemoryStream(...))
{
using(CryptoStream cs = new CryptoStream(...))
{
//code for cs stream in here
}
}
精彩评论