java – Safe storage of encryption keys

Question:

Is it possible in Java for android to store encryption keys in a secure way, for example, protected with a password?

I used to generate it like this:

final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(4096, new SecureRandom());
final KeyPair key = keyGen.generateKeyPair();

Answer:

You can use the KeyStore to store encrypted keys. The key is generated as usual and placed in the repository.

final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(4096, new SecureRandom());
final KeyPair key = keyGen.generateKeyPair();

SecretKey mySecretKey = key.getPrivate();
KeyStore.ProtectionParameter protParam =
    new KeyStore.PasswordProtection("password");
KeyStore.SecretKeyEntry skEntry =
    new KeyStore.SecretKeyEntry(mySecretKey);
keyStore.setEntry("MySecretKey", skEntry, protParam);
Scroll to Top