ProcessBuilder 并运行包含空格的 OpenSSL 命令 [英] ProcessBuilder and running OpenSSL command which contains spaces

查看:24
本文介绍了ProcessBuilder 并运行包含空格的 OpenSSL 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Ubuntu 环境中使用我的 jar 执行 openSSL 命令时遇到问题.我得出的结论是,这是因为文件路径中的空间作为命令中的参数传递,例如以下命令中的 SHA 256.我使用了 process 和 ProcessBuilder 类来执行相同的操作:

I am facing a problem while executing openSSL command using my jar in Ubuntu environemnt. I have concluded that this is happening because of the space in the path of the file which is being passed as a parameter in the command e.g. SHA 256 in below command. I have used both process and ProcessBuilder classes for executing the same:

第一:

String certFilePath = "/home/mplusuer/Desktop/Nishant/210515/TestData/TestData/SHA 256/nishant.cer"
String []cmdGetAlgorithm = new String[3];

cmdGetAlgorithm[0] = "openssl x509 -in";
cmdGetAlgorithm[1] = certFilePath;
cmdGetAlgorithm[2] = "-noout -text -certopt no_subject,no_header,no_version,no_serial,no_validity,no_subject,no_issuer,no_pubkey,no_sigdump,no_aux,no_extensions";

ProcessBuilder pb = new ProcessBuilder(cmdGetAlgorithm[0], cmdGetAlgorithm[1],cmdGetAlgorithm[2]);
// setup other options ...

Process processGetAlgorithm = pb.start();
processGetAlgorithm.waitFor();

第二:

Runtime runtime = Runtime.getRuntime();
String cmdGetAlgorithm = "openssl x509 -in "
        + certFilePAth
        + " -noout -text -certopt no_subject,no_header,no_version,no_serial,no_validity,no_subject,no_issuer,no_pubkey,no_sigdump,no_aux,no_extensions ";

Process processGetAlgorithm = runtime.exec(cmdGetAlgorithm);

最后的命令如下,如果在命令提示符下单独执行它可以正常工作,但使用java代码执行时会失败:

the final command is as below, which works fine if executed separately on command prompt but fails when executed using java code:

openssl x509 -in /home/mplusuer/Desktop/Nishant/210515/TestData/TestData/SHA 256/suketu.cer  
  -noout -text -certopt no_subject,no_header,no_version,no_serial,no_validity,no_subject, 
   no_issuer,no_pubkey,no_sigdump,no_aux,no_extensions

我也使用以下方法来解决此问题,但没有按预期工作:

I have used the below methods also for resolving this issue, but nothing worked as per expectation:

String quoted = """ + certFilePath + """;
String escaped = certFilePath.replace(" ", "\ ");

请注意并帮助我解决相同的问题.

Please see to it and help me in resolving the same.

推荐答案

cmdGetAlgorithm[0] = "openssl x509 -in";
...

正如@immibis 在评论中所述,arg[0] 是程序名称.所以向量应该看起来像:

As @immibis stated in the comments, arg[0] is the program name. So the vector should look something like:

cmdArg[0] = "/usr/local/ssl/bin/openssl";
cmdArg[1] = "x509";
cmdArg[2] = "-in";
cmdArg[3] = certFilePAth;
cmdArg[4] = "-noout"
cmdArg[5] = "-text";
cmdArg[6] = "-certopt";
cmdArg[7] = "no_subject,no_header,no_version,no_serial,no_validity," +
            "no_issuer,no_pubkey,no_sigdump,no_aux,no_extensions ";

您应该始终指定可执行文件的完整文件名,以确保您正在运行预期的可执行文件,而不是对手植入的内容.

You should always specify the complete filename of the executable to ensure you are running the intended executable, and not something planted by an adversary.

这篇关于ProcessBuilder 并运行包含空格的 OpenSSL 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆