Reputation: 107
I'm currently working on a project where we want encryption, but can't use servers (We're not allowed). I'm making my key with this line of code right now:
key = KeyGenerator.getInstance("AES").generateKey();
All the encryption and decryption works perfectly with it. Problem is, evertime you kill the app and run it again, nothing works because a new key is created. All the previously crypted data cannot be decrypted anymore with this new key.
I need to make a function that gets the same key everytime I open my app. That's why I want to use the keystore (I'm not allowed to store the key on the device, I can only use the Android Keystore or RAM).
Any idea how to do this exactly ? The only links I found online are for Pair Keys.
Upvotes: 2
Views: 1465
Reputation: 107
Finally manage to make it work ! Using this link I made a base code that wasn't working, but with some help from other stack overflow post I made it work out.
Here is the solution for anyone else that might need it :
This should be in something like your MainActivity java class.
public KeyStore ks;
@Override protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try{
// Get Keystore
ks = KeyStore.getInstance(KeyStore.getDefaultType());
SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
if (isFirstRun)
{
SharedPreferences.Editor editor = wmbPreference.edit();
editor.putBoolean("FIRSTRUN", false);
editor.commit();
ks.load(null, password);
GenerateKey();
} else {
LoadKey();
}
}
catch(Exception ex){
ex.printStackTrace();
}
myClassNeedingTheKey.secretKey = key;
}
It state that at the first run of your app, it creates the keystore and the secret key that goes with it (we also save everything). If it's not the first time you run the app, then load the key. Then before ending the onCreate we pass the key to whoever needs it.
Here is the actual code to Generate, Save and Load. This is also in a typical MainActivity class since its used by the code you put in onCreate.
public SecretKey key;
public char[] password = "1234567890".toCharArray();
void GenerateKey(){
try {
// Get and Convert the Key
key = KeyGenerator.getInstance("AES").generateKey();
SaveKey();
}
catch(Exception ex){
ex.printStackTrace();
}
}
void SaveKey(){
try{
// Save my secret key
KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry(key);
ks.setEntry("SecretKeyAlias", secretKeyEntry,null);
// Save the keystore
FileOutputStream fos = new FileOutputStream(this.getFilesDir().getAbsolutePath() + "/OEKeyStore");
ks.store(fos, password);
}
catch(Exception ex){
ex.printStackTrace();
}
}
void LoadKey(){
try{
// Load Keystore
FileInputStream fis = new FileInputStream(this.getFilesDir().getAbsolutePath() + "/OEKeyStore");
ks.load(fis, password);
// Load the secret key
KeyStore.SecretKeyEntry secretKeyEntry = (KeyStore.SecretKeyEntry)ks.getEntry("SecretKeyAlias",null);
key = secretKeyEntry.getSecretKey();
}
catch(Exception ex){
ex.printStackTrace();
}
}
There you go ! You can then use the the secret key to do whatever you want.
Upvotes: 1