0
嘗試反序列化爲XML時出現「無法訪問已關閉的文件」錯誤。 我有一個加密的文件。我解密文件並嘗試反序列化它,當我得到這個錯誤。 它工作正常,沒有加密和解密。無法訪問已關閉的文件錯誤
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Models.Test));
var fileLocStream = FileManipultions.DecryptToStream(fileLoc);
var testResult = (Models.Test)xmlSerializer.Deserialize(FileManipultions.DecryptToStream(fileLoc));
public static Stream DecryptToStream(string inputFilePath)
{
try
{
string EncryptionKey = ConfigurationManager.AppSettings["EncDesKey"].ToString();
CryptoStream cs;
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (FileStream fsInput = new FileStream(inputFilePath, FileMode.Open))
{
cs = new CryptoStream(fsInput, encryptor.CreateDecryptor(), CryptoStreamMode.Read);
}
}
return cs;
}
catch (Exception ex)
{
Logger.LogException(ex.Message, System.Reflection.MethodBase.GetCurrentMethod().ToString());
return null;
}
}