gpt4 book ai didi

asp.net-mvc-3 - 从 Window Azure 存储下载加密文件

转载 作者:行者123 更新时间:2023-12-01 12:21:15 25 4
gpt4 key购买 nike

我创建了一个 MVC WebRole Window Azure 应用程序,我在其中使用 SymmetricAlgorithm (Rijndael) 将加密文件上传到 Azure blob 存储,如下所示

Controller > Action 是

[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.

请提出一些解决方案。

最佳答案

这应该相当简单,希望这足以让您开始:

public class CloudFileResult : ActionResult
{
private string m_FileName;
private CloudBlobContainer m_Container;

public CloudFileResult(string imageName, CloudBlobContainer container)
{
if (string.IsNullOrEmpty(imageName))
{
throw new ArgumentNullException("imageName");
}
if (container == null)
{
throw new ArgumentNullException("container");
}

m_FileName = imageName;
m_Container = container;
}

public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Clear();
var blockBlob = m_Container.GetBlockBlobReference(m_FileName);
blockBlob.FetchAttributes();
context.HttpContext.Response.ContentType = blockBlob.Metadata["ContentType"];
const string key = "my secret";
using (var pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }))
{
using (var alg = RijndaelManaged.Create())
{
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);
using (var stream = new CryptoStream(context.HttpContext.Response.OutputStream, alg.CreateDecryptor(), CryptoStreamMode.Write))
{
blockBlob.DownloadToStream(stream);
}
}
}
}
}

static void UploadFileToCloud(CloudBlobContainer container, HttpPostedFileBase file)
{
const string key = "my secret";
using (var pdb = new Rfc2898DeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }))
{
using (var alg = RijndaelManaged.Create())
{
alg.Key = pdb.GetBytes(32);
alg.IV = pdb.GetBytes(16);

var blockBlob = container.GetBlockBlobReference(file.FileName);
using (var stream = new CryptoStream(file.InputStream, alg.CreateEncryptor(), CryptoStreamMode.Read))
{
blockBlob.UploadFromStream(stream);
}
blockBlob.Metadata.Add("ContentType", file.ContentType);
blockBlob.SetMetadata();
}
}
}

static CloudBlobContainer GetContainer()
{
string connection = "DefaultEndpointsProtocol=http;AccountName=AzureAccount;AccountKey=AzureAccountKey;";
var account = CloudStorageAccount.Parse(connection);
var client = account.CreateCloudBlobClient();
var container = client.GetContainerReference("container");
return container;
}

至于下载,您可以简单使用:
[HttpGet]
public ActionResult Index(string fileName)
{
if (!string.IsNullOrEmpty(fileName))
{
return new CloudFileResult(fileName, GetContainer());
}
return View();
}

指针:
  • 我更喜欢使用托管加密算法
  • 我将原始文件的内容类型存储在 blob 元数据中(所以你知道如何提供它)
  • catch {} 让我毛骨悚然,至少在某处记录异常
  • 与其玩 HttpContext.Response,不如创建自定义 ActionResult
  • 总是处理 IDisposable 的东西
  • 关于asp.net-mvc-3 - 从 Window Azure 存储下载加密文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23744500/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com