project-imas / encrypted-core-data

v2.0 - iOS Core Data encrypted SQLite store using SQLCipher
Other
787 stars 237 forks source link

Migration Old unencrypted Core database to Encrypted Core Data base. #332

Open emorphisgit opened 4 years ago

emorphisgit commented 4 years ago

I have a live iOS application on apple app-store which is using the core data. Now I want to encrypt the core-data using EncryptedCoreData (https://github.com/project-imas/encrypted-core-data 2). I am successfully able to implement the same with my project and it working fine for fresh installed but when I tried to override my existing application which is not using the encrypted coredata with the new application which is using the encrypted coredata at that time getting error Error Domain=EncryptedStoreErrorDomain Code=6000 "Incorrect passcode" UserInfo={NSLocalizedDescription=Incorrect passcode, NSUnderlyingError=0x6000039e4810 {Error Domain=NSSQLiteErrorDomain Code=26 "(null)" UserInfo={EncryptedStoreErrorMessage=file is not a database}}}

Following is my old code sample

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (persistentStoreCoordinator_ != nil) {
        return persistentStoreCoordinator_;
    }
    else if (!storeUrl)
        return nil;

    NSError *error = nil;
    persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];

        NSDictionary *options = @{
                                        EncryptedStorePassphraseKey : @"123456",
                                        EncryptedStoreFileManagerOption : [EncryptedStoreFileManager defaultManager],
                                        NSMigratePersistentStoresAutomaticallyOption : @YES
                                        };

    NSPersistentStore *store = [persistentStoreCoordinator_
                                addPersistentStoreWithType:EncryptedStoreType
                                configuration:nil
                                URL:storeUrl
                                options:options
                                error:&error];

    if (!store && error)
    {
        NSLog(@"%@",error.description);
        // Here is the error
        // Error Domain=EncryptedStoreErrorDomain Code=6000 "Incorrect passcode" UserInfo={NSLocalizedDescription=Incorrect passcode, NSUnderlyingError=0x6000039e4810 {Error Domain=NSSQLiteErrorDomain Code=26 "(null)" UserInfo={EncryptedStoreErrorMessage=file is not a database}}}
    }

    return persistentStoreCoordinator_;
}

How I can resolve this issue, I want to implement Encrypted coredata in such a way so the existing user(those having unencrypted DB) does not affect and there DB should be converted into Encrypted core data without losing their data.

iprabhakarpatil commented 3 years ago

Did you find a solution to this? Am facing the same issue.

RamyaThirunavukkarasu commented 3 years ago

Did you find a solution to this? Am facing the same issue.