Example usage for android.security.keystore KeyProperties KEY_ALGORITHM_RSA

List of usage examples for android.security.keystore KeyProperties KEY_ALGORITHM_RSA

Introduction

In this page you can find the example usage for android.security.keystore KeyProperties KEY_ALGORITHM_RSA.

Prototype

String KEY_ALGORITHM_RSA

To view the source code for android.security.keystore KeyProperties KEY_ALGORITHM_RSA.

Click Source Link

Document

Rivest Shamir Adleman (RSA) key.

Usage

From source file:Main.java

@TargetApi(Build.VERSION_CODES.M)
private static void generateNewKey() {
    try {/*  w  w w  .  j  a v  a2  s .c  o m*/
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA,
                KEY_PROVIDER);

        keyPairGenerator.initialize(new KeyGenParameterSpec.Builder(KEY_ALIAS, KeyProperties.PURPOSE_DECRYPT)
                .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP).build());
        keyPairGenerator.generateKeyPair();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static void generateNewKeyOld(Context context) {
    try {/*from w w  w.ja va 2  s . co  m*/
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA,
                KEY_PROVIDER);

        Calendar instance = Calendar.getInstance();
        Date start = instance.getTime();

        instance.add(Calendar.YEAR, 1);
        Date end = instance.getTime();

        keyPairGenerator.initialize(new KeyPairGeneratorSpec.Builder(context).setAlias(KEY_ALIAS)
                .setSubject(new X500Principal("CN=" + KEY_ALIAS)).setSerialNumber(BigInteger.valueOf(20151021))
                .setStartDate(start).setEndDate(end).build());

        keyPairGenerator.generateKeyPair();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
}