我正在使用Android M指纹API,允许用户登录应用程序。为此,我需要在设备上存储用户名和密码。目前,我的登录和指纹API均可正常工作,但是用户名和密码都是以明文形式存储的。我希望在存储之前对密码进行加密,并在用户使用指纹进行身份验证后能够检索到它。
我非常难以使其工作。我一直在尝试从 Android安全示例中使用所学的知识,但每个示例似乎只处理加密或签名,而从来没有解密。
我目前的进展是需要获取一个
坦白说,我不确定这些是否正确,这是我从主题相关的任何地方找到的零散信息。每次更改都会引发不同的异常,而此特定构建无法运行,因为我无法实例化Cipher,它会抛出NoSuchAlgorithmException:未找到EC的提供程序。我也尝试切换到RSA,但我得到类似的错误。
所以我的问题基本上是这样的; 我如何在Android上加密明文,并在用户通过指纹API进行身份验证后使其可用于解密?
我也根据
现在我可以加密密码并保存加密后的密码。但是当我尝试获取加密后的密码并尝试解密时,我会收到一个“KeyStoreException”未知错误...
我非常难以使其工作。我一直在尝试从 Android安全示例中使用所学的知识,但每个示例似乎只处理加密或签名,而从来没有解密。
我目前的进展是需要获取一个
AndroidKeyStore
实例、一个KeyPairGenerator
和一个Cipher
,使用非对称加密来允许使用 Android KeyGenParameterSpec.Builder().setUserAuthenticationRequired(true)
。之所以选择非对称加密是因为setUserAuthenticationRequired
方法将阻止未经用户身份验证的任何密钥使用,但是:这应该允许我在用户使用指纹进行身份验证之前,使用公钥加密密码,然后只有在用户通过身份验证后才能使用私钥解密。此授权仅适用于秘密密钥和私钥操作。公钥操作不受限制。
public KeyStore getKeyStore() {
try {
return KeyStore.getInstance("AndroidKeyStore");
} catch (KeyStoreException exception) {
throw new RuntimeException("Failed to get an instance of KeyStore", exception);
}
}
public KeyPairGenerator getKeyPairGenerator() {
try {
return KeyPairGenerator.getInstance("EC", "AndroidKeyStore");
} catch(NoSuchAlgorithmException | NoSuchProviderException exception) {
throw new RuntimeException("Failed to get an instance of KeyPairGenerator", exception);
}
}
public Cipher getCipher() {
try {
return Cipher.getInstance("EC");
} catch(NoSuchAlgorithmException | NoSuchPaddingException exception) {
throw new RuntimeException("Failed to get an instance of Cipher", exception);
}
}
private void createKey() {
try {
mKeyPairGenerator.initialize(
new KeyGenParameterSpec.Builder(KEY_ALIAS,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setAlgorithmParameterSpec(new ECGenParameterSpec("secp256r1")
.setUserAuthenticationRequired(true)
.build());
mKeyPairGenerator.generateKeyPair();
} catch(InvalidAlgorithmParameterException exception) {
throw new RuntimeException(exception);
}
}
private boolean initCipher(int opmode) {
try {
mKeyStore.load(null);
if(opmode == Cipher.ENCRYPT_MODE) {
PublicKey key = mKeyStore.getCertificate(KEY_ALIAS).getPublicKey();
mCipher.init(opmode, key);
} else {
PrivateKey key = (PrivateKey) mKeyStore.getKey(KEY_ALIAS, null);
mCipher.init(opmode, key);
}
return true;
} catch (KeyPermanentlyInvalidatedException exception) {
return false;
} catch(KeyStoreException | CertificateException | UnrecoverableKeyException
| IOException | NoSuchAlgorithmException | InvalidKeyException
| InvalidAlgorithmParameterException exception) {
throw new RuntimeException("Failed to initialize Cipher", exception);
}
}
private void encrypt(String password) {
try {
initCipher(Cipher.ENCRYPT_MODE);
byte[] bytes = mCipher.doFinal(password.getBytes());
String encryptedPassword = Base64.encodeToString(bytes, Base64.NO_WRAP);
mPreferences.getString("password").set(encryptedPassword);
} catch(IllegalBlockSizeException | BadPaddingException exception) {
throw new RuntimeException("Failed to encrypt password", exception);
}
}
private String decryptPassword(Cipher cipher) {
try {
String encryptedPassword = mPreferences.getString("password").get();
byte[] bytes = Base64.decode(encryptedPassword, Base64.NO_WRAP);
return new String(cipher.doFinal(bytes));
} catch (IllegalBlockSizeException | BadPaddingException exception) {
throw new RuntimeException("Failed to decrypt password", exception);
}
}
坦白说,我不确定这些是否正确,这是我从主题相关的任何地方找到的零散信息。每次更改都会引发不同的异常,而此特定构建无法运行,因为我无法实例化Cipher,它会抛出NoSuchAlgorithmException:未找到EC的提供程序。我也尝试切换到RSA,但我得到类似的错误。
所以我的问题基本上是这样的; 我如何在Android上加密明文,并在用户通过指纹API进行身份验证后使其可用于解密?
我取得了一些进展,主要是由于在KeyGenParameterSpec
文档页面上发现的信息。
我保留了getKeyStore
、encryptePassword
、decryptPassword
、getKeyPairGenerator
和getCipher
大部分相同,但我改变了KeyPairGenerator.getInstance
和Cipher.getInstance
分别为"RSA"
和"RSA/ECB/OAEPWithSHA-256AndMGF1Padding"
。
我还将代码的其余部分改为RSA,而不是椭圆曲线,因为据我所知,Java 1.7(因此也包括Android)不支持使用EC进行加密和解密。我根据文档页面上的“RSA OAEP加密/解密的RSA密钥对”示例更改了我的createKeyPair
方法:
private void createKeyPair() {
try {
mKeyPairGenerator.initialize(
new KeyGenParameterSpec.Builder(KEY_ALIAS, KeyProperties.PURPOSE_DECRYPT)
.setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_OAEP)
.setUserAuthenticationRequired(true)
.build());
mKeyPairGenerator.generateKeyPair();
} catch(InvalidAlgorithmParameterException exception) {
throw new RuntimeException(exception);
}
}
我也根据
KeyGenParameterSpec
文档中的已知问题修改了我的initCipher
方法:
在Android 6.0(API级别23)中已知的一个错误会导致即使对于公钥也强制执行与用户身份验证相关的授权。为了解决这个问题,请提取公钥材料以在Android Keystore之外使用。
private boolean initCipher(int opmode) {
try {
mKeyStore.load(null);
if(opmode == Cipher.ENCRYPT_MODE) {
PublicKey key = mKeyStore.getCertificate(KEY_ALIAS).getPublicKey();
PublicKey unrestricted = KeyFactory.getInstance(key.getAlgorithm())
.generatePublic(new X509EncodedKeySpec(key.getEncoded()));
mCipher.init(opmode, unrestricted);
} else {
PrivateKey key = (PrivateKey) mKeyStore.getKey(KEY_ALIAS, null);
mCipher.init(opmode, key);
}
return true;
} catch (KeyPermanentlyInvalidatedException exception) {
return false;
} catch(KeyStoreException | CertificateException | UnrecoverableKeyException
| IOException | NoSuchAlgorithmException | InvalidKeyException
| InvalidAlgorithmParameterException exception) {
throw new RuntimeException("Failed to initialize Cipher", exception);
}
}
现在我可以加密密码并保存加密后的密码。但是当我尝试获取加密后的密码并尝试解密时,我会收到一个“KeyStoreException”未知错误...
03-15 10:06:58.074 14702-14702/com.example.app E/LoginFragment: Failed to decrypt password
javax.crypto.IllegalBlockSizeException
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:486)
at javax.crypto.Cipher.doFinal(Cipher.java:1502)
at com.example.app.ui.fragment.util.LoginFragment.onAuthenticationSucceeded(LoginFragment.java:251)
at com.example.app.ui.controller.FingerprintCallback.onAuthenticationSucceeded(FingerprintCallback.java:21)
at android.support.v4.hardware.fingerprint.FingerprintManagerCompat$Api23FingerprintManagerCompatImpl$1.onAuthenticationSucceeded(FingerprintManagerCompat.java:301)
at android.support.v4.hardware.fingerprint.FingerprintManagerCompatApi23$1.onAuthenticationSucceeded(FingerprintManagerCompatApi23.java:96)
at android.hardware.fingerprint.FingerprintManager$MyHandler.sendAuthenticatedSucceeded(FingerprintManager.java:805)
at android.hardware.fingerprint.FingerprintManager$MyHandler.handleMessage(FingerprintManager.java:757)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.security.KeyStoreException: Unknown error
at android.security.KeyStore.getKeyStoreException(KeyStore.java:632)
at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:224)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:473)
at javax.crypto.Cipher.doFinal(Cipher.java:1502)
at com.example.app.ui.fragment.util.LoginFragment.onAuthenticationSucceeded(LoginFragment.java:251)
at com.example.app.ui.controller.FingerprintCallback.onAuthenticationSucceeded(FingerprintCallback.java:21)
at android.support.v4.hardware.fingerprint.FingerprintManagerCompat$Api23FingerprintManagerCompatImpl$1.onAuthenticationSucceeded(FingerprintManagerCompat.java:301)
at android.support.v4.hardware.fingerprint.FingerprintManagerCompatApi23$1.onAuthenticationSucceeded(FingerprintManagerCompatApi23.java:96)
at android.hardware.fingerprint.FingerprintManager$MyHandler.sendAuthenticatedSucceeded(FingerprintManager.java:805)
at android.hardware.fingerprint.FingerprintManager$MyHandler.handleMessage(FingerprintManager.java:757)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)