我已解密 mp3 文件并将其保存到 Blob 存储中。
但是,当我解密并下载文件时,我无法播放它。我使用了一个 Mp3 验证工具,它显示“未知文件格式”。我相信解密不起作用,因为它可以下载未加密的 Mp3 文件。下面我首先展示其Azure webjob函数中的加密代码。我展示了解密方法和使用它的方法。我已经删除了对按键等的处理或清晰度。
加密
public static void EncryptBlob(
[BlobTrigger("callstest/{name}")]
[Blob("callstest/{name}", FileAccess.Read)] Stream blobInput,
[Blob("encryptedcalls/{name}.vega", FileAccess.Write)] Stream blobOutput)
{
try
{
var password = "myKey123";
var ue = new UnicodeEncoding();
var key = ue.GetBytes(password);
var rmCrypto = new RijndaelManaged {Padding = PaddingMode.None};
using (var cs = new CryptoStream(blobOutput,
rmCrypto.CreateEncryptor(key, key),
CryptoStreamMode.Write))
{
int data;
while ((data = blobInput.ReadByte()) != -1)
cs.WriteByte((byte)data);
}
}
catch
{
Trace.TraceError("an error occured during encryption of the file-get the name?");
}
}
管理 Controller
public async Task<ActionResult> DownloadMp3FromUrl()
{
var file = await _recordingService.GetRecordingFromUrl();
var fileName = "filetest.mp3";
return File(file,"audio/mpeg", fileName);
}
录音服务处理程序
public async Task<byte[]> GetRecordingFromUrl()
{
var container = _blobClient.GetContainerReference("encryptedcalls");
var blockBlob = container.GetBlockBlobReference("SearchFiles.mp3.vega");
try
{
var password = "myKey123";
var ue = new UnicodeEncoding();
var key = ue.GetBytes(password);
var rmCrypto = new RijndaelManaged { Padding = PaddingMode.None };
using (var stream = new MemoryStream())
{
blockBlob.FetchAttributes();
blockBlob.DownloadToStream(stream, null, null);
using (var cs = new CryptoStream(stream, rmCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read))
{
int data;
while ((data = stream.ReadByte()) != -1)
cs.WriteByte((byte)data);
return stream.ToArray();
}
}
}
catch
{
Trace.TraceError("an error occured during encryption of the file-get the name?");
}
return null;
}
最佳答案
您正在尝试将解密的数据写回到录制服务处理程序中的源流中。这永远不会起作用。我很惊讶这没有抛出异常。
您需要设置输入流,将其传递到解密 CryptoStream,然后将其写入另一个输出流:
using (var inStream = new MemoryStream())
using (var outStream = new MemoryStream())
{
blockBlob.FetchAttributes();
blockBlob.DownloadToStream(inStream, null, null);
using (var cryptoStream = new CryptoStream(
inStream, rmCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read))
{
cryptoStream.CopyTo(outStream);
return outStream.ToArray();
}
}
顺便说一句,您在此处介绍的实现充满安全问题:
- 不要使用非填充密码。您可以通过这种方式泄露信息。
- 不要从密码生成 key 。使用cryptographically secure RNG生成您的 key 。
- 如果您必须使用字符串作为 key 的密码,请使用 Rfc2898DeriveBytes 从密码生成加密安全的随 secret 钥。
- 绝对不要使用您的对称 key 作为 IV。这真的是非常非常糟糕的做法。 IV 用于randomize the cipher's output - 它不是像 key 那样的 secret ,并且对于每个被加密的“消息”(或文件)来说应该是唯一的。
关于C# 使用 RijndaelManaged 和 CryptoStream 解密 mp3 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28692444/