算法 HmacPBESHA256 不可用
因此,我编写了一些代码来获取 PEM,通过 bouncycastle 将其添加到 PKCS 密钥库,然后使用 java 加密将 PKCS 密钥库值导入 JKS 密钥库。
我发誓,昨天我成功地执行了这些步骤的单元测试通过,但今天早上我开始点击这个
Caused by: java.security.NoSuchAlgorithmException: Algorithm HmacPBESHA256 not available
at javax.crypto.Mac.getInstance(Mac.java:181) ~[na:1.8.0_60]
at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:2039) ~[na:1.8.0_65]
现在,总是有可能在我下面有变化,但我不知道它是什么。似乎我用于该算法的任何提供程序都消失了。
这是我的java.security文件片段:
security.provider.1=sun.security.provider.Sun
security.provider.2=sun.security.rsa.SunRsaSign
security.provider.3=sun.security.ec.SunEC
security.provider.4=com.sun.net.ssl.internal.ssl.Provider
security.provider.5=com.sun.crypto.provider.SunJCE
security.provider.6=sun.security.jgss.SunProvider
security.provider.7=com.sun.security.sasl.Provider
security.provider.8=org.jcp.xml.dsig.internal.dom.XMLDSigRI
security.provider.9=sun.security.smartcardio.SunPCSC
security.provider.10=apple.security.AppleProvider
代码中没有很多内容。首先,我通过弹跳城堡创建一个PKCS密钥库,添加一个pem并作为PKCS12保存到磁盘。然后通过java加密导入,保存回JKS。
public KeystoreBuilder createJksFromPem(String pemPrivate, String pemPublic, String alias) throws Exception
{
Preconditions.checkState(StringUtils.isNotEmpty(pemPrivate), "pemPrivate must not be empty");
Preconditions.checkState(StringUtils.isNotEmpty(pemPublic), "pemPublic must not be empty");
Preconditions.checkState(StringUtils.isNotEmpty(alias), "alias must not be empty");
String pkcsFilename = filename + ".pkcs";
convertPemToPkcs(pemPrivate, pemPublic, pkcsFilename);
importPkcsIntoJks(pkcsFilename);
return this;
}
private void importPkcsIntoJks(String pkcsFilename) throws Exception
{
KeyStore pkcs = KeyStore.getInstance("PKCS12");
File pkcsFile = new File(pkcsFilename);
try (FileInputStream fis = new FileInputStream(pkcsFile))
{
pkcs.load(fis, password.toCharArray());
}
pkcsFile.delete();
KeyStore jks = KeyStore.getInstance("JKS");
jks.load(null);
Enumeration<String> aliases = pkcs.aliases();
while (aliases.hasMoreElements())
{
String alias = aliases.nextElement();
if (!pkcs.isKeyEntry(alias))
{
continue;
}
Key key = pkcs.getKey(alias, password.toCharArray());
Certificate[] chain = pkcs.getCertificateChain(alias);
jks.setKeyEntry(alias, key, password.toCharArray(), chain);
}
persist(jks);
}
private void convertPemToPkcs(String pemPrivate, String pemPublic, String pkcsFilename) throws IOException, NoSuchAlgorithmException, OperatorCreationException, PKCSException, FileNotFoundException
{
Security.addProvider(new BouncyCastleProvider());
X509CertificateHolder cert = (X509CertificateHolder) readObject(pemPublic);
PEMKeyPair keyPair = (PEMKeyPair) readObject(pemPrivate);
JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils();
PKCS12SafeBagBuilder pkcs12BagBuilder = new PKCS12SafeBagBuilder(cert);
pkcs12BagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_friendlyName, new DERBMPString("Kafka SSL Certificate"));
pkcs12BagBuilder.addBagAttribute(PKCSObjectIdentifiers.pkcs_9_at_localKeyId, extUtils.createSubjectKeyIdentifier(keyPair.getPublicKeyInfo()));
PKCS12PfxPduBuilder builder = new PKCS12PfxPduBuilder();
builder.addData(pkcs12BagBuilder.build());
builder.addEncryptedData(new JcePKCSPBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd128BitRC2_CBC).setProvider("BC").build(password.toCharArray()), pkcs12BagBuilder.build());
PKCS12PfxPdu pfx = builder.build(new JcePKCS12MacCalculatorBuilder(NISTObjectIdentifiers.id_sha256), password.toCharArray());
try (FileOutputStream fos = new FileOutputStream(new File(pkcsFilename)))
{
fos.write(pfx.getEncoded(ASN1Encoding.DL));
}
}
和繁荣它爆炸
pkcs.load(fis, password.toCharArray());
如您所见,BouncyCastleProvider已明确添加。有什么建议吗?
更新:感谢dave_thompson_085的建议。不敢相信我没有看到那个重载的方法,但解决方案是在调用get Keystore.getInstance(“PKCS12”,“BC”)中指定提供程序。