首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InvalidKeyException:在Android设备上对RSA decrypt执行密钥存储操作失败

InvalidKeyException:在Android设备上对RSA decrypt执行密钥存储操作失败
EN

Stack Overflow用户
提问于 2017-03-03 11:18:02
回答 1查看 9.7K关注 0票数 7

我正在尝试对AndroidKeyStore中的密钥执行RSA加密和解密。加密成功完成,但当我尝试解密时,它在Cipher.init()上抛出一个InvalidKeyException: Keystore操作失败。

以下是我的密钥生成代码:

代码语言:javascript
复制
KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
AlgorithmParameterSpec spec = null;
spec = new KeyGenParameterSpec.Builder(mAlias,
        KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
        .setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
        .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
        .build();
kpGenerator.initialize(spec);
KeyPair kp = kpGenerator.generateKeyPair();

这是我的加密代码:

代码语言:javascript
复制
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(mAlias, null);
Cipher cip = null;
RSAPublicKey pubKey = (RSAPublicKey) entry.getCertificate().getPublicKey();
cip = Cipher.getInstance("RSA/ECB/NoPadding");
cip.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] encryptBytes = cip.doFinal(challenge.getBytes());
String encryptedStr64 = Base64.encodeToString(encryptBytes, Base64.DEFAULT);

这是我的解密代码:

代码语言:javascript
复制
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(mAlias, null);
Cipher cip = null;
cip = Cipher.getInstance("RSA/ECB/NoPadding");
cip.init(Cipher.DECRYPT_MODE, entry.getPrivateKey());
byte[] decryptedBytes = cip.doFinal(Base64.decode(encrypted64, Base64.DEFAULT));
String plainText = new String(decryptedBytes);

我知道密钥生成代码中的填充与我的加密/解密代码不同。但是,当我将密钥生成代码的填充改为KeyProperties.ENCRYPTION_PADDING_NONE时,我得到的是kpGenerator.initialize上的InvalidAlgorithmParameterException (Spec)。在decrypt中使用"RSA/ECB/PKCS1Padding“,它可以工作。无论加密中的填充是什么,它总是有效的。

是的,我也知道使用无填充的安全隐患,但对于我的应用程序,我需要一个确定性的密文。

下面是InvalidKeyException的堆栈跟踪:

代码语言:javascript
复制
03-06 09:10:32.710  5058  5058 W System.err: java.security.InvalidKeyException: Keystore operation failed
03-06 09:10:32.713  5058  5058 W System.err:    at android.security.KeyStore.getInvalidKeyException(KeyStore.java:692)
03-06 09:10:32.713  5058  5058 W System.err:    at android.security.KeyStore.getInvalidKeyException(KeyStore.java:712)
03-06 09:10:32.713  5058  5058 W System.err:    at android.security.keystore.KeyStoreCryptoOperationUtils.getInvalidKeyExceptionForInit(KeyStoreCryptoOperationUtils.java:54)
03-06 09:10:32.713  5058  5058 W System.err:    at android.security.keystore.KeyStoreCryptoOperationUtils.getExceptionForCipherInit(KeyStoreCryptoOperationUtils.java:89)
03-06 09:10:32.713  5058  5058 W System.err:    at android.security.keystore.AndroidKeyStoreCipherSpiBase.ensureKeystoreOperationInitialized(AndroidKeyStoreCipherSpiBase.java:263)
03-06 09:10:32.713  5058  5058 W System.err:    at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineInit(AndroidKeyStoreCipherSpiBase.java:108)
03-06 09:10:32.713  5058  5058 W System.err:    at javax.crypto.Cipher.tryTransformWithProvider(Cipher.java:612)
03-06 09:10:32.713  5058  5058 W System.err:    at javax.crypto.Cipher.tryCombinations(Cipher.java:532)
03-06 09:10:32.714  5058  5058 W System.err:    at javax.crypto.Cipher.getSpi(Cipher.java:437)
03-06 09:10:32.714  5058  5058 W System.err:    at javax.crypto.Cipher.init(Cipher.java:815)
03-06 09:10:32.714  5058  5058 W System.err:    at javax.crypto.Cipher.init(Cipher.java:774)
03-06 09:10:32.714  5058  5058 W System.err:    at dfpidentifiers.my.test.app.MainActivity.decrypt(MainActivity.java:950)
03-06 09:10:32.714  5058  5058 W System.err:    at dfpidentifiers.my.test.app.MainActivity.onCreate(MainActivity.java:117)
03-06 09:10:32.714  5058  5058 W System.err:    at android.app.Activity.performCreate(Activity.java:6251)
03-06 09:10:32.714  5058  5058 W System.err:    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
03-06 09:10:32.714  5058  5058 W System.err:    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
03-06 09:10:32.714  5058  5058 W System.err:    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
03-06 09:10:32.714  5058  5058 W System.err:    at android.app.ActivityThread.-wrap11(ActivityThread.java)
03-06 09:10:32.714  5058  5058 W System.err:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
03-06 09:10:32.714  5058  5058 W System.err:    at android.os.Handler.dispatchMessage(Handler.java:102)
03-06 09:10:32.714  5058  5058 W System.err:    at android.os.Looper.loop(Looper.java:148)
03-06 09:10:32.714  5058  5058 W System.err:    at android.app.ActivityThread.main(ActivityThread.java:5417)
03-06 09:10:32.714  5058  5058 W System.err:    at java.lang.reflect.Method.invoke(Native Method)
03-06 09:10:32.714  5058  5058 W System.err:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-06 09:10:32.714  5058  5058 W System.err:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-06 09:10:32.714  5058  5058 W System.err: Caused by: android.security.KeyStoreException: Incompatible padding mode
03-06 09:10:32.714  5058  5058 W System.err:    at android.security.KeyStore.getKeyStoreException(KeyStore.java:632)
03-06 09:10:32.714  5058  5058 W System.err:    ... 24 more

从堆栈跟踪来看,它似乎是一个不兼容的填充模式,但是我如何生成一个不支持填充的密钥对呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-06 09:54:20

InvalidKeyException实际上是由密钥生成和解密过程中填充的差异造成的。我不确定为什么加密没有抛出同样的异常。

我最初也不能在密钥生成期间使用NoPadding,因为需要IND-CPA。我必须设置setRandomizedEncryptionRequired(true)来覆盖它。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42570020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档