Realm 中的加密给出 java.lang.IllegalStateException:用于解密 Realm 的错误密钥
Encryption in Realm gives java.lang.IllegalStateException: Wrong key used to decrypt Realm
我在 Realm 中使用加密将数据插入数据库。我必须将数据插入不同的表,并试图加密表中的数据。但我刚得到 java.lang.IllegalStateException:用于解密领域的密钥错误。
我的代码:
DatabaseHandlerRealm.java
public class DatabaseHandlerRealm
{
private Realm realm;
byte[] key, GenKey;
public int id;
public void AddBusRoute(Context context, int Id, String RouteName, String Remarks)
{
if(realm == null) {
id = 1;
key = KeyGenerator();
realm = Realm.getInstance(context, key);
}
realm.beginTransaction();
BusRouteSchema busRouteSchema = realm.createObject(BusRouteSchema.class);
busRouteSchema.setId(Id);
busRouteSchema.setRouteName(RouteName);
busRouteSchema.setRemarks(Remarks);
realm.commitTransaction();
}
public void AddBusRouteLocation(Context context, int Id, String LocationName, String Remarks)
{
if(realm == null)
{
id = 2;
key = KeyGenerator();
realm = Realm.getInstance(context, key);
}
realm.beginTransaction();
BusRouteLocationSchema busRouteLocationSchema = realm.createObject(BusRouteLocationSchema.class);
busRouteLocationSchema.setId(Id);
busRouteLocationSchema.setLocationName(LocationName);
busRouteLocationSchema.setRemarks(Remarks);
realm.commitTransaction();
}
public byte[] KeyGenerator()
{
if(GenKey == null) {
GenKey = new byte[64];
new SecureRandom().nextBytes(GenKey);
}
else {
GenKey = new byte[64];
new SecureRandom().nextBytes(GenKey);
}
return GenKey;
}
}
MyActivity.java
//to insert data into database
DatabaseHandlerRealm databaseHandlerRealm = new DatabaseHandlerRealm();
for (BusRoute busRoute : MainActivity.this.busRoutes) {
databaseHandlerRealm.AddBusRoute(getApplication(),busRoute.getId(),busRoute.getRouteName(),busRoute.getRemarks());
}
//called from seperate functions.
DatabaseHandlerRealm databaseHandlerRealm = new DatabaseHandlerRealm();
for (BusRouteLocation busRouteLocation : MainActivity.this.busRouteLocations) {
databaseHandlerRealm.AddBusRouteLocation(getApplication(), busRouteLocation.Id, busRouteLocation.LocationName, busRouteLocation.Remarks);
}
日志:
06-05 17:04:48.991 10326-10326/com.myapp.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Wrong key used to decrypt Realm.
at io.realm.Realm.createAndValidate(Realm.java:490)
at io.realm.Realm.create(Realm.java:471)
at io.realm.Realm.getInstance(Realm.java:389)
at io.realm.Realm.getInstance(Realm.java:370)
at com.myapp.app.DatabaseHandlerRealm.AddBusRouteLocation(DatabaseHandlerRealm.java:42)
at com.cdass.pokharabus.MainActivity.run(MainActivity.java:232)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
我应该为两者使用不同的加密密钥还是可以在每笔交易中重复使用相同的加密密钥?
加密密钥用于 encrypt/decrypt 实际文件。因此它必须在所有交易中都相同。这也是为什么您需要找到一种方法来在应用程序重新启动之间保存密钥,否则您将无法再次访问 Realm。我们的加密示例中有一些链接介绍了如何进行此操作:https://github.com/realm/realm-java/blob/master/examples/encryptionExample/src/main/java/io/realm/examples/encryptionexample/EncryptionExampleActivity.java#L40-Lundefined
我在 Realm 中使用加密将数据插入数据库。我必须将数据插入不同的表,并试图加密表中的数据。但我刚得到 java.lang.IllegalStateException:用于解密领域的密钥错误。
我的代码:
DatabaseHandlerRealm.java
public class DatabaseHandlerRealm
{
private Realm realm;
byte[] key, GenKey;
public int id;
public void AddBusRoute(Context context, int Id, String RouteName, String Remarks)
{
if(realm == null) {
id = 1;
key = KeyGenerator();
realm = Realm.getInstance(context, key);
}
realm.beginTransaction();
BusRouteSchema busRouteSchema = realm.createObject(BusRouteSchema.class);
busRouteSchema.setId(Id);
busRouteSchema.setRouteName(RouteName);
busRouteSchema.setRemarks(Remarks);
realm.commitTransaction();
}
public void AddBusRouteLocation(Context context, int Id, String LocationName, String Remarks)
{
if(realm == null)
{
id = 2;
key = KeyGenerator();
realm = Realm.getInstance(context, key);
}
realm.beginTransaction();
BusRouteLocationSchema busRouteLocationSchema = realm.createObject(BusRouteLocationSchema.class);
busRouteLocationSchema.setId(Id);
busRouteLocationSchema.setLocationName(LocationName);
busRouteLocationSchema.setRemarks(Remarks);
realm.commitTransaction();
}
public byte[] KeyGenerator()
{
if(GenKey == null) {
GenKey = new byte[64];
new SecureRandom().nextBytes(GenKey);
}
else {
GenKey = new byte[64];
new SecureRandom().nextBytes(GenKey);
}
return GenKey;
}
}
MyActivity.java
//to insert data into database
DatabaseHandlerRealm databaseHandlerRealm = new DatabaseHandlerRealm();
for (BusRoute busRoute : MainActivity.this.busRoutes) {
databaseHandlerRealm.AddBusRoute(getApplication(),busRoute.getId(),busRoute.getRouteName(),busRoute.getRemarks());
}
//called from seperate functions.
DatabaseHandlerRealm databaseHandlerRealm = new DatabaseHandlerRealm();
for (BusRouteLocation busRouteLocation : MainActivity.this.busRouteLocations) {
databaseHandlerRealm.AddBusRouteLocation(getApplication(), busRouteLocation.Id, busRouteLocation.LocationName, busRouteLocation.Remarks);
}
日志:
06-05 17:04:48.991 10326-10326/com.myapp.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Wrong key used to decrypt Realm.
at io.realm.Realm.createAndValidate(Realm.java:490)
at io.realm.Realm.create(Realm.java:471)
at io.realm.Realm.getInstance(Realm.java:389)
at io.realm.Realm.getInstance(Realm.java:370)
at com.myapp.app.DatabaseHandlerRealm.AddBusRouteLocation(DatabaseHandlerRealm.java:42)
at com.cdass.pokharabus.MainActivity.run(MainActivity.java:232)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
我应该为两者使用不同的加密密钥还是可以在每笔交易中重复使用相同的加密密钥?
加密密钥用于 encrypt/decrypt 实际文件。因此它必须在所有交易中都相同。这也是为什么您需要找到一种方法来在应用程序重新启动之间保存密钥,否则您将无法再次访问 Realm。我们的加密示例中有一些链接介绍了如何进行此操作:https://github.com/realm/realm-java/blob/master/examples/encryptionExample/src/main/java/io/realm/examples/encryptionexample/EncryptionExampleActivity.java#L40-Lundefined