在我的应用程序中,我使用p12证书文件在与我正在使用的API交谈时加密流量。
对于我的生产环境,我需要从系统而不是应用程序中读取这些文件。
在Linux上,我如何将我的系统中的这些文件从我的应用程序中读入到InputStream中,就像我从应用程序的资源目录中读取一样?
我正在使用Java。
答案 0 :(得分:1)
我构建了一个快速而肮脏的小班,以显示我使用keytools
创建的相对.pfx(P12)的开头。当然,如果有几个可能的位置,你也可以查看不同的潜在目录来查找文件。
文件结构如下所示:
./bin
./bin/Test.java
./bin/Test.class
./conf
./conf/myFile.pfx
这是测试代码:
import java.io.*;
import java.security.*;
class Test {
public static void main(String[] args) {
String pass = "password";
try {
File file = new File("../conf/myFile.pfx");
InputStream stream = new FileInputStream(file);
KeyStore store = KeyStore.getInstance("PKCS12");
store.load(stream, pass.toCharArray());
PrivateKey key = (PrivateKey)store.getKey("example", pass.toCharArray());
System.out.println("Success");
} catch (KeyStoreException kse) {
System.err.println("Error getting the key");
} catch (Exception e) {
System.err.println("Error opening the key file");
e.printStackTrace();
}
}
}