Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize the Keystore

This my code used for usage of key store to save an arbitrary text as a key in the keystore how I am getting the "Keystore is not initialized error", how can I initialise the Keystore?

public void secretKeyGeneration(View view) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {

    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    byte[]  sek = "eru9tyighw34ilty348934i34uiq34q34ri".getBytes();
    SecretKey sk = new SecretKeySpec(sek, 0, sek.length, "AES");    
    char[] password = "keystorepassword".toCharArray();
    KeyStore.ProtectionParameter protParam = 
    new KeyStore.PasswordProtection(password);

    KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(sk);
    ks.setEntry("secretKeyAlias", skEntry, protParam);

    }   
like image 772
user3083447 Avatar asked Aug 31 '14 10:08

user3083447


People also ask

How do I create a keystore?

Generate an upload key and keystoreIn the menu bar, click Build > Generate Signed Bundle/APK. In the Generate Signed Bundle or APK dialog, select Android App Bundle or APK and click Next. Below the field for Key store path, click Create new.

How do you load a key store?

To load a Keystore, we use the KeyStore load() method. The load contains two parameters: An InputStream which tells from where the KeyStore data has to be loaded. A char array which stores the password of the KeyStore.

What is keystore entry?

This type of entry contains a single public key Certificate belonging to another party. It is called a trusted certificate because the keystore owner trusts that the public key in the certificate indeed belongs to the identity identified by the subject (owner) of the certificate.

Where keystore file is created in Java?

By default, Java has a keystore file located at JAVA_HOME/jre/lib/security/cacerts.


1 Answers

Keystores have to be initialized and hence you have to call the Keystore.load(...) method. In your case you can for instance invoke:

KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null, null);
byte[]  sek = "eru9tyighw34ilty348934i34uiq34q34ri".getBytes();
...
like image 103
Jcs Avatar answered Oct 12 '22 23:10

Jcs