执行以下代码时:
KeyStore ks = KeyStore.getInstance(storeType);
ks.load(new FileInputStream(keyStore), storePassword.toCharArray());
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, keyPassword.toCharArray());
我得到一个异常(exception):
java.security.UnrecoverableKeyException: Get Key failed: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
Caused by: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
这最初是使用 keytool 从 JKS keystore 转换为 PKCS12 keystore 。我尝试创建一个全新的 PKCS12 keystore ,但没有成功。
最佳答案
JKS 支持使用两种密码,一种用于商店,一种用于内部 key 。 PKCS12 对两者使用相同的密码。使用 keytool 时,您会收到有关此的警告。
当迁移到新的 keystore 时,代码可能会继续为 keystore 使用一个密码,并为 key 使用另一个(不同的)密码,尽管现在这行不通了。
Java 9 对此提供了更好的异常消息,表明它可能由解密期间的错误 key 引起。
在这种情况下,请确保传入与商店密码匹配的 key 密码。
关于java - BadPaddingException 加载 p12 keystore ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46701288/