SWIFT - Realm db加密不起作用 [英] SWIFT - Realm db encryption not working
问题描述
我试图加密存储在领域数据库中的数据。我遵循了提到的示例代码领域的 Swift页面。我想加密的数据不是数据库文件。以下是我使用的代码:
Im trying to encrypt the data stored in the realm database. I followed the Sample Code mentioned on Realm's Swift page. I want to encrypt the data NOT the database file. Below is the code I'm using:
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也从示例代码中选取:
Where objects is a list of objects i need to insert in the database. Below is the code fore getKey() func also picked from the sample code:
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: http://stackoverflow.com/questions/24145838/querying-ios-keychain-using-swift/27721328#27721328
var dataTypeRef: AnyObject?
var status = withUnsafeMutablePointer(&dataTypeRef) { SecItemCopyMatching(query, UnsafeMutablePointer($0)) }
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 打开领域文件后插入数据后,代码未加密。
The problem is this that the code is not getting encrypted. After inserting data when i open the realm file using Realm Browser the code is NOT encrypted.
在模拟器和设备上进行测试。使用Swift 1.2,Xcode 6.4,Realm 0.95。
Tested on both simulator and device. Using Swift 1.2, Xcode 6.4, Realm 0.95.
任何想法?
推荐答案
领域的加密功能仅适用于加密整个.realm文件的能力。没有任何功能来加密.realm文件中的离散对象,并将其余部分原样保留。
Realm's encryption feature applies only to the ability to encrypt whole .realm files. There's no feature to encrypt discrete objects within the .realm file and leave the rest as-is.
如果您想要这样做,我恐怕你将需要自己滚动加密系统。
If you do want to go about doing this, I'm afraid you would need to roll the encryption system yourself.
如果我要这样做,我会这样做:
If I was going to do this, I'd do it this way:
- 创建一个领域
对象
子类与NSData
属性称为
encryptedData
。 - 使用
NSCoding协议将任何要加密的对象序列化为NSData
(使用NSCoding将自定义SWIFT类保存到UserDefaults ) - 使用您选择的加密方法( NSData 对象。 com / questions / 1400246 / aes-encryption-for-an-nsstring-on-the-iphone> AES加密为iPhone上的NSString )
- 加密的
NSData
对象,并将其保存到Realm对象中的encryptedData
属性。 - 当您要检索该数据时,反转该过程。
- Create a Realm
Object
subclass with anNSData
property calledencryptedData
. - Serialize any objects you wanted to encrypt to
NSData
using the NSCoding protocol. (Saving custom SWIFT class with NSCoding to UserDefaults) - Encrypt that resulting
NSData
object using an encryption method of your choice (AES Encryption for an NSString on the iPhone) - Take the resulting encrypted
NSData
object and save it to theencryptedData
property in your Realm object. - Reverse the process when you want to retrieve that data.
虽然此过程可以正常工作,您可以看到,多余的额外工作,您也将失去领域的所有速度和记忆节省的好处。
While this process would work, as you can see, it's a non-trivial amount of extra work, and you would also lose all of the speed and memory-saving benefits of Realm in the process.
我建议您重新考虑应用程序的设计,并查看是否可行使用领域自己的加密功能。祝你好运!
I would encourage you to rethink your app's design, and see if it is feasible to use Realm's own encryption feature after all. Good luck!
这篇关于SWIFT - Realm db加密不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!