为什么证书中的公钥为空

时间:2019-03-15 02:46:07

标签: java android ssl x509certificate

我使用以下代码在android设备上生成证书:

//keypair 
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC"); 
keyPairGenerator.initialize(2048,new SecureRandom()); 
KeyPair keyPair = keyPairGenerator.generateKeyPair(); 

//certificate 
Calendar calendar = Calendar.getInstance(); 
calendar.set(2009, 0, 1); 
Date notBefore = new Date(calendar.getTimeInMillis()); 
calendar.set(2029, 0, 1); 
Date notAfter = new Date(calendar.getTimeInMillis()); 
BigInteger serialNumber = 
BigInteger.valueOf(Math.abs(System.currentTimeMillis())); 
X509V3CertificateGenerator certGen = new X509V3CertificateGenerator(); 
X500Principal dnName = new 
X500Principal("C=XX,ST=XX,L=XX,O=XX,OU=XX,CN=XXXXX"); 
certGen.setSerialNumber(serialNumber); 
certGen.setIssuerDN(dnName); 
certGen.setNotBefore(notBefore); 
certGen.setNotAfter(notAfter); 
certGen.setSubjectDN(dnName); 
certGen.setPublicKey(keyPair.getPublic());         //set public key here 
certGen.setSignatureAlgorithm("SHA256WithRSAEncryption"); 
PrivateKey signingKey = keyPair.getPrivate(); 

X509Certificate cert = certGen.generate(signingKey,"BC"); 

//why certification's public keWy is null? 
android.util.Log.e("mylog","cert.getPublicKey():"+cert.getPublicKey()); 

该日志表明cert.getPublicKey()返回null。怎么了?

0 个答案:

没有答案