[英]How can I get an ssh key data from a command line argument in java program?
I'm trying to get an ssh private key data from command line.我正在尝试从命令行获取 ssh 私钥数据。
./myProgram --prvKey '-----BEGIN RSA PRIVATE KEY-----\nxxx\n-----END RSA PRIVATE KEY-----\n'
In my Git class, I use the key data as below:在我的 Git class 中,我使用如下关键数据:
protected JSch createDefaultJSch(FS fs) throws JSchException {
String prvKeyData = Main.getPrvKeyData();
System.out.println(prvKeyData);
JSch jSch = super.createDefaultJSch(fs);
jSch.addIdentity("monitoring-configs", prvKeyData.getBytes(), null, "passphrase".getBytes());
return jSch;
}
But this fails with an invalid ssh key error message.但这会因无效的 ssh 密钥错误消息而失败。
I checked that it works well when putting that string into the code directly.我检查了直接将该字符串放入代码时它是否运行良好。
And I also checked that the System.out.println
result is exactly the same as the input I gave(containing \n
).而且我还检查了
System.out.println
结果与我给出的输入完全相同(包含\n
)。
I guess that bash does something with backslashes, so that in my Java program, the \n
is not understood properly.我猜想 bash 用反斜杠做了一些事情,所以在我的 Java 程序中,
\n
没有被正确理解。
How can I debug and resolve this?我该如何调试和解决这个问题?
If you can change the input string, just use enter instead of '\n' sequences.如果您可以更改输入字符串,只需使用 enter 而不是 '\n' 序列。
/myProgram --prvKey '-----BEGIN RSA PRIVATE KEY-----
xxx
-----END RSA PRIVATE KEY-----
'
Then the java argument will contain new lines instead of the string "\n"然后 java 参数将包含新行而不是字符串“\n”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.