以块为单位解密媒体文件并通过AVPlayer播放

时间:2016-05-03 21:28:44

标签: ios swift cryptography swift2 avplayer

我有一个mp4 video file,我encrypting要保存,decrypting通过AVPlayer播放。 Using CRYPTOSWIFT Library for encrypting/decrypting

当我一次解密整个文件但其文件很大并且占用100%的CPU和大量内存时,它的工作正常。所以,我需要以块的形式解密加密文件。 我尝试以块的形式解密文件,但由于AVPlayer无法识别解密的块数据,因此可能在加密文件时不会按顺序存储数据。我尝试过chacha20, AES, AES.CTR & AES.CBC协议来加密和解密文件,但无济于事。

extension PlayerController: AVAssetResourceLoaderDelegate {

   func resourceLoader(resourceLoader: AVAssetResourceLoader, shouldWaitForLoadingOfRequestedResource loadingRequest: AVAssetResourceLoadingRequest) -> Bool {
      let request = loadingRequest.request
      guard let path = request.URL?.path where request.URL?.scheme == Constants.customVideoScheme else { return true }
      if let contentRequest = loadingRequest.contentInformationRequest {
         do {
            let fileAttributes = try NSFileManager.defaultManager().attributesOfItemAtPath(path)
            if let fileSizeNumber = fileAttributes[NSFileSize] {
               contentRequest.contentLength = fileSizeNumber.longLongValue
            }
         } catch { }

         if fileHandle == nil {
            fileHandle = NSFileHandle(forReadingAtPath: (request.URL?.path)!)!
         }
         contentRequest.contentType = "video/mp4"
         contentRequest.byteRangeAccessSupported = true
      }

      if let data = decryptData(loadingRequest, path: path), dataRequest = loadingRequest.dataRequest {
         dataRequest.respondWithData(data)
         loadingRequest.finishLoading()
         return true
      }
      return true
   }

   func decryptData(loadingRequest: AVAssetResourceLoadingRequest, path: String) -> NSData? {
      print("Current OFFSET: \(loadingRequest.dataRequest?.currentOffset)")
      print("requested OFFSET: \(loadingRequest.dataRequest?.requestedOffset)")
      print("Current Length: \(loadingRequest.dataRequest?.requestedLength)")
      if loadingRequest.contentInformationRequest != nil {
         var data = fileHandle!.readDataOfLength((loadingRequest.dataRequest?.requestedLength)!)
         fileHandle!.seekToFileOffset(0)
         data = decodeVideoData(data)!
         return data
      } else {
         fileHandle?.seekToFileOffset(UInt64((loadingRequest.dataRequest?.currentOffset)!))
         let data = fileHandle!.readDataOfLength((loadingRequest.dataRequest?.requestedLength)!)
// let data = fileHandle!.readDataOfLength(length!) ** When I use this its not playing video but play fine when try with requestedLength **
         return decodeVideoData(data)
      }
   }
}

解码代码以解码nsdata:

 func decodeVideoData(data: NSData) -> NSData? {
      if let cha = ChaCha20(key: Constants.Encryption.SecretKey, iv: Constants.Encryption.IvKey) {
         let decrypted: NSData = try! data.decrypt(cha)
         return decrypted
      }
      return nil
   }

我需要有关此问题的帮助,请指导我以正确的方式实现这一目标。

1 个答案:

答案 0 :(得分:0)

要深入了解更完整的 CommonCrypto 包装器,请查看 my CommonCrypto wrapper。我已经为这个答案提取了点点滴滴。

首先,我们需要定义一些进行加密/解密的函数。我现在假设您使用带有 PKCS#7 填充的 AES(256) CBC。总结下面的片段:我们有一个更新函数,可以重复调用它来消耗块。还有一个 final 函数可以包装任何剩余的东西(通常处理填充)。

import CommonCrypto
import Foundation

enum CryptoError: Error {
    case generic(CCCryptorStatus)
}

func getOutputLength(_ reference: CCCryptorRef?, inputLength: Int, final: Bool) -> Int {
    CCCryptorGetOutputLength(reference, inputLength, final)
}

func update(_ reference: CCCryptorRef?, data: Data) throws -> Data {
    var output = [UInt8](repeating: 0, count: getOutputLength(reference, inputLength: data.count, final: false))
    let status = data.withUnsafeBytes { dataPointer -> CCCryptorStatus in
        CCCryptorUpdate(reference, dataPointer.baseAddress, data.count, &output, output.count, nil)
    }
    guard status == kCCSuccess else {
        throw CryptoError.generic(status)
    }
    return Data(output)
}

func final(_ reference: CCCryptorRef?) throws -> Data {
    var output = [UInt8](repeating: 0, count: getOutputLength(reference, inputLength: 0, final: true))
    var moved = 0
    let status = CCCryptorFinal(reference, &output, output.count, &moved)
    guard status == kCCSuccess else {
        throw CryptoError.generic(status)
    }
    output.removeSubrange(moved...)
    return Data(output)
}

接下来,为了演示,加密。

let key = Data(repeating: 0x0a, count: kCCKeySizeAES256)
let iv = Data(repeating: 0, count: kCCBlockSizeAES128)
let bigFile = (0 ..< 0xffff).map { _ in
    return Data(repeating: UInt8.random(in: 0 ... UInt8.max), count: kCCBlockSizeAES128)
}.reduce(Data(), +)

var encryptor: CCCryptorRef?
CCCryptorCreate(CCOperation(kCCEncrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionPKCS7Padding), Array(key), key.count, Array(iv), &encryptor)

do {
    let ciphertext = try update(encryptor, data: bigFile) + final(encryptor)
    print(ciphertext)    // 1048576 bytes
} catch {
    print(error)
}

在我看来,这是一个相当大的文件。现在解密,将以类似的方式完成。

var decryptor: CCCryptorRef?
CCCryptorCreate(CCOperation(kCCDecrypt), CCAlgorithm(kCCAlgorithmAES), CCOptions(kCCOptionPKCS7Padding), Array(key), key.count, Array(iv), &decryptor)

do {
    var plaintext = Data()
    for i in 0 ..< 0xffff {
        plaintext += try update(decryptor, data: ciphertext[i * kCCBlockSizeAES128 ..< i * kCCBlockSizeAES128 + kCCBlockSizeAES128])
    }
    plaintext += try final(decryptor)
    
    print(plaintext == bigFile, plaintext)    // true 1048560 bytes
} catch {
    print(error)
}

加密器可以针对不同的模式进行更改,一旦完成也应该释放,我不太确定 update 函数上的任意输出会如何表现,但这应该足以给你一个关于如何使用 CommonCrypto 来完成的想法。