【问题标题】:Playing an encrypted mp3 in iOS在 iOS 中播放加密的 mp3
【发布时间】:2012-06-18 01:06:21
【问题描述】:
我有一个 aes 加密的 mp3 文件,需要在 iOS 中播放;出于安全原因,我无法将文件解密到磁盘,并且由于内存限制,我可能无法将其解密到内存。
有没有办法直接播放加密文件,或者将其流式传输到播放器而不将其全部加载到内存中?我真的很茫然,我什至不知道从哪里开始......我正在使用 AVAudioPlayer 播放文件,但我猜它不够灵活,无法满足我的需求。
【问题讨论】:
标签:
ios
audio
encryption
mp3
avaudioplayer
【解决方案1】:
我不知道 AVAudioPlayer 是如何工作的,但解决这个问题的唯一方法是提供某种播放器可以访问的抽象。如果播放器只能访问“文件”对象,那么您很不走运,必须使用另一个播放器。如果播放器可以访问某种输入流(我怀疑它可以),您可以从文件创建一个流(称为“文件流”),并从文件流创建一个解密流。您只需要了解 AES 加密,因为您需要使用已经存在的代码(如 crypto++)来创建解密流。
在伪代码中,它看起来像这样:
filestream fs = new filestream( path )
decryptionstream ds = decryptionstream( fs, decryptionkeydata )
AVAudioPlayer.open( ds );
AVAudioPlayer.play()
在内部,AVAudioPlayer 将从解密流中读取数据块,从文件流中拉取数据,从文件流中拉取数据。数据将在解密流中被解密,一次一个块。
【解决方案2】:
对于音频或图像文件的加密和解密使用RNCryptor算法-->
https://github.com/RNCryptor/RNCryptor
加密:
let fileData = try NSData(contentsOf: fileURL, options: NSData.ReadingOptions.mappedIfSafe)
let encryptData = RNCryptor.encrypt(data: fileData as Data, withPassword: "password")
try encryptData.write(to: fileURL, options: Data.WritingOptions.completeFileProtection)
解密:(要播放音频,您必须先解密然后播放)
let fileData = try NSData(contentsOfFile: filePath, options:NSData.ReadingOptions.mappedIfSafe)
encryptData = RNCryptor.encrypt(data: fileData as Data, withPassword: password)
try encryptData.write(to: URL.init(fileURLWithPath: filePath), options: Data.WritingOptions.completeFileProtection)