2015-02-24 66 views
0

我解密並將mp3文件保存到blob存儲中。C#使用RijndaelManaged和CryptoStream解密mp3文件

但是,當我解密並下載文件時,我無法播放它。我使用了一個說明「未知文件格式」的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?"); 
     } 
    } 

AdminController

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

回答

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 - 與密鑰相同,它不是祕密,並且對每個正在加密的「消息」(或文件)應該是唯一的。