打开加密领域总是错误的密钥

时间:2018-01-30 09:07:29

标签: java android realm

我尝试在Android中使用Realm加密,这样用户无法在没有密码的情况下打开数据库。 我尝试this example,但是当我尝试使用Realm StudioRealm Browser for Android打开Realm文件时,密钥总是错误的。 我使用在logcat上打印的密钥。

我已尝试对密钥(字节)进行硬编码,因此密钥永远不会改变,但没有任何反应。当我尝试打开Realm文件时,密钥总是错误的。

这是我尝试的代码:

public class MyApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();
    Realm.init(this);
}}

活动

public class MainActivity extends Activity {

public static final String TAG = MainActivity.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];
    key[0] = -54;
    key[1] = 73;
    key[2] = 91;
    key[3] = -7;
    key[4] = -25;
    key[5] = -37;
    key[6] = 85;
    key[7] = -33;
    key[8] = -98;
    key[9] = -63;
    key[10] = -12;
    key[11] = -32;
    key[12] = 87;
    key[13] = -51;
    key[14] = -70;
    key[15] = -44;
    key[16] = -82;
    key[17] = -78;
    key[18] = -118;
    key[19] = -22;
    key[20] = 2;
    key[21] = 14;
    key[22] = 15;
    key[23] = 19;
    key[24] = 111;
    key[25] = -36;
    key[26] = -96;
    key[27] = -93;
    key[28] = 91;
    key[29] = 6;
    key[30] = 38;
    key[31] = 74;
    key[32] = -7;
    key[33] = -82;
    key[34] = 6;
    key[35] = -128;
    key[36] = 71;
    key[37] = 39;
    key[38] = 9;
    key[39] = -64;
    key[40] = -14;
    key[41] = 6;
    key[42] = 106;
    key[43] = -40;
    key[44] = 106;
    key[45] = -92;
    key[46] = 94;
    key[47] = 8;
    key[48] = -16;
    key[49] = -127;
    key[50] = 69;
    key[51] = 22;
    key[52] = 126;
    key[53] = 63;
    key[54] = 110;
    key[55] = 77;
    key[56] = -72;
    key[57] = -3;
    key[58] = 126;
    key[59] = -90;
    key[60] = -128;
    key[61] = -11;
    key[62] = 19;
    key[63] = 105;

    // An encrypted Realm file can be opened in Realm Studio by using a Hex encoded version
    // of the key. Copy the key from Logcat, then download the Realm file from the device using
    // the method described here: https://stackoverflow.com/a/28486297/1389357
    // The path is normally `/data/data/io.realm.examples.encryption/files/default.realm`
    Log.i("RealmEncryptionKey", Util.bytesToHex(key));

    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
            .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.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            Person person = realm.createObject(Person.class);
            person.setName("Happy Person");
            person.setAge(14);
        }
    });

    Person 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.
}}

Util for get string byte to Hex

public class Util {

private final static char[] hexArray = "0123456789ABCDEF".toCharArray();

//Original source: https://stackoverflow.com/a/9855338/1389357
public static String bytesToHex(byte[] bytes) {
    char[] hexChars = new char[bytes.length * 2];
    for ( int j = 0; j < bytes.length; j++ ) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}}

请告诉我我的错误在哪里。

谢谢。

0 个答案:

没有答案