java如何使用privatekey encryption PBEWIHMACSHA512andaes128创建PKCS12密钥库
我需要在Scala/Java中构建一个PKCS-12文件,我希望使用基于AES的私钥加密(例如PBewithhmacsha512andaes128)
我使用此代码(取自here)
val outputStream = new FileOutputStream(file)
val salt = new Array[Byte](20)
new SecureRandom().nextBytes(salt)
val kspkcs12 = KeyStore.getInstance("PKCS12")
kspkcs12.load(null, null)
kspkcs12.setEntry("test", new KeyStore.PrivateKeyEntry(keys.getPrivate, Array(cert)),
new KeyStore.PasswordProtection("changeMe".toCharArray, "PBEWithHmacSHA512AndAES_128", new PBEParameterSpec(salt,
100000)))
kspkcs12.store(outputStream, "changeMe".toArray)
现在,当使用
openssl pkcs12 -info -in filename.p12 -noout
我得到:
MAC:sha1 Iteration 100000 PKCS7 Data Shrouded Keybag: PBES2<unsupported parameters> PKCS7 Encrypted data: pbeWithSHA1And40BitRC2-CBC, Iteration 50000 Certificate bag'
为什么我得到的是“PKCS7加密数据”而不是PKCS12? 我从报纸上了解到
"PBES2<unsupported parameters>"
提供程序不支持请求的算法的消息。是否有PKCS12提供程序可以这样做
# 1 楼答案
PKCS是一堆规范,即每个更高级别的规范重复使用较低级别的规范
在PKCS#12的情况下,我们至少会遇到:
因此,在输出中看到与PKCS#7相关的消息是正常的
是的,这是Java中的一个bug。内置JCE提供程序在输出流中错误地对PBES2标记进行了两次编码:
偶尔
每隔一段时间:
以下是它产生的结果(我将HMAC SHA256与AES256结合使用):
下面是它应该是怎样的(来自
openssl pkcs12 -encode
的输出):它应该如何在RFC 2898, Appendix A.4中指定
是的,以下是如何使用BouncyCastle创建有效的PKCS#12密钥库:
结果是: