2017-10-06 96 views
0

我需要加密領域文件,我喜歡這樣的應用程序啓動。加密領域數據庫,並崩潰

- (void)encryptRealm { 
    // Generate 64 bytes of random data to serve as the encryption key 
    uint8_t buffer[64]; 
    SecRandomCopyBytes(kSecRandomDefault, 64, buffer); 
    NSData *keyData = [[NSData alloc] initWithBytes:buffer length:sizeof(buffer)]; 

    // Create a Realm Configuration object with the encryption key 
    RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration]; 
    configuration.encryptionKey = keyData; 

    // Attempt to open a Realm file with the encryption key 
    NSError *error = nil; 
    RLMRealm *realm = [RLMRealm realmWithConfiguration:configuration error:&error]; 

    // If the encryption key was not accepted, the error will state that the database was invalid 
    if (error != nil) { 
     NSLog(@"%@", error.localizedDescription); 
     return; 
    } 
} 

而且它像這樣崩潰。我不知道發生了什麼。我該怎麼辦? 教程從這裏開始。

https://academy.realm.io/posts/tim-oliver-realm-cocoa-tutorial-on-encryption-with-realm/

2017年10月6日15:58:33.366167 + 0800活着2.0 [20770:6589296] *** 終止應用程序由於未捕獲的異常 'RLMException',原因: 「/無功/mobile/Containers/Data/Application/FA128FC0-BB80-469E-8B05-6B7957AD04A1/Documents/default.realm: 無法打開路徑爲 '/ var/mobile/Containers/Data/Application/FA128FC0-BB80- 469E-8B05-6B7957AD04A1/Documents/default.realm': 不是Realm文件。'

+0

我假設你嘗試重新打開相同的文件有兩個不同的加密密鑰,這顯然是行不通的。您需要使用相同的加密密鑰才能第二次打開它 – EpicPandaForce

回答

2

而不是每次啓動時生成一個新的密鑰,您將需要存儲用於給定的領域的加密密鑰。在那篇文章中進一步向下鏈接加密例如有一個example of how to do this

- (NSData *)getKey { 
    // Identifier for our keychain entry - should be unique for your application 
    static const uint8_t kKeychainIdentifier[] = "io.Realm.EncryptionExampleKey"; 
    NSData *tag = [[NSData alloc] initWithBytesNoCopy:(void *)kKeychainIdentifier 
               length:sizeof(kKeychainIdentifier) 
             freeWhenDone:NO]; 

    // First check in the keychain for an existing key 
    NSDictionary *query = @{(__bridge id)kSecClass: (__bridge id)kSecClassKey, 
          (__bridge id)kSecAttrApplicationTag: tag, 
          (__bridge id)kSecAttrKeySizeInBits: @512, 
          (__bridge id)kSecReturnData: @YES}; 

    CFTypeRef dataRef = NULL; 
    OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, &dataRef); 
    if (status == errSecSuccess) { 
     return (__bridge NSData *)dataRef; 
    } 

    // No pre-existing key from this application, so generate a new one 
    uint8_t buffer[64]; 
    status = SecRandomCopyBytes(kSecRandomDefault, 64, buffer); 
    NSAssert(status == 0, @"Failed to generate random bytes for key"); 
    NSData *keyData = [[NSData alloc] initWithBytes:buffer length:sizeof(buffer)]; 

    // Store the key in the keychain 
    query = @{(__bridge id)kSecClass: (__bridge id)kSecClassKey, 
       (__bridge id)kSecAttrApplicationTag: tag, 
       (__bridge id)kSecAttrKeySizeInBits: @512, 
       (__bridge id)kSecValueData: keyData}; 

    status = SecItemAdd((__bridge CFDictionaryRef)query, NULL); 
    NSAssert(status == errSecSuccess, @"Failed to insert new key in the keychain"); 

    return keyData; 
} 
+0

謝謝。這行得通。你是最棒的。 –