How to protect iOS bundle files like plist ,image,sqlite,media files

Ravindhiran picture Ravindhiran · Mar 1, 2014 · Viewed 8.8k times · Source

I have created sample hello world project and then added Data.plist file to resource folder. Now people can easily get the bundle files by unzipping the .ipa file. Is there any ways to protect the Data.plist file that saved in the application bundle of iPhone app?

 Encryption is a decent method of scrambling the data but i don't know how to implement encription concept.

Do you have any sample code?

enter image description here

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
    NSArray *arrData = [[NSArray alloc]initWithContentsOfFile:filePath];

    NSData *datas = [NSKeyedArchiver archivedDataWithRootObject:arrData];
    [datas writeToFile:filePath atomically:YES];

After extracting IPA file

enter image description here

Answer

Daij-Djan picture Daij-Djan · Mar 1, 2014
  1. encrypt the files on the mac... during deployment:

    FIRST: don't add the files to be encrypted to the target
    e.g. Encryption-Test.plist

    THEN add a shell script phase to your xcode project to use openssl to encrypt&copy the files.
    e.g.
    openssl enc -e -aes-256-cbc -pass pass:asdasd -in $PROJECT_DIR/test/Encryption-Test.plist -out $TARGET_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH/Encryption-Test.enc

  2. add RNCryptor source files from github to your project. This makes decryption of openSSL encrypted AES files really easy. (Thanks rob!) https://github.com/RNCryptor/RNCryptor (Apple's CCrypt api isn't nice to work with directly)

  3. load the data and decrypt it:

e.g.

@implementation TestViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Encryption-Test" ofType:@"enc"];
    NSData *passEncryptedData =[[NSData alloc] initWithContentsOfFile:path];
    NSString *pass = @"asdasd";

    NSData *dataDecrypted = [RNOpenSSLDecryptor decryptData:passEncryptedData withSettings:kRNCryptorAES256Settings password:pass error:nil];
    id plist = [NSPropertyListSerialization propertyListFromData:dataDecrypted mutabilityOption:NSPropertyListImmutable format:nil errorDescription:nil];

    assert(plist);
    self.text.text = [plist description];
}

@end

added full sample: https://github.com/Daij-Djan/encryptBundleFiles