AndroidKeyStore getEntry在某些点后一直失败

AndroidKeyStore getEntry在某些点后一直失败,第1张

概述我正在使用AndroidKeyStore生成一个RSA密钥对,用于加密/解密内部数据.执行此 *** 作的代码如下–它尝试检索现有的RSA密钥对(通过别名).如果不存在则尝试生成新的.代码如下–privatevoidinitializePublicPrivateKeys(){try{KeyStorekeyStore=KeyStore

我正在使用AndroidKeyStore生成一个RSA密钥对,用于加密/解密内部数据.

执行此 *** 作的代码如下 – 它尝试检索现有的RSA密钥对(通过别名).如果不存在则尝试生成新的.
代码如下 –

private voID initializePublicPrivateKeys(){    try    {        KeyStore keyStore = KeyStore.getInstance("AndroIDKeyStore");        keyStore.load(null);        KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(APP_RSA_KEY_PAIR_SECRET_AliAS, null);        _app_privateRSAKey = entry.getPrivateKey();        _app_publicRSAKey = entry.getCertificate().getPublicKey();    }    catch(Exception e){    }}private voID initializeRSAKeyPairs() {    initializePublicPrivateKeys();    boolean isKeyNotGenerated = _app_privateRSAKey == null || _app_publicRSAKey == null;    if(isKeyNotGenerated)    {        //Check here, if we already stored some data with prevIoUs RSA key pair - if a entry is present in SharedPreference then that would mean we had prevIoUsly generated a RSA key pair and the entry is in-turn encrypted by this key pair.        generateAppRSAPublicPrivateKeys();        initializePublicPrivateKeys();// initialize it again , since we have new keys generated.    }}@TargetAPI(18)private voID generateAppRSAPublicPrivateKeys(){    Calendar cal = Calendar.getInstance();    Date Now = cal.getTime();    // the certificate created would be valID for 25 years. This is just a random value.    cal.add(Calendar.YEAR, 25);    Date end = cal.getTime();    try{        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "AndroIDKeyStore");        Context appContext = getApplicationContext();        KeyPairGeneratorSpec.Builder keyPairGeneratorBuilder =                new KeyPairGeneratorSpec.Builder(appContext)                        .setAlias("myrsaalias")                        .setStartDate(Now)                        .setEndDate(end)                        .setSerialNumber(BigInteger.valueOf(1))                        .setSubject(new X500Principal(String.format("CN=%s, OU=%s", "myrsaalias",                                appContext.getApplicationInfo().packagename)));        if(Build.VERSION.SDK_INT >= 19){            keyPairGeneratorBuilder.setKeySize(2048);         }        generator.initialize(keyPairGeneratorBuilder.build());        generator.generateKeyPair();    }    catch(Exception e){        e.printstacktrace();        throw new IllegalArgumentException("Failed to generate RSA Public Private Key pair");    }}

这段代码工作正常.生成密钥对后,我使用它们来加密/解密数据(以共享偏好方式存储此数据)但是在某个时间点之后(某些应用程序重新启动之后),initializePublicPrivateKeys函数无法检索密钥对.(在此之后它一直失败)
因此,以加密形式存储在共享首选项中的数据会丢失,因为我根本没有相应的公钥来解密它. (如果我生成一个新的,那么,我想,这将是不同的,并将在我解密数据时返回不正确的结果)

我想知道这个initializePublicPrivateKeys函数在什么情况下会失败?

PS:目前,我无法捕获异常细节,因为问题发生在我无法访问的某些客户设备上,我需要自己找出问题的根源.
此外,设备密码或PIN在此期间不会更改(我已与我的客户确认)

提前致谢,

解决方法:

是否有可能用户将其屏幕锁定类型更改为不安全的方法(如“无”或“滑动”),从而触发AndroIDKeyStore中的所有键被删除?这是博客文章中讨论的一个已知问题:https://doridori.github.io/android-security-the-forgetful-keystore/

这里也提到:https://code.google.com/p/android/issues/detail?id=61989

如果您没有严格的安全要求,并且希望您的应用程序即使用户没有屏幕锁定也能正常工作,那么可能只需使用存储在数据区域的密钥库文件中的密钥对,并跳过AndroIDKeyStore.

总结

以上是内存溢出为你收集整理的AndroidKeyStore getEntry在某些点后一直失败全部内容,希望文章能够帮你解决AndroidKeyStore getEntry在某些点后一直失败所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1097231.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存