Jay
2024-11-01 03e8aca3ad6201c0d74e00d4c8d7367cdaaa54f9
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.channel.opcua.collector;
H 2
3 import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
4
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.FileOutputStream;
8 import java.security.Key;
9 import java.security.KeyStore;
10 import java.security.PublicKey;
11 import java.security.cert.Certificate;
12 import java.security.cert.CertificateException;
13 import java.security.cert.CertificateFactory;
14
15 /**
16  * @Author: zhangsong
17  * @Date: 2019/5/28 20:21
18  * @Version 1.0
19  * 首先需要通过jdk的keytool生成秘钥库等,需确保机器中安装有jdk
20  * 0.在本代码环境下,执行1命令时,秘钥库的密码和证书的密码须一致,请自行修改路径
21  *   请确保路径下无名为mykeystore.keystore的文件,否则可能会报密码错等
22  * 1.keytool -genkey -alias mykey -keyalg RSA -keystore C:/Users/Lenovo/AppData/Local/Temp/security/mykeystore.keystore -keysize 1024 -validity 3650
23    2.keytool -export -alias mykey -keystore C:/Users/Lenovo/AppData/Local/Temp/security/mykeystore.keystore -file  C:/Users/Lenovo/AppData/Local/Temp/security/mykey.cer
24    3.代码中的秘钥库密码为keystore,请自行修改
25    4.查看信息  keytool -list -v -keystore C:/Users/Lenovo/AppData/Local/Temp/security/mykeystore.keystore -storepass "keystore"
26  *
27  */
28 public class RSACertHelper {
29
30     public static void main(String[] args) {
31         String path = "C:/Users/Lenovo/AppData/Local/Temp/security/";
32         String keyStoreFile = "mykeystore.keystore";
33         String passwd = "keystore";
34         String keyAlias = "mykey";
35         String pfxFile = "mykey.pfx";
36         String cerFile = "mykey.cer";
37
38         System.out.println("请确保已执行完注释中的两条命令再执行本代码\n\n");
39         System.out.println("1.开始生成PFX文件");
40         coverToPfx(path + keyStoreFile, passwd, keyAlias, path + pfxFile);
41         System.out.println("===============================================================");
42         System.out.println("2.开始提取.cer中的公钥字符串");
43         String cerStr = getPubStr(path + cerFile);
44         System.out.println("从.cer文件中提取的公钥字符串如下:");
45         System.out.println(cerStr);
46     }
47
48     //1生成pfx文件
49     public static void coverToPfx(String keyStoreFile, String passwd, String keyAlias, String pfxFile) {
50         try {
51             KeyStore inputKeyStore = KeyStore.getInstance("JKS");
52             FileInputStream fis = new FileInputStream(keyStoreFile);
53             char[] nPassword = null;
54             if ((passwd == null)
55                     || passwd.trim().equals("")) {
56                 nPassword = null;
57             } else {
58                 nPassword = passwd.toCharArray();
59             }
60             inputKeyStore.load(fis, nPassword);
61             fis.close();
62             KeyStore outputKeyStore = KeyStore.getInstance("PKCS12");
63             outputKeyStore.load(null, passwd.toCharArray());
64
65             if (inputKeyStore.isKeyEntry(keyAlias)) {
66                 Key key = inputKeyStore.getKey(keyAlias, passwd.toCharArray());
67                 Certificate[] certChain = inputKeyStore
68                         .getCertificateChain(keyAlias);
69                 outputKeyStore.setKeyEntry(keyAlias, key, passwd
70                         .toCharArray(), certChain);
71             }
72
73             FileOutputStream out = new FileOutputStream(pfxFile);
74             outputKeyStore.store(out, nPassword);
75             out.close();
76             System.out.println("已生成PFX文件" + pfxFile);
77         } catch (Exception e) {
78             e.printStackTrace();
79         }
80     }
81
82     //2获取公钥字符串
83     public static String getPubStr(String cerFile) {
84         String key = "";
85         // 读取证书文件
86         try {
87             CertificateFactory cf = CertificateFactory.getInstance("X.509");
88             FileInputStream in = new FileInputStream(cerFile);
89
90             //生成一个证书对象并使用从输入流 inStream 中读取的数据对它进行初始化。
91             Certificate c = cf.generateCertificate(in);
92             PublicKey publicKey = c.getPublicKey();
93             key = Base64.encode(publicKey.getEncoded());
94         } catch (CertificateException e) {
95             e.printStackTrace();
96         } catch (FileNotFoundException e) {
97             e.printStackTrace();
98         }
99         return key;
100     }
101 }