每次尝试访问IO下的证书时,我都尝试读取带有IO文件的证书。
testApp/src/main/resources -> cert
它总是读
X:\workspace1\testApp\target\classes\cert\test.p12
这是我正在使用的代码,它总是用空指针排除。
// String fileName = "config/sample.txt";
ClassLoader classLoader = App2.class.getClassLoader();
File file = new File(classLoader.getResource("cert/test.p12").getFile());
FileInputStream fm = new FileInputStream(file);
KeyStore ks = KeyStore.getInstance("PKCS12");
try {
ks.load(fm, "test".toCharArray());
}
catch(Exception e) {
e.printStackTrace();
}
Key key = ks.getKey("test", "test".toCharArray());
Certificate cert = ks.getCertificate("test");
PublicKey publicKey = cert.getPublicKey();
System.out.println("Public key");
System.out.println(Base64.getEncoder().encodeToString(publicKey.getEncoded()));
fm.close();
我想阅读此证书以提取公钥或私钥。
答案 0 :(得分:0)
这对我有用
File file = new File(classLoader.getResource("./cert/test.p12").getFile());