Android.KeyStore getEntry()返回null

时间:2015-07-14 09:50:56

标签: android android-keystore

我正在使用密钥库生成加密密钥。它工作正常但不知何故停止生成密钥。请在下面找到我的代码:

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();
}

现在,当我尝试在我的加密方法中检索此密钥时,我将条目值视为null。我在ks.load(null)中做错了什么。我之前测试过,它工作正常。

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;
}

0 个答案:

没有答案