我在我的项目中使用java.security.PublicKey
但是,当我启用proguard时,我得到以下错误:
java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String java.security.PublicKey.getAlgorithm()' on a null object reference经过一番调查,我发现它落在了
val entry = KeyStore.PrivateKeyEntry(keyPair.private, arrayOf(certificate)) <--- this line
keyStore.setEntry(alias, entry, keyProtection)我该如何让它工作呢?
将以下内容添加到proguard文件不起作用:
-keep class java.security.** { *; }
-keepclassmembers class java.security.** { *; }
-keep public interface java.security.Key {*;}
-keep public interface java.security.PublicKey {*;}
-keepclassmembers class * implements java.security.PublicKey {
public <methods>;
}经过更多的调查后,我发现KeyStore.PrivateEntry()的构造函数中更具体的一行导致了这个问题
它就是
Certificate[] clonedChain = chain.clone();
clonedChain[0].publicKey is left null我如何让它也克隆公钥?
发布于 2020-01-22 17:35:14
这个问题是不推荐使用的代码。
我使用废弃的海绵城堡代码来生成一个自签名证书。所述证书中的公钥在处于保护状态时变为空。
我更改为以下内容:
api 'com.madgag.spongycastle:core:1.58.0.0'
api 'com.madgag.spongycastle:prov:1.58.0.0'
api 'com.madgag.spongycastle:bcpkix-jdk15on:1.58.0.0'
api 'com.madgag.spongycastle:bcpg-jdk15on:1.58.0.0'然后:
fun KeyPair.toSelfSignedCertificate(principal: String, signatureAlgorithm: String, validity: Int): X509Certificate? {
val x500Name = X500Name("CN=$principal")
val today = Calendar.getInstance()
val notBefore = today.timeInMillis
today.add(Calendar.YEAR, validity)
val notAfter = today.timeInMillis
val sigGen: ContentSigner = JcaContentSignerBuilder(signatureAlgorithm).build(private)
val publicKeyInfo = SubjectPublicKeyInfo.getInstance(ASN1Sequence.getInstance(public.encoded))
val certGen = X509v3CertificateBuilder(x500Name,
RSAKeyGenParameterSpec.F4,
Date(notBefore),
Date(notAfter),
x500Name,
publicKeyInfo)
val certHolder: X509CertificateHolder = certGen.build(sigGen)
return JcaX509CertificateConverter().getCertificate(certHolder)
}它工作得很好。
https://stackoverflow.com/questions/59848764
复制相似问题