Android.KeyStore getEntry() returning null
Asked Answered
S

0

7

I am using keystore to generate key for encryption. It was working fine but somehow it stopped to generate key. Please find below my code:

private void createKey() throws NoSuchProviderException,
        NoSuchAlgorithmException, InvalidAlgorithmParameterException {

    // Create a start and end time, for the validity range of the key pair that's about to be
    // generated.
    Calendar start = new GregorianCalendar();
    Calendar end = new GregorianCalendar();
    end.add(Calendar.YEAR, 1);

    Context context = SNContext.getInstance().getContext();

    // The KeyPairGeneratorSpec object is how parameters for your key pair are passed
    // to the KeyPairGenerator.  For a fun home game, count how many classes in this sample
    // start with the phrase "KeyPair".
    KeyPairGeneratorSpec spec =
            new KeyPairGeneratorSpec.Builder(context)
                    // You'll use the alias later to retrieve the key.  It's a key for the key!
                    .setAlias(FNConstants.ALIAS)
                            // The subject used for the self-signed certificate of the generated pair
                    .setSubject(new X500Principal(String.format("CN=%s, OU=%s", FNConstants.ALIAS, context.getPackageName())))
                            // The serial number used for the self-signed certificate of the
                            // generated pair.
                    .setSerialNumber(BigInteger.valueOf(1337))
                            // Date range of validity for the generated pair.
                    .setStartDate(start.getTime())
                    .setEndDate(end.getTime())
                    .build();


    // Initialize a KeyPair generator using the the intended algorithm (in this example, RSA
    // and the KeyStore.  This example uses the AndroidKeyStore.
    KeyPairGenerator kpGenerator = KeyPairGenerator
            .getInstance(FNConstants.TYPE_RSA,
                    FNConstants.KEYSTORE_PROVIDER_ANDROID);
    kpGenerator.initialize(spec);

    KeyPair kp = kpGenerator.generateKeyPair();
}

Now when I trying to retrieve this key in my encrypt method, I am getting value of entry as null. Am I doing something wrong in ks.load(null). I had tested it earlier and it was working fine.

public static String encryptData(String inputStr) throws KeyStoreException,
        UnrecoverableEntryException, NoSuchAlgorithmException, InvalidKeyException,
        SignatureException, IOException, CertificateException, NoSuchProviderException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {

    String result = "";
    KeyStore ks = KeyStore.getInstance(FNConstants.KEYSTORE_PROVIDER_ANDROID);
    // Weird artifact of Java API.  If you don't have an InputStream to load, you still need
    // to call "load", or it'll crash.
    ks.load(null);

    // Load the key pair from the Android Key Store
    KeyStore.Entry entry = ks.getEntry(FNConstants.ALIAS, null);

    if (entry == null) {
        return null;
    }

    PublicKey publicKey = ks.getCertificate(FNConstants.ALIAS).getPublicKey();
    byte[] encodedBytes = null;
    Cipher cipher = Cipher.getInstance(FNConstants.TRANSFORMATION, FNConstants.PROVIDER);
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    encodedBytes = cipher.doFinal(inputStr.getBytes());

    result = Base64.encodeToString(encodedBytes, Base64.DEFAULT);
    return result;
}
Schumann answered 14/7, 2015 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.