gpt4 book ai didi

安卓.security.KeyStoreException : Unknown error On a rare number of devices

转载 作者:太空狗 更新时间:2023-10-29 14:39:52 33 4
gpt4 key购买 nike

我遇到 android.security.KeyStoreException: Unknown error 在极少数具有不同 Android 版本 (6 - 8) 的设备上

这是我的 key 生成代码:

final KeyPairGenerator keyGenerator = KeyPairGenerator
.getInstance(KeyProperties.KEY_ALGORITHM_RSA,

ANDROID_KEY_STORE);

keyGenerator.initialize(new KeyGenParameterSpec.Builder(ALIAS,
KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT)
.setKeySize(2048)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1)
.build());

return keyGenerator.generateKeyPair();

这是我加载 key 对的方式:

if (keyStore.containsAlias(ALIAS))
{
KeyStore.Entry entry = keyStore.getEntry(ALIAS, null);
if (entry != null)
{
if (entry instanceof KeyStore.PrivateKeyEntry)
{
Log.i(TAG, "KeyPair found.");
KeyStore.PrivateKeyEntry pke = (KeyStore.PrivateKeyEntry) entry;
Certificate cert = pke.getCertificate();

if (cert != null)
{
return new KeyPair(cert.getPublicKey(), pke.getPrivateKey());
}

Log.w(TAG, "Cert / Public Key is null");
}
}
}

这是我的解密代码:

Cipher RSACipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");          

RSACipher.init(Cipher.DECRYPT_MODE, privateKey);
return new String(RSACipher.doFinal(base64.decode(textToDecrypt)), "UTF-8");

这是一个失败的解密过程的示例 stracktrace:

Caused by javax.crypto.IllegalBlockSizeException
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:519)
at javax.crypto.Cipher.doFinal(Cipher.java:1736)
at com.examplecompany.security.EncryptionController.decryptAsymmetric(EncryptionController.java:297)
at com.example.crypto.android2.services.CryptoClass.decryptMessage(CryptoClass.java:684)
at com.example.crypto.android2.services.CryptoClass.handleDecryption(CryptoClass.java:619)
at com.example.crypto.android2.services.CryptoClass.doInBackgroundInternal(CryptoClass.java:450)
at com.example.crypto.android2.services.CryptoClass.doInBackground(CryptoClass.java:165)
at com.example.crypto.android2.services.CryptoClass.doInBackground(CryptoClass.java:84)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)

Caused by android.security.KeyStoreException: Unknown error
at android.security.KeyStore.getKeyStoreException(KeyStore.java:1137)
at android.security.keystore.KeyStoreCryptoOperationChunkedStreamer.doFinal(KeyStoreCryptoOperationChunkedStreamer.java:224)
at android.security.keystore.AndroidKeyStoreCipherSpiBase.engineDoFinal(AndroidKeyStoreCipherSpiBase.java:506)
at javax.crypto.Cipher.doFinal(Cipher.java:1736)
at com.examplecompany.security.EncryptionController.decryptAsymmetric(EncryptionController.java:297)
at com.example.crypto.android2.services.CryptoClass.decryptMessage(CryptoClass.java:684)
at com.example.crypto.android2.services.CryptoClass.handleDecryption(CryptoClass.java:619)
at com.example.crypto.android2.services.CryptoClass.doInBackgroundInternal(CryptoClass.java:450)
at com.example.crypto.android2.services.CryptoClass.doInBackground(CryptoClass.java:165)
at com.example.crypto.android2.services.CryptoClass.doInBackground(CryptoClass.java:84)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)

它在数千台设备上 99.999% 的所有要加密的消息中运行良好,但有时会失败。你能帮帮我吗?

最佳答案

刚刚在 other question here in SO 上找到了类似问题的解决方案

I found my answer on the Android Issue Tracker, from what I understand, the unrestricted PublicKey, created to work around another known issue, becomes incompatible with the current Cipher. The work around for this is to specify an OAEPParameterSpec when the Cipher is initialized:

您需要以下内容作为您的 Cipher 初始化代码的第三个参数

OAEPParameterSpec spec = new OAEPParameterSpec(
"SHA-256", "MGF1", MGF1ParameterSpec.SHA1, PSource.PSpecified.DEFAULT);


RSACipher.init(Cipher.DECRYPT_MODE, privateKey, spec); // I added the same to the init in Cipher.ENCRYPT_MODE too

关于安卓.security.KeyStoreException : Unknown error On a rare number of devices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49982508/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com