-1

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);
    }

}

Tito
  • 49
  • 1
  • 8
  • `new File("mycertificate.pfx")` does not point anywhere meaningful. Where exactly is this file on the user's device? – CommonsWare Mar 10 '20 at 13:10
  • It's not in any device yet. It's only in the root folder of my android studio project. MyApp/certificate/mycertificate.pfx. I tried File file = new File("/certificate/mycertificate.pfx") and still getting the same error. – Tito Mar 10 '20 at 13:20
  • But your app is running on your Android device. And you are in town. You are never without phone. How could it find a file on your pc then!? – blackapps Mar 10 '20 at 13:30
  • running the app in my device wouldn't automatically get all the stuff [classes, libs, files, imgs etc ] it needs? – Tito Mar 10 '20 at 13:41

1 Answers1

0

It's only in the root folder of my android studio project

That file is on your development machine, not on any device. Instead, package it as an asset (src/main/assets/ of your module) and use AssetManager to open() an InputStream on that asset.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Something like this? InputStream certificate; certificate = getClass().getResourceAsStream("mycertificate.pfx"); – Tito Mar 10 '20 at 13:47
  • @Tito: No, something like `getAssets().open("mycertificate.pfx")`. `getAssets()` is a method on `Context`. – CommonsWare Mar 10 '20 at 14:41
  • InputStream certificate = getApplicationContext().getAssets().open("mycertificate.pfx"); – Tito Mar 10 '20 at 17:44
  • Now I'm getting W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference – Tito Mar 10 '20 at 17:45
  • @Tito: Presumably, you are trying to do this from a field initializer. Do not try to use `getApplicationContext()` (which is unnecessary here) or `getAssets()` until after `super.onCreate()` of your activity or service. – CommonsWare Mar 10 '20 at 17:49
  • I Solved it! public void open(Context context){InputStream certificate = context.getAssets().open("mycertificate.pfx")}; – Tito Mar 10 '20 at 17:53