2014-01-15 83 views
1

我正在開發一個asp mvc#應用程序。我要加密的HttpPostedFileBase(在下面的代碼file是,我要加密的HttpPostedFileBase):加密HttpPostedFileBase

void Upload(string target, HttpPostedFileBase file) 
{ 
FullPath dest = ParsePath(target); 
FileInfo path = new FileInfo(Path.Combine(dest.Directory.FullName, Path.GetFileName(file.FileName))); 

//file.SaveAs(path.FullName); 

MemoryStream _MemoryStream = new MemoryStream(); 
file.InputStream.CopyTo(_MemoryStream); 

DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider(); 

cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH"); 
cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH"); 

CryptoStream crStream = new CryptoStream(_MemoryStream, cryptic.CreateEncryptor(), CryptoStreamMode.Write); 
//Here I want to save my crypted stream to the path 
//.... 
} 
+4

什麼是你想實現什麼? HttpPostedFileBase是從客戶端瀏覽器完成的傳輸。它已經通過網絡傳輸,並且您在這裏獲取數據時已將數據存儲在您的服務器上。你想在保存到磁盤之前進行加密嗎?如果是這樣,你不應該嘗試使用InputStream來做任何寫作。 –

+0

我想在保存到我的服務器之前加密它 – Sayadi

+1

好吧,將'file.InputStream'複製到'Stream'對象中,然後加密該對象。我不明白問題在哪裏。 – ataravati

回答

0
CryptoStream crStream = new CryptoStream(_MemoryStream, cryptic.CreateEncryptor(), CryptoStreamMode.Write); 
    using (var output = new FileStream(path.FullName, FileMode.Create, FileAccess.Write)) 
    { 
     crStream.CopyTo(output); 
    } 
+0

我相信CryptoSteam應該纏繞在FileStream上並複製到CryptoSteam中,如下所示: using(var output = new FileStream(path.FullName,FileMode.Create,FileAccess.Write)) CryptoStream crStream = new CryptoStream(output,cryptic.CreateEncryptor(),CryptoStreamMode.Write); file.InputStream.CopyTo(crStream); } 這樣,它更像是:inputStream-> crypto-> file –