獲取.crt證書的資訊

J_bai發表於2020-09-24

獲取.crt證照的資訊

生成後的.crt檔案的位置 :==cerPath==要能拿到生成的RootCa.pfx檔案

測試的程式碼如下:

		public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, UnrecoverableKeyException, CertificateException, SignatureException, IOException {
		try {
			getX509CerCate("D:\\file\\");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//CerUtil util = new CerUtil();
		//util.createRootCA();
		//util.signCert("dev1","hijklm","zxcvbn");
	}
	public static X509Certificate getX509CerCate(String cerPath) throws Exception {
		KeyStore ks = KeyStore.getInstance("pkcs12");
		FileInputStream ksfis = new FileInputStream(cerPath + "RootCa.pfx");
		char[] storePwd = "12345678".toCharArray();

		char[] keyPwd = "abcd1234".toCharArray();
		ks.load(ksfis, storePwd);

		ksfis.close();
		PrivateKey privK = (PrivateKey) ks.getKey("RootCA", keyPwd);
		X509Certificate certificate = (X509Certificate) ks
				.getCertificate("RootCA");
		byte certbytes[] = certificate.getEncoded();
		X509CertImpl x509certimpl = new X509CertImpl(certbytes);
		X509CertInfo x509certinfo = (X509CertInfo) x509certimpl
				.get("x509.info");
		System.out.println("讀取Crt證照資訊...");
		System.out.println(x509certinfo);
		System.out.println("x509Certificate_SerialNumber_序列號___:"+x509certimpl.getSerialNumber());
		System.out.println("x509Certificate_getIssuerDN_釋出方標識名___:"+x509certimpl.getIssuerDN()); 
		System.out.println("x509Certificate_getSubjectDN_主體標識___:"+x509certimpl.getSubjectDN());
		System.out.println("x509Certificate_getSigAlgOID_證照演算法OID字串___:"+x509certimpl.getSigAlgOID());
		System.out.println("x509Certificate_getNotBefore_證照有效期___:"+x509certimpl.getNotAfter());
		System.out.println("x509Certificate_getSigAlgName_簽名演算法___:"+x509certimpl.getSigAlgName());
		System.out.println("x509Certificate_getVersion_版本號___:"+x509certimpl.getVersion());
		System.out.println("x509Certificate_getPublicKey_公鑰___:"+x509certimpl.getPublicKey());
		return x509certimpl;
	}

**

列印的資訊:

**
讀取Crt證照資訊…
[
Version: V3
Subject: CN=RootCA, OU=SHEPC, O=IOT, L=SHANGHAI, ST=SHANGHAI, C=CN
Signature Algorithm: MD5withRSA, OID = 1.2.840.113549.1.1.4

Key: Sun RSA public key, 1024 bits
modulus: 147005897895154579158611362635705015526343567540179810787720962647813476263007438703260428614713959050489969320407032188354154864458173391924174572670760787020376059450959590201769673352310306436317737681841023634858859421706688264107997615904562148395975950346111044420745647553726796261007466030261120823247
public exponent: 65537
Validity: [From: Wed Sep 23 11:05:07 CST 2020,
To: Sat Sep 21 11:05:07 CST 2030]
Issuer: CN=RootCA, OU=SHEPC, O=IOT, L=SHANGHAI, ST=SHANGHAI, C=CN
SerialNumber: [ 5811e900]

]
x509Certificate_SerialNumber_序列號___:1477568768
x509Certificate_getIssuerDN_釋出方標識名___:CN=RootCA, OU=SHEPC, O=IOT, L=SHANGHAI, ST=SHANGHAI, C=CN
x509Certificate_getSubjectDN_主體標識___:CN=RootCA, OU=SHEPC, O=IOT, L=SHANGHAI, ST=SHANGHAI, C=CN
x509Certificate_getSigAlgOID_證照演算法OID字串___:1.2.840.113549.1.1.4
x509Certificate_getNotBefore_證照有效期___:Sat Sep 21 11:05:07 CST 2030
x509Certificate_getSigAlgName_簽名演算法___:MD5withRSA
x509Certificate_getVersion_版本號___:3
x509Certificate_getPublicKey_公鑰___:Sun RSA public key, 1024 bits
modulus: 147005897895154579158611362635705015526343567540179810787720962647813476263007438703260428614713959050489969320407032188354154864458173391924174572670760787020376059450959590201769673352310306436317737681841023634858859421706688264107997615904562148395975950346111044420745647553726796261007466030261120823247
public exponent: 65537

相關文章