android-lib/src/main/java/io/v/android/v23/KeyStoreUtil.java
/**
     * Generates a new private key and stores it in the Android KeyStore under the provided alias.
     * If a key already exists under the provided alias, it will be overwritten.
     * Throws an exception if the key could not be generated.
     *
     * @param  ctx             android Context.
     * @param  alias           alias under which the private key will be stored inside the KeyStore.
     * @return                 an entry storing the private key and the certificate chain for the
     *                         corresponding public key.
     * @throws VException      if the key could not be generated.
     */
Generates a new private key and stores it in the Android KeyStore under the provided alias.
If a key already exists under the provided alias, it will be overwritten.
Throws an exception if the key could not be generated.

@param  ctx             android Context.
@param  alias           alias under which the private key will be stored inside the KeyStore.
@return                 an entry storing the private key and the certificate chain for the
corresponding public key.
@throws VException      if the key could not be generated.
Generates a new private key and stores it in the Android KeyStore under the provided alias . If a key already exists under the provided alias it will be overwritten . Throws an exception if the key could not be generated . @param ctx android Context . @param alias alias under which the private key will be stored inside the KeyStore . @return an entry storing the private key and the certificate chain for the corresponding public key . @throws VException if the key could not be generated .
public static KeyStore.PrivateKeyEntry genKeyStorePrivateKey(
        android.content.Context ctx, String alias) throws VException {
        try {
            final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", KEYSTORE);
            final Calendar notBefore = Calendar.getInstance();
            final Calendar notAfter = Calendar.getInstance();
            notAfter.add(1, Calendar.YEAR);
            final KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(ctx)
                        .setAlias(alias)
                        .setKeyType(PK_ALGORITHM)
                        .setKeySize(KEY_SIZE)
                        .setSubject(new X500Principal(
                            String.format("CN=%s, OU=%s", alias, ctx.getPackageName())))
                        .setSerialNumber(BigInteger.ONE)
                        .setStartDate(notBefore.getTime())
                        .setEndDate(notAfter.getTime())
                        .build();
            keyGen.initialize(spec);
            keyGen.generateKeyPair();
            return getKeyStorePrivateKey(alias);
        } catch (NoSuchProviderException e) {
            throw new VException("Couldn't find Android KeyStore");
        } catch (NoSuchAlgorithmException e) {
            throw new VException(
                    "Keystore doesn't support " + PK_ALGORITHM + " algorithm: " + e.getMessage());
        } catch (InvalidAlgorithmParameterException e) {
            throw new VException("Invalid keystore algorithm parameters: " + e.getMessage());
        }
    }
public static KeyStore . PrivateKeyEntry genKeyStorePrivateKey ( android . content . Context ctx , String alias ) throws VException { try { final KeyPairGenerator keyGen = KeyPairGenerator . getInstance ( "RSA" , KEYSTORE ) ; final Calendar notBefore = Calendar . getInstance ( ) ; final Calendar notAfter = Calendar . getInstance ( ) ; notAfter . add ( 1 , Calendar . YEAR ) ; final KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec . Builder ( ctx ) . setAlias ( alias ) . setKeyType ( PK_ALGORITHM ) . setKeySize ( KEY_SIZE ) . setSubject ( new X500Principal ( String . format ( "CN=%s, OU=%s" , alias , ctx . getPackageName ( ) ) ) ) . setSerialNumber ( BigInteger . ONE ) . setStartDate ( notBefore . getTime ( ) ) . setEndDate ( notAfter . getTime ( ) ) . build ( ) ; keyGen . initialize ( spec ) ; keyGen . generateKeyPair ( ) ; return getKeyStorePrivateKey ( alias ) ; } catch ( NoSuchProviderException e ) { throw new VException ( "Couldn't find Android KeyStore" ) ; } catch ( NoSuchAlgorithmException e ) { throw new VException ( "Keystore doesn't support " + PK_ALGORITHM + " algorithm: " + e . getMessage ( ) ) ; } catch ( InvalidAlgorithmParameterException e ) { throw new VException ( "Invalid keystore algorithm parameters: " + e . getMessage ( ) ) ; } }
Generates a new private key and stores it in the Android KeyStore under the provided alias.
Generates a new private key and stores it in the Android KeyStore under the provided alias .
// NOTE(spetrovic): KeyPairGenerator needs to be initialized with "RSA" algorithm and // not "EC" algorithm, even though we generate "EC" keys below.  Otherwise, Android // KeyStore claims that "EC" is an unrecognized algorithm!