我正在尝试在我的项目中使用 Softhsm2。不幸的是,它一直显示
java.security.ProviderException:解析配置时出错
我尝试重新安装应用程序,但问题仍然存在。我可以知道我错过了什么吗?请检查以下源代码、配置文件、令牌、Java 版本和环境变量。
public static void main(String[] args) {
try {
String configName = "softhsm2.conf";
Provider p = new SunPKCS11(configName);
if (-1 == Security.addProvider(p)) {
throw new RuntimeException("could not add security provider");
}
// Load the key store
char[] pin = "mypin".toCharArray();
KeyStore keyStore = KeyStore.getInstance("PKCS11", p);
keyStore.load(null, pin);
// AES key
SecretKeySpec secretKeySpec = new SecretKeySpec("0123456789ABCDEF".getBytes(), "AES");
Key key = new SecretKeySpec(secretKeySpec.getEncoded(), "AES");
keyStore.setKeyEntry("AA", key, "1234".toCharArray(), null);
keyStore.store(null); //this gives me the exception.
} catch(Exception e) {
System.out.println("error: " + e.toString());
}
}
生成的令牌:
Microsoft Windows [Version 10.0.17134.1304]
(c) 2018 Microsoft Corporation. All rights reserved.
C:\WINDOWS\system32>cd/
C:\>cd softhsm2
C:\SoftHSM2>cd bin
C:\SoftHSM2\bin>softhsm2-util.exe --show-slots
Available slots:
Slot 1759353984
Slot info:
Description: SoftHSM slot ID 0x68dd9c80
Manufacturer ID: SoftHSM project
Hardware version: 2.5
Firmware version: 2.5
Token present: yes
Token info:
Manufacturer ID: SoftHSM project
Model: SoftHSM v2
Hardware version: 2.5
Firmware version: 2.5
Serial number: 8414edde68dd9c80
Initialized: yes
User PIN init.: yes
Label: My token 2
Slot 2066642166
Slot info:
Description: SoftHSM slot ID 0x7b2e74f6
Manufacturer ID: SoftHSM project
Hardware version: 2.5
Firmware version: 2.5
Token present: yes
Token info:
Manufacturer ID: SoftHSM project
Model: SoftHSM v2
Hardware version: 2.5
Firmware version: 2.5
Serial number: 9176dec57b2e74f6
Initialized: yes
User PIN init.: yes
Label: My token 1
Slot 2
Slot info:
Description: SoftHSM slot ID 0x2
Manufacturer ID: SoftHSM project
Hardware version: 2.5
Firmware version: 2.5
Token present: yes
Token info:
Manufacturer ID: SoftHSM project
Model: SoftHSM v2
Hardware version: 2.5
Firmware version: 2.5
Serial number:
Initialized: no
User PIN init.: no
Label:
C:\SoftHSM2\bin>
配置:C:\SoftHSM2\etc\softhsm2.conf
# SoftHSM v2 configuration file
directories.tokendir = C:\SoftHSM2\var\softhsm2\tokens\
objectstore.backend = file
log.level = INFO
slots.removable = false
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)
环境变量 SOFTHSM2_CONF C:\SoftHSM2\etc\softhsm2.conf
路径:C:\SoftHSM2\lib\