【问题标题】:Decrypt mp3 file using commoncrypto in iOS (mp3 file was encrypted using openssl)在 iOS 中使用 commoncrypto 解密 mp3 文件(mp3 文件使用 openssl 加密)
【发布时间】:2012-09-22 03:54:29
【问题描述】:

我有一个加密的 MP3 文件。该文件使用以下 openssl 方法加密

openssl enc -e -aes-128-cbc -K 00ff349830193845af43984758690213 -p -iv 0 -nosalt -in  input.mp3 -out output.mp3

然后我尝试在我的 ios 应用程序中解密文件,如下所示,

NSString *resourceDocPath = [[NSString alloc] initWithString:[[NSBundle mainBundle] bundlePath]] ;
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"output.mp3"];
NSData *key = [@"00ff349830193845af43984758690213" dataUsingEncoding:NSUTF8StringEncoding];
NSData *iv = [@"00000000000000000000000000000000" dataUsingEncoding:NSUTF8StringEncoding];
NSData *fileData = [[NSData alloc] initWithContentsOfFile:filePath];
NSData *decryptData;

CCCryptorStatus ccStatus   = kCCSuccess;
size_t          cryptBytes = 0;    // Number of bytes moved to buffer.
NSMutableData  *dataOut    = [NSMutableData dataWithLength:fileData.length + kCCBlockSizeAES128];

ccStatus = CCCrypt(kCCDecrypt,
                   kCCAlgorithmAES128,
                   kSecPaddingNone,
                   key.bytes,
                   kCCKeySizeAES128,
                   iv.bytes,
                   fileData.bytes,
                   fileData.length,
                   dataOut.mutableBytes,
                   dataOut.length,
                   &cryptBytes);

if (ccStatus != kCCSuccess) {
    NSLog(@"CCCrypt status: %d", ccStatus);
}    
dataOut.length = cryptBytes;
decryptData = dataOut;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"MyDECFile.mp3"];
[decrypt writeToFile:appFile atomically:YES];
NSLog(@"%@",documentsDirectory);

现在我在文档目录中有解密的 MP3 文件,但它没有播放,实际上它是一个垃圾输出。请建议我一种获得所需输出的方法。

【问题讨论】:

  • 我建议您尝试使用相同的工具链来加密/解密一个已知的纯文本 - 也许这样错误会变得很明显。
  • 在 HEX 编辑器中比较原始(加密)文件和 CC 加密文件。如果看起来几乎相同(但末尾或开头的某些字节不同),您需要深入研究:kSecPaddingNone(末尾)NSData *iv(文件开头)。
  • @JonasSchnelli>> 感谢您的支持,伙计。是的,您是对的..我发现 HEX 值不同。现在我通过使用github.com/rnapier/RNCryptor 解决了这个问题。

标签: ios objective-c ios5 encryption openssl


【解决方案1】:

我使用https://github.com/rnapier/RNCryptor 进行解密。

#import "ViewController.h"
#import <Security/Security.h>
#import <MediaPlayer/MediaPlayer.h>
#import "RNEncryptor.h"
#import "RNDecryptor.h"
#import "RNOpenSSLEncryptor.h"
#import "RNOpenSSLDecryptor.h"

NSString *resourceDocPath = [[NSString alloc] initWithString:[[NSBundle mainBundle] bundlePath]] ;
NSString *filePath1 = [resourceDocPath stringByAppendingPathComponent:@"output.mp3"]; //The encrypted mp3 file (with aPassword)
NSData *passEncryptedData =[[NSData alloc] initWithContentsOfFile:filePath1]; 
NSError *error;   
NSString *pass = @"aPassword";    
NSData *dataDecrypted = [RNOpenSSLDecryptor decryptData:passEncryptedData 
                                           withSettings:kRNCryptorAES256Settings
                                               password:pass
                                                  error:&error];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"passDecryptFile.mp3"]; //The Decrypted file saved here
[dataDecrypted writeToFile:appFile atomically:YES];
NSLog(@"%@",documentsDirectory);

【讨论】:

  • 要记住的重要一点是,上述代码中的解密是基于 kRNCryptorAES256Settings 的,因此当您加密文件时(如在终端中),您必须使用 AES256 加密选项。如何加密的示例:openssl aes-256-cbc -in questions.plist -out questions.encr
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
相关资源
最近更新 更多