当前位置: 首页 > 知识库问答 >
问题:

Android密码。DoFinal在重新打开应用程序后尝试解密时出现了BadPaddingException

戚明朗
2023-03-14

问题可能会很长,但我会试着详细描述它。

这是一个演示有类似我的问题。

存储时:

  1. 用户输入密码
  2. 通过AndroidKeyStore生成的SecretKey创建encrpty模式密码
public Cipher getEncryptCipher() {
    try {
        Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
                + KeyProperties.BLOCK_MODE_CBC + "/"
                + KeyProperties.ENCRYPTION_PADDING_PKCS7);
        mKeyStore.load(null);
        SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME, null);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher;
    } catch (Exception e) {
        throw new RuntimeException("Failed to encrypt pin ", e);
    }
}
//mCryptoObject is generated by cipher in step 2
mFingerprintManager.authenticate(mCryptoObject, mCancellationSignal, 0 /* flags */, FingerprintManager.AuthenticationCallback, null);


//in FingerprintManager.AuthenticationCallback
javax.crypto.Cipher cipher = result.getCryptoObject().getCipher();
//In my app, we have many device, every could have one password. Pin is password.
public void encryptPin(String deviceId, String pin, javax.crypto.Cipher cipher) {
    try {
        if (cipher == null) return;
        byte[] encryptedBytes = cipher.doFinal(pin.getBytes("utf-8"));
        byte[] cipherIv = cipher.getIV();
        String encodedPin = Base64.encodeToString(encryptedBytes, Base64.DEFAULT);
        String encodeCipherIv = Base64.encodeToString(cipherIv, Base64.DEFAULT);
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putString(createPinKey(deviceId), encodedPin);
        editor.putString(createIvKey(deviceId), encodeCipherIv);
        editor.apply();
    } catch (IOException | IllegalBlockSizeException | BadPaddingException e) {
        throw new RuntimeException("Failed to encrypt pin ", e);
    }
}

阅读时:

    null
public Cipher getDecryptCipher(String deviceId) {
    try {
        String encodedIv = mSharedPreferences.getString(createIvKey(deviceId), "");
        byte[] cipherIv = Base64.decode(encodedIv, Base64.DEFAULT);
        Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
                + KeyProperties.BLOCK_MODE_CBC + "/"
                + KeyProperties.ENCRYPTION_PADDING_PKCS7);
        mKeyStore.load(null);
        SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME, null);
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(cipherIv));
        return cipher;
    } catch (Exception e) {
        throw new RuntimeException("Failed to encrypt pin ", e);
    }
}
//mCryptoObject is generated by cipher in step 1
mFingerprintManager.authenticate(mCryptoObject, mCancellationSignal, 0 /* flags */, FingerprintManager.AuthenticationCallback, null);


//in FingerprintManager.AuthenticationCallback
javax.crypto.Cipher cipher = result.getCryptoObject().getCipher();
public String decryptPin(String deviceId, javax.crypto.Cipher cipher) {
    String encryptedPin = mSharedPreferences.getString(createPinKey(deviceId), "");
    if (TextUtils.isEmpty(encryptedPin)) {
        return "";
    }
    try {
        if (cipher == null) return "";
        byte[] decodedBytes = Base64.decode(encryptedPin, Base64.DEFAULT);
        //BadPaddingException in this line 
        byte[] decryptBytes = cipher.doFinal(decodedBytes);
        return new String(decryptBytes, Charset.forName("UTF8"));
    } catch (Exception e) {
        MyLog.d(TAG, "Failed to decrypt the data with the generated key." + e.getMessage());
        e.printStackTrace();
        return "";
    }
}
private void init() {
    try {
        mKeyStore = KeyStore.getInstance("AndroidKeyStore");
        mKeyStore.load(null);
        KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            builder.setInvalidatedByBiometricEnrollment(true);
        }


        KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
        keyGenerator.init(
                    builder.build());
        keyGenerator.generateKey();


    } catch (Exception e) {
        throw new RuntimeException("Fail to init:" + e);
    }
}
System.err: javax.crypto.BadPaddingException
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:482)
at javax.crypto.Cipher.doFinal(Cipher.java:1502)
at com.xiaomi.smarthome.framework.page.verify.DeviceVerifyConfigCache.decryptPin(DeviceVerifyConfigCache.java:156)
at com.xiaomi.smarthome.framework.page.verify.VerifyManager.decryptPin(VerifyManager.java:173)
at com.xiaomi.smarthome.framework.page.verify.FingerPrintVerifyActivity$1.onAuthenticated(FingerPrintVerifyActivity.java:62)
at com.xiaomi.smarthome.framework.page.verify.view.FingerPrintOpenVerifyDialog.onAuthenticationSucceeded(FingerPrintOpenVerifyDialog.java:136)
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:5458)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: android.security.KeyStoreException: Invalid argument
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)
... 13 more

有人能帮我解决这个问题吗?是由SecretKey引起的吗?谢谢!!!

共有1个答案

乐刚毅
2023-03-14

我找到我的问题了。在init()方法中用相同的别名生成键是错误的。我通过增加一个判断条件来解决这个问题。

        if (!mKeyStore.containsAlias(KEY_NAME)) {
            KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
            keyGenerator.init(
                    builder.build());
            keyGenerator.generateKey();
        }
 类似资料:
  • 我有一个文本文件,我已经加密使用移位,但我需要加密加密的文本再次,但这一次使用vigenere密码。然后我需要解密加密的文本(首先是vigenere,然后是Shift),但所有的大小写字母以及黑色空格、引号、逗号和句号都需要保持不变。我已经完成了移位、加密和解密,剩下的就是Vigenere了。下面显示的是我加密Vigenere的类,我还没有写解密,因为我被卡在加密步骤。谢谢你。

  • 我想在我的项目上使用RSA/AES文件加密。我用rsa做了一对私钥/公钥和一个AES密钥,然后用公钥加密AES密钥。最后用私有RSA密钥对AES加密密钥进行解密。

  • 在我的应用程序中,有一个使用相机捕捉图像的工具。在Android6之前的所有版本中,它都能完美地工作,但在Android7.0版本中,它让我的应用程序崩溃了。 添加的权限 权限侦听器 打开相机的代码 崩溃日志 由:android.content.activityNotFoundException引起:未找到处理意图的活动{act=android.media.action.image_capture

  • 我尝试在1个应用程序中构建扫描仪和生成器。当我按下发电机按钮时,它突然崩溃了。我的日志中没有任何错误或警告。 这是我的生成器代码: 主活动代码: 有人知道怎么解决这个问题吗?请帮帮我。 更新 以下是我的生成器xml代码: 我希望这将有助于我的问题:( 更新:这是由我愚蠢的打字错误修正;)。非常感谢迄今为止所有回答我问题的人。我说不出我有多感激。特别是对于那个我已经投票支持正确答案的家伙。你真是个英

  • 我在尝试调用此方法时遇到以下异常。 MessageQueue回调中出现异常:HandlerReceiveCallback 08-21 00:12:43.454 10843-10843/common。以货换货通用域名格式。barterapp E/MessageQueue JNI﹕ Android看法膨胀异常:二进制XML文件行#2:在android上inflating类时出错。看法更平坦。com上的

  • 一旦我运行了它,我就得到了python shell上的以下代码。假设消息变量是“My name is Daniel”,我使用的shiftValue为2。这是打印出来的: