java keystore pkcs12,为什么pkcs12的keyStore.aliases()为空

I'm trying to load PrivateKey from .p12 file by using this code:

Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

java.security.KeyStore keyStore = KeyStore.getInstance("PKCS12", "BC");

keyStore.load(new FileInputStream(new File("my_domain_com.p12")), password);

keyStore.aliases().hasMoreElements(); //this is false

java.security.PrivateKey privateKey = (PrivateKey) keyStore.getKey("SomeAlias", password);

I'm trying to find the reason why there is no aliases. But I'm not able to find.

What can be reason for the empty alias?

I want to get private key and ecrypt some text using this key. Is there other apporach?

I also have .cer file but I'm not sure I should use together.

解决方案

Is it possible the keystore has nothing in it at all? Use the Java keytool command to verify.

>keytool -list -v -keystore test.p12 -storetype PKCS12

Enter keystore password:

Keystore type: PKCS12

Keystore provider: SunJSSE

Your keystore contains 1 entry

Alias name: test_alias

Creation date: Sep 3, 2013

Entry type: PrivateKeyEntry

Certificate chain length: 1

Certificate[1]:

...

...

If there are entries in the keystore, you should see an "Alias name" for each one. If there are no entries in the keystore, you will see "Your keystore contains 0 entries", and you will need to import them into the keystore.

Also, when encrypting, you should encrypt with someone else's public key, and they will decrypt with their private key. And they encrypt with your public key, which you decrypt with your private key.

java.security path: D:\biayu\jdk\jre\lib\security Security providers: [SUN version 1.8, SunRsaSign version 1.8, SunEC version 1.8, SunJSSE version 1.8, SunJCE version 1.8, SunJGSS version 1.8, SunSASL version 1.8, XMLDSig version 1.8, SunPCSC version 1.8, SunMSCAPI version 1.8] SSLContext provider info: Sun JSSE provider(PKCS12, SunX509/PKIX key/trust factories, SSLv3/TLSv1/TLSv1.1/TLSv1.2/TLSv1.3) SSLContext provider services: [SunJSSE: KeyPairGenerator.RSA -> sun.security.rsa.RSAKeyPairGenerator$Legacy aliases: [OID.1.2.840.113549.1.1, 1.2.840.113549.1.1, 1.2.840.113549.1.1.1] , SunJSSE: KeyFactory.RSA -> sun.security.rsa.RSAKeyFactory$Legacy aliases: [OID.1.2.840.113549.1.1, 1.2.840.113549.1.1, 1.2.840.113549.1.1.1] , SunJSSE: Signature.SHA1withRSA -> sun.security.rsa.RSASignature$SHA1withRSA aliases: [OID.1.2.840.113549.1.1.5, 1.2.840.113549.1.1.5, 1.3.14.3.2.29] attributes: {SupportedKeyClasses=java.security.interfaces.RSAPublicKey|java.security.interfaces.RSAPrivateKey} , SunJSSE: SSLContext.TLS -> sun.security.ssl.SSLContextImpl$TLSContext aliases: [SSL] , SunJSSE: SSLContext.TLSv1 -> sun.security.ssl.SSLContextImpl$TLS10Context aliases: [SSLv3] , SunJSSE: Signature.MD2withRSA -> sun.security.rsa.RSASignature$MD2withRSA aliases: [OID.1.2.840.113549.1.1.2, 1.2.840.113549.1.1.2] attributes: {SupportedKeyClasses=java.security.interfaces.RSAPublicKey|java.security.interfaces.RSAPrivateKey} , SunJSSE: Signature.MD5withRSA -> sun.security.rsa.RSASignature$MD5withRSA aliases: [OID.1.2.840.113549.1.1.4, 1.2.840.113549.1.1.4] attributes: {SupportedKeyClasses=java.security.interfaces.RSAPublicKey|java.security.interfaces.RSAPrivateKey} , SunJSSE: KeyStore.PKCS12 -> sun.security.pkcs12.PKCS12KeyStore , SunJSSE: TrustManagerFactory.SunX509 -> sun.security.ssl.TrustManagerFactoryImpl$SimpleFactory , SunJSSE: KeyManagerFactory.SunX509 -> sun.security.ssl.KeyManagerFactoryImpl$SunX509 , SunJSSE: TrustManagerFactory.PKIX -> sun.security.ssl.TrustManagerFactoryImpl$PKIXFactory aliases: [SunPKIX, X509, X.509] , SunJSSE: KeyManagerFactory.NewSunX509 -> sun.security.ssl.KeyManagerFactoryImpl$X509 aliases: [PKIX] , SunJSSE: SSLContext.Default -> sun.security.ssl.SSLContextImpl$DefaultSSLContext , SunJSSE: SSLContext.TLSv1.1 -> sun.security.ssl.SSLContextImpl$TLS11Context , SunJSSE: Signature.MD5andSHA1withRSA -> sun.security.ssl.RSASignature , SunJSSE: SSLContext.TLSv1.3 -> sun.security.ssl.SSLContextImpl$TLS13Context , SunJSSE: SSLContext.TLSv1.2 -> sun.security.ssl.SSLContextImpl$TLS12Context ] java.ext.dirs: D:\biayu\jdk\jre\lib\ext;C:\Windows\Sun\Java\lib\ext 2023-07-14 09:02:08.895 ERROR 6772 --- [ main] com.zaxxer.hikari.pool.HikariPool : master - Exception during pool initialization. com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“The server selected protocol version TLS10 is not accepted by client preferences [TLS13, TLS12]”。 ClientConnectionId:275886a9-fcb1-451b-af33-3b3ea3195ee6
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值