【问题标题】:C# Decrypting mp3 file using RijndaelManaged and CryptoStreamC# 使用 RijndaelManaged 和 CryptoStream 解密 mp3 文件
【发布时间】:2015-02-24 18:13:12
【问题描述】:

我已解密一个 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?");
        }
    }

管理员控制器

 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;
    }

【问题讨论】:

    标签: c# azure encryption aes


    【解决方案1】:

    您正在尝试将解密的数据写入录制服务处理程序的源流中。这永远不会奏效。我很惊讶这不会引发异常。

    您需要设置输入流,将其传递到解密的 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();
        }
    }
    

    顺便说一句,您在此处介绍的实现充满了安全问题:

    • 不要使用非填充密码。您可以通过这种方式泄露信息。
    • 不要根据密码生成密钥。使用cryptographically secure RNG 生成您的密钥。
    • 如果您必须使用字符串作为密钥的密码,请使用 Rfc2898DeriveBytes 从密码中生成加密安全的随机密钥。
    • 绝对不要使用您的对称密钥作为您的 IV。这是非常非常糟糕的做法。 IV 用于randomize the cipher's output - 它与密钥不同,它不是秘密,并且对于每个被加密的“消息”(或文件)应该是唯一的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      相关资源
      最近更新 更多