草庐IT

安卓.security.KeyStoreException : Invalid key blob

coder 2023-12-04 原文

我无法从 Android 上的 KeyStore 获取(私有(private)) key 。问题主要出现 在三星设备(S6、S6 Edge)和 Android 6 上。

android.security.KeyStoreException:无效的 key blob

调用以下行时抛出(其中别名是存储 key 的名称)。

KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry)keyStore.getEntry(alias, null);

KeyStore本身是通过

获取的
KeyStore.getInstance("AndroidKeyStore");

key 通过以下方法生成:

private static void createKey(String alias, String subject, KeyStore keyStore, BigInteger serialNumber, Date startDate, Date endDate, String algorithm, String keyStoreProvider, Context context)
            throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException {
    if (keyStore.containsAlias(alias)) {
        // Key already exists.
        return;
    }

    // Generate keys.
    KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(context)
            .setAlias(alias)
            .setSubject(new X500Principal(subject))
            .setSerialNumber(serialNumber)
            .setStartDate(startDate)
            .setEndDate(endDate)
            .build();

    KeyPairGenerator generator = KeyPairGenerator.getInstance(algorithm, keyStoreProvider);
    generator.initialize(spec);

    KeyPair keyPair = generator.generateKeyPair();
}

其中算法是“RSA”,keyStoreProvider 是“AndroidKeyStore”。

堆栈跟踪部分:

android.security.KeyStoreException: Invalid key blob
       at android.security.KeyStore.getKeyStoreException(KeyStore.java:939)
       at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:216)
       at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyPairFromKeystore(AndroidKeyStoreProvider.java:252)
       at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePrivateKeyFromKeystore(AndroidKeyStoreProvider.java:263)
       at android.security.keystore.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:93)
       at java.security.KeyStoreSpi.engineGetEntry(KeyStoreSpi.java:372)
       at java.security.KeyStore.getEntry(KeyStore.java:645)

异常导致 java.security.UnrecoverableKeyException: Failed to obtain information about private key 被抛出。

我找不到任何关于“无效 key blob”的更详细信息, 只是消息本身在此处定义:https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/security/keymaster/KeymasterDefs.java

最佳答案

当用户尝试从LOCK/UNINITIALIZEDUNLOCK 时会出现此问题。它默认定义为 30 secs 计时。 此问题是与 API 相关的实现问题。

此错误由 InvalidKeyException 生成。通过绕过此异常并再次调用该方法,您可以摆脱此错误。

您必须从 catch 参数中删除 InvalidKeyException 类。这仍然允许您检查 InvalidKeyException。检查后,您必须再次尝试使用代码,这样问题就不会出现在眼睛中,但进行 2 次检查可能会解决您的问题。代码如下。

try {
    KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) this.keyStore
            .getEntry("alias", null);

} catch (InvalidKeyException ex) {
    ex.printStackTrace();
    if (ex instanceof InvalidKeyException) { // bypass
                                                // InvalidKeyException
        // You can again call the method and make a counter for deadlock
        // situation or implement your own code according to your
        // situation
        if (retry) {
            keyStore.deleteEntry(keyName);
            return getCypher(keyName, false);
        } else {
            throw ex;
        }
    }
} catch (final Exception e) {
    e.printStackTrace();
    throw e;
}

You can see my another answer that describes one by one occurring issue and solution.

更新自 @Ankis :

当您通过将 InvalidKeyException 更改为 UnrecoverableKeyException 解决问题时。因此,我已根据您的建议进行了更新,以便全世界都可以知道实际答案。感谢分享:)。

try {
    KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) this.keyStore
            .getEntry("alias", null);

} catch (UnrecoverableKeyException ex) {
    ex.printStackTrace();
        // You can again call the method and make a counter for deadlock
        // situation or implement your own code according to your
        // situation
        if (retry) {
            keyStore.deleteEntry(keyName);
            return getCypher(keyName, false);
        }
} catch (final Exception e) {
    e.printStackTrace();
    throw e;
}

关于安卓.security.KeyStoreException : Invalid key blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36488219/

有关安卓.security.KeyStoreException : Invalid key blob的更多相关文章

  1. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  2. Spring Security 6.0系列【32】授权服务器篇之默认过滤器 - 2

    有道无术,术尚可求,有术无道,止于术。本系列SpringBoot版本3.0.4本系列SpringSecurity版本6.0.2本系列SpringAuthorizationServer版本1.0.2源码地址:https://gitee.com/pearl-organization/study-spring-security-demo文章目录前言1.OAuth2AuthorizationServerMetadataEndpointFilter2.OAuth2AuthorizationEndpointFilter3.OidcProviderConfigurationEndpointFilter4.N

  3. ruby-on-rails - 使用 secure_random stub rspec 中的随机值 - 2

    我正在尝试为我的gem编写规范,它生成otp并将其保存在数据库中。现在我正在为它编写规范。所以基本上我有三种方法generate_otp!、regenerate_otp!、verify_otp(otp)。generate_otp!的作用是调用包含三个变量的方法generate_otpotp_code-基本上是使用secure_random生成的随机值otp_verified-一个bool值,用于设置otp是否已验证的状态otp_expiry_time-设置otp的到期时间,可以由Rails应用在配置中设置。这三个也是我的数据库的列。在generate_otp之后,我正在调用active

  4. ruby - 使用 ruby​​ 应用程序时出现 remove_entry_secure 错误 - 2

    我正在尝试使用docsplit将PDF文件拆分为图像。但看来我的ruby​​安装有问题。我每次都会收到以下错误:/usr/lib/ruby/1.8/fileutils.rb:694:in`remove_entry_secure':parentdirectoryisworldwritable这是完整的命令行输出:$docsplitimagespdf-test.pdf/usr/lib/ruby/1.8/fileutils.rb:694:in`remove_entry_secure':parentdirectoryisworldwritable,FileUtils#remove_entry_

  5. ruby - 不能使用 has_secure_password,password_digest 错误 - 2

    晚上好。我有个问题。我正在使用has_secure_password并且导致我有一个错误undefinedmethodpassword_digest='for#`,但是我没有这个方法!!请帮忙,不知道该怎么办。我阅读了如何解决此问题,但对我没有帮助(这是我的用户模型。如果可以,请帮忙。classUser:createbefore_create{generate_token(:auth_token)}defsend_password_resetgenerate_token(:password_reset_token)self.password_reset_sent_at=Time.zon

  6. (一)专题介绍:移动端安卓手机改造成linux服务器&linux服务器中安装软件、部署前后端分离项目实战 - 2

    快捷目录前言一、涉及到的相关技术简介二、具体实现过程及踩坑杂谈1.安卓手机改造成linux系统实现方案2.改造后的手机Linux中软件的安装3.手机Linux中安装MySQL5.7踩坑实录4.手机Linux中安装软件的正确方法三、Linux服务器部署前后端分离项目流程1.前提准备(安装必要软件,搭建环境):2.前后端分离项目的详细部署过程:总结前言总体概述:本篇文章隶属于“手机改造服务器部署前后端分离项目”系列专栏,该专栏将分多个板块,每个板块独立成篇来详细记录:手机(安卓)改造成个人服务器(Linux)、Linux中安装软件、配置开发环境、部署JAVA+VUE+MySQL5.7前后端分离项目

  7. ruby-on-rails - salt 如何在 Rails 的 has_secure_password 中工作 - 2

    据我所知,为了使加密密码更安全,我会生成一个随机数(盐)并将其与散列密码一起存储在用户记录中(例如。)我会连接盐使用明文密码,然后对其进行加密(哈希)。生成的哈希将更难破解。将重复此过程以验证密码。查看has_secure_password和bcrypt_ruby(披露:我不是安全专家)我不知道这是如何完成的,因为用户记录中唯一存储的是散列密码。盐在哪里? 最佳答案 密码哈希和盐保存在数据库中名为password_digest的字符串列中。看这个question. 关于ruby-on-r

  8. Spring Security详细讲解(JWT+SpringSecurity登入案例) - 2

    本篇博文目录:一.SpringSecurity简介1.SpringSecurity2.SpringSecurity相关概念二.认证和授权1.认证(1)使用SpringSecurity进行简单的认证(SpringBoot项目中)(2)SpringSecurity的原理(3)SpringSecurity核心类(4)认证登入案例(JWT+SpringSecurity实现登入案例)2.授权(1)加入权限到Authentication中(2)SecurityConfig配置文件中开启注解权限配置(3)给接口中的方法添加访问权限(4)用户权限表的建立3.自定义失败处理(1)创建异常处理类(2)配置移除处理

  9. ruby-on-rails - 如何跳过 has_secure_password 验证 - 2

    在我的应用中,只有管理员可以创建新的用户记录。用户会通过电子邮件收到一个激活链接,他们可以在其中设置密码。我想使用has_secure_passord方法(railscast):classUser效果很好,但它会自动验证密码摘要的存在...所以当管理员创建记录时,验证失败。我有办法只跳过自动添加的password_digest验证而不跳过我添加的其他验证吗? 最佳答案 从4.X版本的rails开始,has_secure_password采用一个选项:validations。如果将其设置为false,它将不会运行验证。gem3.X版本

  10. ruby-on-rails - Rails 3 额外的 session 配置选项(key、expires_after、secure) - 2

    谁能指出新的Rails3.xsession配置选项是什么?我正在尝试复制我在Rails2.3.x应用程序中的相同配置。这是我在应用程序中使用的配置:#environment.rbconfig.action_controller.session_store=:active_record_storeconfig.action_controller.session={:key=>'_something',#non-securefordevelopment:secret=>'reallylongrandomstring'}#production.rb-overrideenvironment.r

随机推荐