gpt4 book ai didi

java - 算法 HmacPBESHA256 不可用

转载 作者:行者123 更新时间:2023-12-02 00:11:38 30 4
gpt4 key购买 nike

因此,我编写了一些代码来获取 PEM,通过 bouncycaSTLe 将其添加到 PKCS keystore ,然后使用 java crypto 将 PKCS keystore 值导入 JKS keystore 。

我发誓昨天我成功执行了这些步骤,通过了单元测试,但是今天早上我开始这样做

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

代码并不多。首先,我通过 bouncycaSTLe 创建一个 PKCS keystore ,添加一个 pem 并以 PKCS12 的形式保存到磁盘。然后通过 java crypto 导入,保存为 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") 中指定提供程序。

最佳答案

打开 Android Studio 并转到File > Settings > Build, Execution, Development > Build Tools > Gradle,然后将 Gradle JDK 更改为 jdk16,然后单击 Sync Gradle。

并且您可以毫无错误地进入构建项目。

关于java - 算法 HmacPBESHA256 不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39797759/

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