gpt4 book ai didi

java - KeyStore 加载导致 Android 上出现 EOFException 错误

转载 作者:行者123 更新时间:2023-11-29 09:25:43 26 4
gpt4 key购买 nike

我的应用程序有一些代码可以在 android 中创建 keystore ,创建一些默认 key 然后保存 keystore 。

稍后代码尝试重新加载 keystore ,在模拟器上这个运行良好,但是在手机上运行时出现 EOFException。任何关于我哪里出错的建议?

代码摘录如下:

在类中我有以下变量

static KeyStore keyStore; 
String filenameKeyStore = "KeyStore.kstore";

然后是下面两个函数

    public KeyHandler(Context context) { 
if(keyStore == null) {
Log.d("KeyStore", "Keystore is null so loading");
if(initialiseKeyStore(context) == false) {
// KeyStore failed to initialise
Log.e("ERROR", "Store failed to initialise");
}
}
}
private boolean initialiseKeyStore(Context context) {
FileInputStream input = null;
try {
// Get an instance of KeyStore
keyStore = KeyStore.getInstance("BKS");
// Load the KeyStore file
try {
// Try and open the private key store
input = context.openFileInput(filenameKeyStore);
} catch (FileNotFoundException e) {
// If the file doesn't exist then create the file, a ECDH key and
store the key
Log.w("Warning","File does not exist, creating new file");
try {
// Load the default Key Store
keyStore.load(null, null);
// Create the file
FileOutputStream output =
context.openFileOutput(filenameKeyStore, 0);
// Reset private key
resetPrivateKey(context);
// Save the key
keyStore.store(output, "password".toCharArray());
// Close the keystore and set the input stream
output.close();
input = context.openFileInput(filenameKeyStore);
// Reset the keyStore
keyStore = KeyStore.getInstance("BKS");
} catch (FileNotFoundException ee) {
Log.e("ERROR", "File not found, even though we just created it");
return false;
} catch (NoSuchProviderException e1) {
// BC isn't working exit
e1.printStackTrace();
System.exit(1);
} catch (InvalidAlgorithmParameterException e1) {
Log.e("ERROR", "The algorithm used for secure algorithm is
incorrect");
e1.printStackTrace();
return false;
}
}
// Load the store
keyStore.load(input, "password".toCharArray());
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (CertificateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (EOFException e) {
// Where the exception is caught
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (NoSuchProviderException e) {
// Serious error, return
e.printStackTrace();
System.exit(1);
}
return true;
}

最佳答案

这通常发生在 keystore 文件损坏时。我们刚刚(又一次)遇到了这样的问题,外部开发人员发送的 keystore 无法记忆起他究竟是如何创建 keystore 文件的。

创建一个新的 keystore 文件并导入证书 - 这将解决问题。

关于java - KeyStore 加载导致 Android 上出现 EOFException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2221351/

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