SWIFT - 领域数据库加密不起作用
SWIFT - Realm db encryption not working
我正在尝试加密存储在领域数据库中的数据。我跟着Sample Code mentioned on Realm's Swift page。我想加密数据而不是数据库文件。下面是我正在使用的代码:
var error: NSError? = nil
let configuration = Realm.Configuration(encryptionKey: EncryptionManager().getKey())
if let realmE = Realm(configuration: configuration, error: &error) {
// Add an object
realmE.write {
realmE.add(objects, update: T.primaryKey() != nil)
}
}
其中对象是我需要插入数据库的对象列表。下面是同样从示例代码中选取的 getKey() func 的代码:
func getKey() -> NSData {
// Identifier for our keychain entry - should be unique for your application
let keychainIdentifier = "io.Realm.test"
let keychainIdentifierData = keychainIdentifier.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
// First check in the keychain for an existing key
var query: [NSString: AnyObject] = [
kSecClass: kSecClassKey,
kSecAttrApplicationTag: keychainIdentifierData,
kSecAttrKeySizeInBits: 512,
kSecReturnData: true
]
// To avoid Swift optimization bug, should use withUnsafeMutablePointer() function to retrieve the keychain item
// See also:
var dataTypeRef: AnyObject?
var status = withUnsafeMutablePointer(&dataTypeRef) { SecItemCopyMatching(query, UnsafeMutablePointer([=12=])) }
if status == errSecSuccess {
return dataTypeRef as! NSData
}
// No pre-existing key from this application, so generate a new one
let keyData = NSMutableData(length: 64)!
let result = SecRandomCopyBytes(kSecRandomDefault, 64, UnsafeMutablePointer<UInt8>(keyData.mutableBytes))
// Store the key in the keychain
query = [
kSecClass: kSecClassKey,
kSecAttrApplicationTag: keychainIdentifierData,
kSecAttrKeySizeInBits: 512,
kSecValueData: keyData
]
status = SecItemAdd(query, nil)
assert(status == errSecSuccess, "Failed to insert the new key in the keychain")
return keyData
}
问题是代码没有加密。当我使用 Realm Browser 打开领域文件时插入数据后,代码未加密。
已在模拟器和设备上进行测试。使用 Swift 1.2,Xcode 6.4,领域 0.95。
有什么想法吗?
Realm 的加密功能仅适用于加密整个 .realm 文件的能力。没有功能可以加密 .realm 文件中的离散对象并让其余部分保持原样。
如果你真的想这样做,恐怕你需要自己滚动加密系统。
如果我要这样做,我会这样做:
- 创建一个 Realm
Object
子类并调用 NSData
属性
encryptedData
。
- 使用
NSCoding 协议。 (Saving custom SWIFT class with NSCoding to UserDefaults)
- 使用您选择的加密方法加密生成的
NSData
对象 (AES Encryption for an NSString on the iPhone)
- 获取生成的加密
NSData
对象并将其保存到您的 Realm 对象中的 encryptedData
属性。
- 当您想要检索该数据时反向执行该过程。
虽然这个过程可行,但如您所见,这是一项非常重要的额外工作,并且在此过程中您还会失去 Realm 的所有速度和内存节省优势。
我鼓励您重新考虑您的应用程序设计,看看是否可以使用 Realm 自己的加密功能。祝你好运!
我正在尝试加密存储在领域数据库中的数据。我跟着Sample Code mentioned on Realm's Swift page。我想加密数据而不是数据库文件。下面是我正在使用的代码:
var error: NSError? = nil
let configuration = Realm.Configuration(encryptionKey: EncryptionManager().getKey())
if let realmE = Realm(configuration: configuration, error: &error) {
// Add an object
realmE.write {
realmE.add(objects, update: T.primaryKey() != nil)
}
}
其中对象是我需要插入数据库的对象列表。下面是同样从示例代码中选取的 getKey() func 的代码:
func getKey() -> NSData {
// Identifier for our keychain entry - should be unique for your application
let keychainIdentifier = "io.Realm.test"
let keychainIdentifierData = keychainIdentifier.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
// First check in the keychain for an existing key
var query: [NSString: AnyObject] = [
kSecClass: kSecClassKey,
kSecAttrApplicationTag: keychainIdentifierData,
kSecAttrKeySizeInBits: 512,
kSecReturnData: true
]
// To avoid Swift optimization bug, should use withUnsafeMutablePointer() function to retrieve the keychain item
// See also:
var dataTypeRef: AnyObject?
var status = withUnsafeMutablePointer(&dataTypeRef) { SecItemCopyMatching(query, UnsafeMutablePointer([=12=])) }
if status == errSecSuccess {
return dataTypeRef as! NSData
}
// No pre-existing key from this application, so generate a new one
let keyData = NSMutableData(length: 64)!
let result = SecRandomCopyBytes(kSecRandomDefault, 64, UnsafeMutablePointer<UInt8>(keyData.mutableBytes))
// Store the key in the keychain
query = [
kSecClass: kSecClassKey,
kSecAttrApplicationTag: keychainIdentifierData,
kSecAttrKeySizeInBits: 512,
kSecValueData: keyData
]
status = SecItemAdd(query, nil)
assert(status == errSecSuccess, "Failed to insert the new key in the keychain")
return keyData
}
问题是代码没有加密。当我使用 Realm Browser 打开领域文件时插入数据后,代码未加密。
已在模拟器和设备上进行测试。使用 Swift 1.2,Xcode 6.4,领域 0.95。
有什么想法吗?
Realm 的加密功能仅适用于加密整个 .realm 文件的能力。没有功能可以加密 .realm 文件中的离散对象并让其余部分保持原样。
如果你真的想这样做,恐怕你需要自己滚动加密系统。
如果我要这样做,我会这样做:
- 创建一个 Realm
Object
子类并调用NSData
属性encryptedData
。 - 使用 NSCoding 协议。 (Saving custom SWIFT class with NSCoding to UserDefaults)
- 使用您选择的加密方法加密生成的
NSData
对象 (AES Encryption for an NSString on the iPhone) - 获取生成的加密
NSData
对象并将其保存到您的 Realm 对象中的encryptedData
属性。 - 当您想要检索该数据时反向执行该过程。
虽然这个过程可行,但如您所见,这是一项非常重要的额外工作,并且在此过程中您还会失去 Realm 的所有速度和内存节省优势。
我鼓励您重新考虑您的应用程序设计,看看是否可以使用 Realm 自己的加密功能。祝你好运!