为了与 执行 Diffie-Hellman 密钥协议Curve25519
,我使用 生成以下密钥对BouncyCastle 1.68
:
// Generate a key pair
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("X25519", BouncyCastleProvider.PROVIDER_NAME);
keyPairGenerator.initialize(new XDHParameterSpec(XDHParameterSpec.X25519));
KeyPair keyPair = keyPairGenerator.generateKeyPair();
使用这个密钥对,我现在可以成功地执行密钥协议:
// Perform a (dummy) key agreement
KeyAgreement keyAgreement = KeyAgreement.getInstance("X25519", BouncyCastleProvider.PROVIDER_NAME);
keyAgreement.init(keyPair.getPrivate());
keyAgreement.doPhase(keyPair.getPublic(), true);
byte[] secret = keyAgreement.generateSecret();
现在我想将此密钥对安全地存储在 BCFKS 密钥库中以供将来使用,如下所示:
// Create a key store for the key pair
KeyStore keyStore = KeyStore.getInstance("BCFKS", BouncyCastleProvider.PROVIDER_NAME);
keyStore.load(null, "keyStorePassword".toCharArray());
// Put the key pair in the key store as a PrivateKeyEntry
final X509Certificate selfSignedCertificate = generateSelfSignedCertificate(keyPair); // TODO: How to generate a certificate?
final KeyStore.PrivateKeyEntry entry = new KeyStore.PrivateKeyEntry(keyPair.getPrivate(), new Certificate[]{selfSignedCertificate});
keyStore.setEntry("alias", entry, new KeyStore.PasswordProtection("keyEntryPassword".toCharArray()));
...除了KeyStore.PrivateKeyEntry
构造函数需要证书(而不是公钥),并且X25519
根据定义不能用于签署证书。(尝试使用它创建签名者自然会失败java.lang.IllegalArgumentException: Unknown signature type requested: X25519
)
我在这里遗漏了一些明显的东西,还是目前没有简单的方法将X25519
密钥对放入BCFKS
密钥库?考虑到我真的不需要证书,是否可以应用一种解决方法,只是一种将私钥/公钥存储在密钥库中的方法?