編輯:我發現了這個問題。調試器在沒有任何解釋的情況下殺死應用程序(在CryptoStream.ReadByte()調用中)
我的對象的構造函數正在以書寫模式初始化文件流,無論我們是否要加密或解密文件。即使寫入文件流在解密之前會被關閉(用於此目的的另一個文件流),它仍然會導致無聲的崩潰。現在它工作正常。
所以它看起來像一個相當普通的文件訪問問題。 CryptoStream
沒有把它作爲例外處理,這真是一件好事。
感謝您的建議 - 我會看看ProcDump和WinDbg。
我剛剛創建了一個小應用程序來讀取和解密本地硬盤上的文件。
Visual Studio調試器完全沒有任何消息或解釋而終止應用程序。在下面的代碼中執行read = crypto.ReadByte()
行時發生。
try
{
if (file != null)
{
// the stream that was used for encryption
file.Close();
}
var provider = new DESCryptoServiceProvider();
provider.Key = Key;
provider.IV = Key;
var ict = provider.CreateDecryptor();
var bytes = new List<byte>();
using (var stream = new FileStream(fileName, FileMode.Open))
{
crypto = new CryptoStream(stream, ict, CryptoStreamMode.Read);
var decrypted = new MemoryStream();
Int32 read;
try
{
do
{
read = crypto.ReadByte(); // the application dies here
if (read != -1) bytes.Add(Convert.ToByte(read));
}
while (read != -1);
}
finally
{
// we never get here
decrypted.Close();
}
};
return ASCIIEncoding.ASCII.GetString(bytes.ToArray());
}
catch (Exception x)
{
// we never get here either
crypto.Close();
return x.Message;
}
我嘗試和排除:
沒有異常拋出。我們從不輸入
finally
或catch
。我也啓用了Break when an exception is thrown
,但它沒有區別。沒有任何異常的跡象。發佈應用程序(ClickOnce)並運行它具有相同的效果。
新鮮重新啓動沒有區別。
製作文件的副本(如果其他進程阻止訪問它等),並嘗試讀取該副本以相同的方式失敗。
如果我嘗試在監視窗口,以評估
crypto.ReadByte()
,我在爲了得到以下信息:This expression causes side effects and will not be evaluated
;,當我刷新:
Function evaluation was aborted
當我要麼刷新或只是等待幾秒鐘:
Unable to evaluate the expression. The object invoked has disconnected from its clients.
和應用程序被終止,我們又回到了視覺工作室。
這裏有什麼問題?
是'Key'初始化?看起來它在某些方面可能不正確。 –
@Jeremy'Key'是硬編碼的。這是一個由Pi的8個第一個數字組成的字節數組。 –
使用此密鑰進行編碼/解碼是否正常工作?嘗試一個專門的單元測試 - 至少這會消除這個原因。 –