我从以下链接https://github.com/realm/realm-java/tree/master/examples/encryptionExample/src/main/java/io/realm/examples/encryptionexample创建了Realm Encryption Example项目源.当我运行时项目没有任何代码更改,它运行没有错误.但我在源代码中注释以下行
Realm.deleteRealm(realmConfiguration);
Run Code Online (Sandbox Code Playgroud)
因为我不需要删除旧文件.然后我启动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.
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我有同样的问题.只需更改您的文件名称,如下所示:
RealmConfiguration config = new RealmConfiguration.Builder(getActivity().getBaseContext())
.name("myrealm_designtv.realm")
.encryptionKey(loadkeyStore())
.build();
Realm.setDefaultConfiguration(config);
Run Code Online (Sandbox Code Playgroud)
并且还要更改加密密钥,这不是正确的方法.使用密钥库.这是我使用的教程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;
}
Run Code Online (Sandbox Code Playgroud)
/*你也需要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));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7827 次 |
| 最近记录: |