"Get Key Failed: Given final block not properly padded" 当我尝试在 Java 项目中加载 .p12 证书时
"Get Key Failed: Given final block not properly padded" when I try loading .p12 certificate in a Java project
具体来说,代码的相关部分如下所示(大写单词是占位符):
FileInputStream("PATH TO P12 FILE");
KeyStore keyStore = KeyStore.getInstance( "PKCS12" );
keyStore.load( file_inputstream, "PASSWORD".toCharArray() );
Key privatni = keyStore.getKey( "ALIAS", " PASSWORD ".toCharArray() );
Signature biljeznik = Signature.getInstance( "SHA256withRSA" );
biljeznik.initSign( ( PrivateKey )privatni );
biljeznik.update( medjurezultat.getBytes() );
potpisano = biljeznik.sign();
我 100% 确定密码和别名是正确的。我知道 P12 文件不能像 JKS 密钥库那样有 2 个密码,但我不确定如果这是问题的话如何更改代码(唯一的密码写在两个“密码”占位符下)。
确切的错误信息是这条:
java.security.UnrecoverableKeyException: Get Key failed: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at java.base/sun.security.pkcs12.PKCS12KeyStore.engineGetKey(PKCS12KeyStore.java:450)
at java.base/sun.security.util.KeyStoreDelegator.engineGetKey(KeyStoreDelegator.java:91)
at java.base/java.security.KeyStore.getKey(KeyStore.java:1050)
at primjer.ZastitniKodIzracun.main(ZastitniKodIzracun.java:56)
Caused by: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at java.base/com.sun.crypto.provider.CipherCore.unpad(CipherCore.java:859)
at java.base/com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:939)
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:735)
at java.base/com.sun.crypto.provider.PKCS12PBECipherCore.implDoFinal(PKCS12PBECipherCore.java:424)
at java.base/com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndDESede.engineDoFinal(PKCS12PBECipherCore.java:456)
at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2205)
at java.base/sun.security.pkcs12.PKCS12KeyStore.lambda$engineGetKey[=13=](PKCS12KeyStore.java:371)
at java.base/sun.security.pkcs12.PKCS12KeyStore$RetryWithZero.run(PKCS12KeyStore.java:257)
at java.base/sun.security.pkcs12.PKCS12KeyStore.engineGetKey(PKCS12KeyStore.java:361)
... 3 more
非常感谢任何帮助。
这是一个非常愚蠢的错误:
keyStore.load( file_inputstream, "PASSWORD".toCharArray() );
Key privatni = keyStore.getKey( "ALIAS", " PASSWORD ".toCharArray() );
第二个密码中的空格导致了错误。应该是:
keyStore.load( file_inputstream, "PASSWORD".toCharArray() );
Key privatni = keyStore.getKey( "ALIAS", "PASSWORD".toCharArray() );
具体来说,代码的相关部分如下所示(大写单词是占位符):
FileInputStream("PATH TO P12 FILE");
KeyStore keyStore = KeyStore.getInstance( "PKCS12" );
keyStore.load( file_inputstream, "PASSWORD".toCharArray() );
Key privatni = keyStore.getKey( "ALIAS", " PASSWORD ".toCharArray() );
Signature biljeznik = Signature.getInstance( "SHA256withRSA" );
biljeznik.initSign( ( PrivateKey )privatni );
biljeznik.update( medjurezultat.getBytes() );
potpisano = biljeznik.sign();
我 100% 确定密码和别名是正确的。我知道 P12 文件不能像 JKS 密钥库那样有 2 个密码,但我不确定如果这是问题的话如何更改代码(唯一的密码写在两个“密码”占位符下)。
确切的错误信息是这条:
java.security.UnrecoverableKeyException: Get Key failed: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at java.base/sun.security.pkcs12.PKCS12KeyStore.engineGetKey(PKCS12KeyStore.java:450)
at java.base/sun.security.util.KeyStoreDelegator.engineGetKey(KeyStoreDelegator.java:91)
at java.base/java.security.KeyStore.getKey(KeyStore.java:1050)
at primjer.ZastitniKodIzracun.main(ZastitniKodIzracun.java:56)
Caused by: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at java.base/com.sun.crypto.provider.CipherCore.unpad(CipherCore.java:859)
at java.base/com.sun.crypto.provider.CipherCore.fillOutputBuffer(CipherCore.java:939)
at java.base/com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:735)
at java.base/com.sun.crypto.provider.PKCS12PBECipherCore.implDoFinal(PKCS12PBECipherCore.java:424)
at java.base/com.sun.crypto.provider.PKCS12PBECipherCore$PBEWithSHA1AndDESede.engineDoFinal(PKCS12PBECipherCore.java:456)
at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2205)
at java.base/sun.security.pkcs12.PKCS12KeyStore.lambda$engineGetKey[=13=](PKCS12KeyStore.java:371)
at java.base/sun.security.pkcs12.PKCS12KeyStore$RetryWithZero.run(PKCS12KeyStore.java:257)
at java.base/sun.security.pkcs12.PKCS12KeyStore.engineGetKey(PKCS12KeyStore.java:361)
... 3 more
非常感谢任何帮助。
这是一个非常愚蠢的错误:
keyStore.load( file_inputstream, "PASSWORD".toCharArray() );
Key privatni = keyStore.getKey( "ALIAS", " PASSWORD ".toCharArray() );
第二个密码中的空格导致了错误。应该是:
keyStore.load( file_inputstream, "PASSWORD".toCharArray() );
Key privatni = keyStore.getKey( "ALIAS", "PASSWORD".toCharArray() );