. :
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));
}
}