Reputation: 3349
I know that there are a few postings on this, but just want to make sure there is something that I am not missing / current.
Using sqlcipher, with an unencrypted database, want to encrypt it. Encrypting a new database is working fine.
Am trying the sqlcipher rekey with an existing database seems NOT to be working (Database remains unencrypted).
[fmdb open];
NSString *sel = @"SELECT count(*) FROM sqlite_master";
FMResultSet *fmr = [self executeQuery : fmdb : sel];
if ( [fmr next] ) // unencrypted
{
NSLog(@"Encrypting");
fmdb.key = @"";
[fmdb rekey : @"somekey"];
}
Otherwise will have to use one of the other PRAGMA methods, etc.
Does rekey only work with databases that are already encrypted?
This is using the FMDatabase Framework, but under the hood in the framework it is doing ...
- (BOOL)rekey:(NSString*)key {
#ifdef SQLITE_HAS_CODEC
if (!key) {
return NO;
}
int rc = sqlite3_rekey(db, [key UTF8String], (int)strlen([key UTF8String]));
if (rc != SQLITE_OK) {
NSLog(@"error on rekey: %d", rc);
NSLog(@"%@", [self lastErrorMessage]);
}
return (rc == SQLITE_OK);
#else
return NO;
#endif
}
It does run though the sqlite3_rekey, no errors, but database does not get encrypted.
Upvotes: 0
Views: 3231
Reputation: 3349
The trick was that when the database is used to check for encryption (next time opening app) when it is already encrypted, but do not use a key to do a select, this will fail, but then the database will HAVE to be closed and reopened again with the key.
Upvotes: 0
Reputation: 1523
All of the previous comments on this question are incorrect. You cannot use rekey to encrypt a plaintext database. Rekey is only to be used to change the encryption key on an encrypted database.
The correct way to encrypt a plaintext database is attach and export - see examples here http://sqlcipher.net/sqlcipher-api/#sqlcipher_export
Upvotes: 4