zbb378@sohu.com
2024-11-04 b2bad51be3c8d3e78d7f81a19415faeac2d0297c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.nova.sankuai.domain.api.yinsheng;
 
import cfca.sadk.algorithm.sm2.SM2PublicKey;
import cfca.sadk.org.bouncycastle.jce.provider.BouncyCastleProvider;
import cfca.sadk.x509.certificate.X509Cert;
import sun.misc.BASE64Decoder;
 
import java.io.FileInputStream;
import java.io.Serializable;
import java.security.PublicKey;
import java.security.Security;
 
public class GMCertInfo implements Serializable
{
    private static final long serialVersionUID = 1L;
    private SM2PublicKey pubKeySM2;
    private PublicKey  pubKey;
    private byte[] priKey;
    private X509Cert x509Cert;
 
    //读取原始公钥
    public static byte[]  ReadPublicKeyFromRaw(String publickeyFile) throws Exception
    {
        FileInputStream br = new FileInputStream(publickeyFile);
        byte[] b1 = new byte[2000];
        int k = br.read(b1);
        byte[] b = new byte[k];
        System.arraycopy(b1, 0, b, 0, k);
        return b;
    }
    
    // 从X509证书文件读取公钥,Certificate只含有公钥
    // .crt .cer文件都可以读取 .cer是IE导出的公钥证书(der格式)
    // -----BEGIN CERTIFICATE-----开始 文件头不许有其它内容
    // -----END CERTIFICATE-----
    // cer公钥证书 二进制
    public void ReadPublicKeyFromX509CertificateSM2(String cerFilePath) throws Exception
    {
        Security.addProvider(new BouncyCastleProvider());
        byte[] cer =  ReadPublicKeyFromRaw(cerFilePath);
        ReadPublicKeyFromX509CertificateSM2(cer);
    }
 
    public void ReadPublicKeyFromX509CertificateSM2Str(String pubKey) throws Exception
    {
        Security.addProvider(new BouncyCastleProvider());
        ReadPublicKeyFromX509CertificateSM2(new BASE64Decoder().decodeBuffer(pubKey));
    }
 
 
    public void ReadPublicKeyFromX509CertificateSM2(byte[] cer) throws Exception
    {
        Security.addProvider(new BouncyCastleProvider());
        x509Cert = new X509Cert(cer);
        //获取公钥对象
        pubKeySM2 = (SM2PublicKey) x509Cert.getPublicKey();
        pubKey = x509Cert.getPublicKey();
    }
 
    public SM2PublicKey getPubKeySM2() {
        return pubKeySM2;
    }
 
    public void setPubKeySM2(SM2PublicKey pubKeySM2) {
        this.pubKeySM2 = pubKeySM2;
    }
 
    public PublicKey getPubKey() {
        return pubKey;
    }
 
    public void setPubKey(PublicKey pubKey) {
        this.pubKey = pubKey;
    }
 
    public byte[] getPriKey() {
        return priKey;
    }
 
    public void setPriKey(byte[] priKey) {
        this.priKey = priKey;
    }
 
    public X509Cert getX509Cert() {
        return x509Cert;
    }
 
    public void setX509Cert(X509Cert x509Cert) {
        this.x509Cert = x509Cert;
    }
 
   
    
    
}