gpt4 book ai didi

ios - 分块解密媒体文件并通过 AVPlayer 播放

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

我有一个 mp4 video file我是谁encrypting保存和decrypting通过 AVPlayer 播放。 Using CRYPTOSWIFT Library for encrypting/decrypting
当我一次解密整个文件但我的文件很大并且占用 100% 的 CPU 使用率和大量内存时,它工作正常。所以,我需要分 block 解密加密文件。
我试图以 block 的形式解密文件,但它没有播放视频,因为 AVPlayer 无法识别解密的 block 数据,可能在加密文件时数据没有按顺序存储。我试过chacha20, AES, AES.CTR & AES.CBC加密和解密文件的协议(protocol),但无济于事。

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
}

我需要有关此问题的帮助,请引导我找到实现此目标的正确方法。

最佳答案

有关更深入和更完整的 CommonCrypto 包装器,请查看 my CommonCrypto wrapper .我已经为这个答案提取了点点滴滴。
首先,我们需要定义一些将进行加密/解密的函数。我现在假设您使用 AES(256) CBC 和 PKCS#7 填充。总结下面的代码片段:我们有一个更新函数,可以重复调用来消耗 block 。还有一个 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 来完成它。 .

关于ios - 分块解密媒体文件并通过 AVPlayer 播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37014686/

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