1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| /** encrypt sqlite database to new file */
+ (BOOL)encryptDatabase:(NSString *)sourcePath targetPath:(NSString *)targetPath encryptKey:(NSString *)encryptKey { const char* sqlQ = [[NSString stringWithFormat:@"ATTACH DATABASE '%@' AS encrypted KEY '%@';", targetPath, encryptKey] UTF8String]; sqlite3 *unencrypted_DB; if (sqlite3_open([sourcePath UTF8String], &unencrypted_DB) == SQLITE_OK) { char *errmsg; // Attach empty encrypted database to unencrypted database sqlite3_exec(unencrypted_DB, sqlQ, NULL, NULL, &errmsg); if (errmsg) { NSLog(@"%@", [NSString stringWithUTF8String:errmsg]); sqlite3_close(unencrypted_DB); return NO; } // export database sqlite3_exec(unencrypted_DB, "SELECT sqlcipher_export('encrypted');", NULL, NULL, &errmsg); if (errmsg) { NSLog(@"%@", [NSString stringWithUTF8String:errmsg]); sqlite3_close(unencrypted_DB); return NO; } // Detach encrypted database sqlite3_exec(unencrypted_DB, "DETACH DATABASE encrypted;", NULL, NULL, &errmsg); if (errmsg) { NSLog(@"%@", [NSString stringWithUTF8String:errmsg]); sqlite3_close(unencrypted_DB); return NO; } sqlite3_close(unencrypted_DB); return YES; } else { sqlite3_close(unencrypted_DB); NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(unencrypted_DB)); return NO; } } /** decrypt sqlite database to new file */ + (BOOL)unEncryptDatabase:(NSString *)sourcePath targetPath:(NSString *)targetPath encryptKey:(NSString *)encryptKey { const char* sqlQ = [[NSString stringWithFormat:@"ATTACH DATABASE '%@' AS plaintext KEY '';", targetPath] UTF8String]; sqlite3 *encrypted_DB; if (sqlite3_open([sourcePath UTF8String], &encrypted_DB) == SQLITE_OK) { char* errmsg; sqlite3_exec(encrypted_DB, [[NSString stringWithFormat:@"PRAGMA key = '%@';", encryptKey] UTF8String], NULL, NULL, &errmsg); // Attach empty unencrypted database to encrypted database sqlite3_exec(encrypted_DB, sqlQ, NULL, NULL, &errmsg); if (errmsg) { NSLog(@"%@", [NSString stringWithUTF8String:errmsg]); sqlite3_close(encrypted_DB); return NO; } // export database sqlite3_exec(encrypted_DB, "SELECT sqlcipher_export('plaintext');", NULL, NULL, &errmsg); if (errmsg) { NSLog(@"%@", [NSString stringWithUTF8String:errmsg]); sqlite3_close(encrypted_DB); return NO; } // Detach unencrypted database sqlite3_exec(encrypted_DB, "DETACH DATABASE plaintext;", NULL, NULL, &errmsg); if (errmsg) { NSLog(@"%@", [NSString stringWithUTF8String:errmsg]); sqlite3_close(encrypted_DB); return NO; } sqlite3_close(encrypted_DB); return YES; } else { sqlite3_close(encrypted_DB); NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(encrypted_DB)); return NO; } } /** change secretKey for sqlite database */ + (BOOL)changeKey:(NSString *)dbPath originKey:(NSString *)originKey newKey:(NSString *)newKey { sqlite3 *encrypted_DB; if (sqlite3_open([dbPath UTF8String], &encrypted_DB) == SQLITE_OK) { sqlite3_exec(encrypted_DB, [[NSString stringWithFormat:@"PRAGMA key = '%@';", originKey] UTF8String], NULL, NULL, NULL); sqlite3_exec(encrypted_DB, [[NSString stringWithFormat:@"PRAGMA rekey = '%@';", newKey] UTF8String], NULL, NULL, NULL); sqlite3_close(encrypted_DB); return YES; } else { sqlite3_close(encrypted_DB); NSAssert1(NO, @"Failed to open database with message '%s'.", sqlite3_errmsg(encrypted_DB)); return NO; } }
|