我已经从以下链接https://github.com/realm/realm-java/tree/master/examples/encryptionExample/src/main/java/io/realm/examples/encryptionexample创建了Realm Encryption Example项目源。当我在没有任何代码更改的情况下运行项目时,它运行时没有错误。但我在源代码中注释以下行
Realm.deleteRealm(realmConfiguration);
因为不需要为我删除旧文件。 然后我启动application.it抛出错误java.lang.IllegalArgumentException:非法参数:领域文件的格式无效。
如何使用相同的加密密钥读取Realm文件。
源代码:
package io.realm.examples.encryptionexample;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import java.security.SecureRandom;
import io.realm.Realm;
import io.realm.RealmConfiguration;
public class EncryptionExampleActivity extends Activity {
public static final String TAG = EncryptionExampleActivity.class.getName();
private Realm realm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Generate a key
// IMPORTANT! This is a silly way to generate a key. It is also never stored.
// For proper key handling please consult:
// * https://developer.android.com/training/articles/keystore.html
// * http://nelenkov.blogspot.dk/2012/05/storing-application-secrets-in-androids.html
byte[] key = new byte[64];
new SecureRandom().nextBytes(key);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
.encryptionKey(key)
.build();
// Start with a clean slate every time
Realm.deleteRealm(realmConfiguration);
// Open the Realm with encryption enabled
realm = Realm.getInstance(realmConfiguration);
// Everything continues to work as normal except for that the file is encrypted on disk
realm.beginTransaction();
Person person = realm.createObject(Person.class);
person.setName("Happy Person");
person.setAge(14);
realm.commitTransaction();
person = realm.where(Person.class).findFirst();
Log.i(TAG, String.format("Person name: %s", person.getName()));
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close(); // Remember to close Realm when done.
}
}
先谢谢。
答案 0 :(得分:8)
我遇到了同样的问题。只需更改文件名称,如下所示:
RealmConfiguration config = new RealmConfiguration.Builder(getActivity().getBaseContext())
.name("myrealm_designtv.realm")
.encryptionKey(loadkeyStore())
.build();
Realm.setDefaultConfiguration(config);
并且还要更改加密密钥,这不是正确的方法。使用密钥库。这是我使用的教程http://www.androidauthority.com/use-android-keystore-store-passwords-sensitive-information-623779/
如果您需要更多帮助,我可以给您loadkeyStore()
功能(我没有这样做,因为这不是问题)。
/////////////////这里我的laodKeyStore功能///////////
public byte[] loadkey(Context context) {
byte[] content = new byte[64];
try {
if (ks == null) {
createNewKeys(context);
}
ks = KeyStore.getInstance("AndroidKeyStore");
ks.load(null);
content= ks.getCertificate(ALIAS).getEncoded();
Log.e(TAG, "key original :" + Arrays.toString(content));
} catch (KeyStoreException | CertificateException | IOException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
content = Arrays.copyOfRange(content, 0, 64);
return content;
}
/ * 而且你也需要createNewKeys功能 * /
public void createNewKeys(Context context) throws KeyStoreException {
firstloadKeyStore();
try {
// Create new key if needed
if (!ks.containsAlias(ALIAS)) {
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 1);
KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
.setAlias(ALIAS)
.setSubject(new X500Principal("CN=Sample Name, O=Android Authority"))
.setSerialNumber(BigInteger.ONE)
.setStartDate(start.getTime())
.setEndDate(end.getTime())
.build();
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
generator.initialize(spec);
KeyPair keyPair = generator.generateKeyPair();
Log.e(TAG, "key :" + keyPair.getPrivate().getEncoded().toString());
}
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}
}