0

I am trying to add a private key pair to an existing Java KeyStore file. When I execute the following command via the Terminal, it works. (ie. when I execute "keytool -list -v -keystore ecekeystore.jks", I can see the newly added key)

keytool -genkey -alias blabla -keyalg RSA -keystore ecekeystore.jks -dname "CN=MyName, OU=blabla, O=blabla, L=blabla, S=blabla, C=US" -storepass password1 -keypass password2

But when I run the following piece of Java code, nothing changes in my keystore file.

    try {
        Runtime rt = Runtime.getRuntime();
        String command = "keytool " +
                "-genkey -alias blabla -keyalg RSA " +
                "-keystore ecekeystore.jks " +
                "-dname \"CN=MyName, OU=blabla, O=blabla, L=blabla, S=blabla, C=US\" " +
                "-storepass password1 " +
                "-keypass password2";
        System.out.println(command);
        Process pr = rt.exec(command);
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }

I'd appreciate any help!


SOLVED:

    Runtime rt = Runtime.getRuntime();
    try {
        String[] cmdArray = new String[14];
        cmdArray[0] = "keytool";
        cmdArray[1] = "-genkey";
        cmdArray[2] = "-alias";
        cmdArray[3] = "blabla";
        cmdArray[4] = "-keyalg";
        cmdArray[5] = "RSA";
        cmdArray[6] = "-keystore";
        cmdArray[7] = "ecekeystore.jks";
        cmdArray[8] = "-dname";
        cmdArray[9] = "CN=MyName, OU=blabla, O=blabla, L=blabla, S=blabla, C=US";
        cmdArray[10] = "-storepass";
        cmdArray[11] = "password1";
        cmdArray[12] = "-keypass";
        cmdArray[13] = "password1";

        Process pr = rt.exec(cmdArray);

        InputStream is = pr.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;

        System.out.printf("Output is:\n");

        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }

        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
9
  • 1
    This is a programming question, not a security question. It looks to me like Process.exec requires arguments to be specified in a separate array parameter. Commented Apr 23, 2018 at 18:29
  • I followed the documentation here (docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html), it says the only input is the String 'command'. Commented Apr 23, 2018 at 18:36
  • "This is a convenience method. An invocation of the form exec(command) behaves in exactly the same way as the invocation exec(command, null, null)." Also I hope it's clear that I meant Runtime.exec previously. Commented Apr 23, 2018 at 18:47
  • Are you in the proper directory? The -keystore is a relative path, so if you're not in the appropriate directory... Commented Apr 23, 2018 at 19:04
  • I tried specifying the working directory with 'Process pr = rt.exec(command, null, dir);' but nothing changed. Commented Apr 23, 2018 at 19:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.