I need to open, read, and get the path of .pfx file. The file is inside my android studio project folder, MyApp/certificate/mycertificate.pfx.
public void openFile(){
CkCert cert = new CkCert();
File file = new File("/certificate/mycertificate.pfx");
String pfxFilename = file.getAbsolutePath();
String pfxPassword = "1234";
cert.LoadPfxFile(pfxFilename,pfxPassword)};
I'm getting the following error: No such file or directory. Any suggestion?
Solved: I moved the file to \app\src\main\assets
public void open(Context context){
InputStream cert= context.getAssets().open("mycert.pfx");
char[] password = null;
KeyStore keystore = null;
Enumeration<String> aliases;
String alias = "";
//open the file
senha = "1234".toCharArray();
keystore = KeyStore.getInstance("PKCS12");
keystore.load(cert, password);
//Get the alias
KeyStore.PrivateKeyEntry pkEntry = null;
PrivateKey pk = null;
try {
aliases = keystore.aliases();
while(aliases.hasMoreElements()) {
alias = aliases.nextElement();
System.out.println(alias);
if (keystore.isKeyEntry(alias)) {
pkEntry = (KeyStore.PrivateKeyEntry) keystore.getEntry(alias, new KeyStore.PasswordProtection(password));
pk = pkEntry.getPrivateKey();
}
}
} catch (KeyStoreException e) {
throw new RuntimeException("CATCH", e);
}
}