0

我正在构建一个需要在 android 设备上存储安全数据的 android 应用程序,该设备还支持 4.0 之前的旧版本 android。我知道 android 4.0 和之后的版本支持钥匙串,但我的应用程序需要支持旧版本。那么任何人都可以帮助并让我知道旧操作系统版本的Android中使用了哪种方法。

4

1 回答 1

2

在 ICS 之前的 Android 版本上,您可以使用KeyStore,以下是存储到的示例KeyStore

public boolean setEntry(String alias, String secretKey) {

    boolean keyStoreEntryWritten = false;

    if (mKeystore != null && secretKey != null) {
        // store something in the key store
        SecretKeySpec sks = new SecretKeySpec(secretKey.getBytes(), "MD5");
        KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(sks);
        KeyStore.ProtectionParameter pp = new KeyStore.PasswordProtection(null);

        try {
            mKeystore.setEntry(alias, ske, pp);

            // save key store
            boolean success = saveKeyStore();

            if (success) {
                keyStoreEntryWritten = true;
            }
        } catch (KeyStoreException ex) {
            Log.e(TAG, "Failed to read keystore" + mKeyStoreName);
        }
    }
    return keyStoreEntryWritten;
}


private boolean saveKeyStore() {

    FileOutputStream fos = null;
    boolean keyStoreSaved = true;

    // generate key store path
    String keyStoreFilePath = generateKeyStoreFilePath(mKeyStoreName, mKeystoreDirectoryPath);


    try {
        fos = new FileOutputStream(keyStoreFilePath);
        mKeystore.store(fos, mKeyStorePassword.toCharArray());
    } catch (Exception ex) {
        keyStoreSaved = false;
        Log.e(TAG, "Failed to save keystore " + mKeyStoreName);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ex) {
                keyStoreSaved = false;
                Log.e(TAG, "Failed to close FileOutputStream");
            }
        }
    }
    return keyStoreSaved;
}

您可以在这里找到更多信息:http: //developer.android.com/reference/java/security/KeyStore.html

编辑: 这是您检索密钥的方式:

public String getEntry(String alias) {

    String secretStr = null;
    byte[] secret = null;

    if (mKeystore != null) {



        try {
            if (!mKeystore.containsAlias(alias)) {
                Log.w(TAG, new StringBuilder().append("Keystore ").append(mKeyStoreName)
                        .append(" does not contain entry ").append(alias).toString());
                return null;
            }
        } catch (KeyStoreException ex) {
            Log.e(TAG, "Failed to read keystore entry " + alias);
        }

        // get my entry from the key store
        KeyStore.ProtectionParameter pp = new KeyStore.PasswordProtection(null);
        KeyStore.SecretKeyEntry ske = null;
        try {
            ske = (KeyStore.SecretKeyEntry) mKeystore.getEntry(alias, pp);
        } catch (Exception ex) {
            Log.e(TAG, "Failed to read keystore entry " + alias);
        }

        if (ske != null) {
            SecretKeySpec sks = (SecretKeySpec) ske.getSecretKey();
            secret = sks.getEncoded();

            if (secret != null) {
                secretStr = new String(secret);


            } else {
                Log.e(TAG, new StringBuilder().append("Read empty keystore entry ").append(alias).toString());
            }
        } else {
            Log.e(TAG, "Failed to read keystore entry " + alias);
        }
    }
    return secretStr;
}
于 2013-04-04T15:43:50.930 回答