【发布时间】:2014-07-07 19:29:18
【问题描述】:
我创建了一个 MVC WebRole Window Azure 应用程序,我在其中使用 SymmetricAlgorithm (Rijndael) 将加密文件上传到 Azure blob 存储
控制器>动作是
[HttpPost]
public ActionResult UploadImage_post(HttpPostedFileBase fileBase)
{
if (fileBase.ContentLength > 0)
{
// Retrieve a reference to a container
Microsoft.WindowsAzure.StorageClient.CloudBlobContainer blobContainer =
_myBlobStorageService.GetCloudBlobContainer();
Microsoft.WindowsAzure.StorageClient.CloudBlob blob =
blobContainer.GetBlobReference(fileBase.FileName);
using (BlobStream blobStream = blob.OpenWrite())
{
string encryptionKey = //somekey;
byte[] file = new byte[fileBase.ContentLength];
EncDecAlgo.EncryptBlobFile(file, blobStream, encryptionKey);
}
}
}
public void EncryptBlobFile(byte[] file, BlobStream bs, string key)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(key,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d,
0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
CryptoStream cs = new CryptoStream(bs,
alg.CreateEncryptor(), CryptoStreamMode.Write);
foreach (var data in file)
{
cs.WriteByte((byte)data);
}
cs.Close();
bs.Close();
}
上述文件加密工作正常。
用于下载代码
public ActionResult DownloadFile(string filename)
{
// Retrieve reference to a previously created container.
Microsoft.WindowsAzure.StorageClient.CloudBlobContainer blobContainer =
_myBlobStorageService.GetCloudBlobContainer();
Microsoft.WindowsAzure.StorageClient.CloudBlob blob =
blobContainer.GetBlobReference(filename);
blob.FetchAttributes();
string encryptionKey = //same key used in encryption;
using (BlobStream blobStream = blob.OpenRead())
{
EncDecAlgo.DecryptBlobFile(blobStream, encryptionKey, filename);
}
}
public static void DecryptBlobFile(BlobStream bs, string key, string filePath)
{
try
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(key,
new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65,
0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
Rijndael alg = Rijndael.Create();
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
CryptoStream cs = new CryptoStream(bs,
alg.CreateDecryptor(), CryptoStreamMode.Read);
// Decrypt & Download Here
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
System.Web.HttpContext.Current.Response.ContentType = "application/" + Path.GetExtension(filePath).Replace(".", "");
int data;
while ((data = cs.ReadByte()) != -1)
{
if (data != 0)
{
}
System.Web.HttpContext.Current.Response.OutputStream.WriteByte((byte)data);
System.Web.HttpContext.Current.Response.Flush();
}
cs.Close();
bs.Close();
}
catch
{
}
}
下载时出现以下错误
Server cannot set content type after HTTP headers have been sent.
请提出一些解决方案。
【问题讨论】:
-
您可以尝试欺骗文件类型。如果文件是 myReport.pdf 重命名为 myReportpdf.txt。我对拉水晶报告 .rpt 文件的应用程序做了类似的事情。当我们将其发布到网站时,我们将其重命名为 zip。
-
@davek,更改文件扩展名,不会使它作为文本文件而不是 pdf 打开?它不起作用..
-
嘿@davek,实际上没有文件正在下载,我收到一个错误“发送 HTTP 标头后服务器无法设置内容类型。”,我已经编辑了我的问题
-
抱歉,您原本可以下载 txt 文件类型。上传有效吗?
-
是的,上传工作正常..
标签: asp.net-mvc-3 azure-storage azure-web-roles encryption-symmetric