gpt4 book ai didi

java - 是否可以在 SoftHSM 上存储 key ?

转载 作者:太空狗 更新时间:2023-10-29 12:11:56 29 4
gpt4 key购买 nike

我找到了这个线程:Connecting to SoftHSM java,它在存储私钥时有效,就像示例一样。

但是我需要存储 key ,比如AES。

这是我的代码:

import java.security.*;
import sun.security.pkcs11.*;
import javax.crypto.spec.SecretKeySpec;

public class Main {
public static void main(String[] args) throws Exception {
// Set up the Sun PKCS 11 provider
String configName = "softhsm.cfg";
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.
}
}

这是 softhsm.cfg 文件:

name = SoftHSM
library = /usr/local/lib/softhsm/libsofthsm.so
slot = 0
attributes(generate, *, *) = {
CKA_TOKEN = true
}
attributes(generate, CKO_CERTIFICATE, *) = {
CKA_PRIVATE = false
}
attributes(generate, CKO_PUBLIC_KEY, *) = {
CKA_PRIVATE = false
}

当执行 keyStore.store(null) 时,我得到 java.security.KeyStoreException Cannot convert to PKCS11 keys

最佳答案

原来异常发生在 SoftHSMv1 上。我安装了 SoftHSMv2,您可以使用 git 从 GitHub 下载它,并用它来存储 key 。不要从 OpenDNSsec 网站下载 SoftHSMv2,因为它不会工作!

最后我不得不更改 softhsm.cfg 文件以指向新库,出于某种原因我忽略了,SoftHSM2 更改了初始化插槽的数量,您可以使用 sudo softhsm2-util --show- 验证它插槽

软件配置文件:

name = SoftHSM
library = /usr/local/lib/softhsm/libsofthsm2.so
slot = 498488451
attributes(generate, *, *) = {
CKA_TOKEN = true
}
attributes(generate, CKO_CERTIFICATE, *) = {
CKA_PRIVATE = false
}
attributes(generate, CKO_PUBLIC_KEY, *) = {
CKA_PRIVATE = false
}

我的代码:

import java.security.*;
import sun.security.pkcs11.*;
import javax.crypto.spec.SecretKeySpec;

public class Main {
public static void main(String[] args) throws Exception {
// Set up the Sun PKCS 11 provider
String configName = "softhsm.cfg";
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 no longer gives me the exception.

Enumeration<String> aliases = keyStore.aliases();
while(aliases.hasMoreElements()){
String alias = aliases.nextElement();
System.out.println(alias + ": " + keyStore.getKey(alias,"1234".toCharArray()));
}
}
}

这给了我输出:

AA: SunPKCS11-SoftHSM AES secret key, 16 bits (id 2, token object, not sensitive, unextractable)

如果您尝试使用 keyStore.getKey("AA", "1234".toCharArray()); 之类的方法获取 key ,您将获得一个具有 key 某些属性的对象,但是您将无法使用 .getEncoded() 实际获取 key 本身,因为它是不可提取的。

关于java - 是否可以在 SoftHSM 上存储 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40381171/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com