Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Secure key for android database

Is there any algorithm to generate an encrypted key in android to secure a database?

I tried this PBE algorithm:

PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, 
    NUM_OF_ITERATIONS, KEY_SIZE);
SecretKeyFactory factoryKey = SecretKeyFactory.getInstance(PBE_ALGORITHM);
SecretKey tempKey = factoryKey.generateSecret(pbeKeySpec);
SecretKey secretKey = new SecretKeySpec(tempKey.getEncoded(), "AES");

But it generates the same key every time. Any other good algorithms for generating a secure key?

like image 607
Monty Avatar asked Jun 04 '26 10:06

Monty


1 Answers

To generate a random secret key, use the KeyGenerator class, with code something like this:

KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(KEY_SIZE);
SecretKey skey = kgen.generateKey();

Note that you will obviously have to store this key securely somewhere if you wish to decrypt your database later, hence it may be worthwhile to pursue the PBE-based solution proposed in your question.

like image 62
Peter Elliott Avatar answered Jun 05 '26 23:06

Peter Elliott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!