私のアプリでは、保存する前に暗号化する必要があり、取得するときに復号化する必要がある共有設定にいくつかのデータを保存しています。
AES-256 暗号化を使用しています。そのために、パスフレーズ/ピンを使用して秘密鍵を生成しています。以下は私のコードスニペットです。
public static SecretKey generateKey(char[] passphraseOrPin, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {
// Number of PBKDF2 hardening rounds to use. Larger values increase
// computation time. You should select a value that causes computation
// to take >100ms.
final int iterations = 1000;
// Generate a 256-bit key
final int outputKeyLength = 256;
SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpec = new PBEKeySpec(passphraseOrPin, salt, iterations, outputKeyLength);
SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
return secretKey;
}
私のアプリに従って、ユーザーに一意のピンを提供するように依頼できます。しかし、アプリは 4.0 からサポートする必要があるため、ピンをキーストアに保存できません。どうすればピンを保存できますか?