pfx证书获取私钥


public class KeyUtil {
    public KeyUtil() {
    }

    public static PrivateKey getPrivateKeyFromPFX(InputStream inputStream, String pfxFilePwd) throws PKIException {
        PKCS12 pkcs12 = new PKCS12();
        pkcs12.load(inputStream);
        pkcs12.decrypt(pfxFilePwd.toCharArray());
        return pkcs12.getPrivateKey();
    }

    public static PrivateKey getPrivateKeyFromPFX(String pfxFilePath, String pfxFilePwd) throws PKIException {
        PKCS12 pkcs12 = new PKCS12();
        pkcs12.load(pfxFilePath);
        pkcs12.decrypt(pfxFilePwd.toCharArray());
        return pkcs12.getPrivateKey();
    }

    public static PrivateKey getPrivateKeyFromPFX(byte[] pfxFileData, String pfxFilePwd) throws PKIException {
        PKCS12 pkcs12 = new PKCS12();
        pkcs12.load(pfxFileData);
        pkcs12.decrypt(pfxFilePwd.toCharArray());
        return pkcs12.getPrivateKey();
    }

    public static SM2PrivateKey getPrivateKeyFromSM2(String sm2FilePath, String sm2FilePwd) throws PKIException {
        ASN1InputStream ais = null;
        FileInputStream fis = null;

        SM2PrivateKey var8;
        try {
            fis = new FileInputStream(sm2FilePath);
            byte[] data = new byte[fis.available()];
            fis.read(data);
            fis.close();
            boolean isB64 = ASN1Parser.isBase64Compatability(data);
            if (isB64) {
                data = Base64.decode(data);
            }

            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            ais = new ASN1InputStream(bis);
            PKCS12_SM2 sm2Pfx = PKCS12_SM2.getInstance(ais.readObject());
            var8 = sm2Pfx.getPrivateKey(sm2FilePwd);
        } catch (Exception var20) {
            throw new PKIException(var20.getMessage());
        } finally {
            try {
                fis.close();
            } catch (IOException var19) {
                var19.printStackTrace();
            }

            if (ais != null) {
                try {
                    ais.close();
                } catch (IOException var18) {
                    throw new PKIException(var18.getMessage());
                }
            }

        }

        return var8;
    }

    public static SM2PrivateKey getPrivateKeyFromSM2(InputStream inputStream, String sm2FilePwd) throws PKIException {
        ASN1InputStream ais = null;

        SM2PrivateKey var7;
        try {
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);
            boolean isB64 = ASN1Parser.isBase64Compatability(data);
            if (isB64) {
                data = Base64.decode(data);
            }

            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            ais = new ASN1InputStream(bis);
            PKCS12_SM2 object = PKCS12_SM2.getInstance(ais.readObject());
            var7 = object.getPrivateKey(sm2FilePwd);
        } catch (Exception var19) {
            throw new PKIException(var19.getMessage());
        } finally {
            try {
                inputStream.close();
            } catch (IOException var18) {
                var18.printStackTrace();
            }

            if (ais != null) {
                try {
                    ais.close();
                } catch (IOException var17) {
                    throw new PKIException(var17.getMessage());
                }
            }

        }

        return var7;
    }

    public static SM2PrivateKey getPrivateKeyFromSM2(byte[] sm2FileData, String sm2FilePwd) throws PKIException {
        ASN1InputStream ais = null;

        SM2PrivateKey var6;
        try {
            boolean isB64 = ASN1Parser.isBase64Compatability(sm2FileData);
            if (isB64) {
                sm2FileData = Base64.decode(sm2FileData);
            }

            ByteArrayInputStream bis = new ByteArrayInputStream(sm2FileData);
            ais = new ASN1InputStream(bis);
            PKCS12_SM2 object = PKCS12_SM2.getInstance(ais.readObject());
            var6 = object.getPrivateKey(sm2FilePwd);
        } catch (Exception var15) {
            throw new PKIException(var15.getMessage());
        } finally {
            if (ais != null) {
                try {
                    ais.close();
                } catch (IOException var14) {
                    throw new PKIException(var14.getMessage());
                }
            }

        }

        return var6;
    }

    public static PrivateKey getPrivateKeyFromJKS(String jksFilePath, String jksFilePwd, String alias) throws PKIException {
        try {
            KeyStore ks = InitKeyStore.initJKSKeyStore(jksFilePath, jksFilePwd);
            char[] password = jksFilePwd.toCharArray();
            return (PrivateKey)ks.getKey(alias, password);
        } catch (Exception var5) {
            throw new PKIException(var5.getMessage());
        }
    }

    public static PrivateKey getPrivateKeyFromJKS(InputStream is, String jksFilePwd, String alias) throws PKIException {
        try {
            KeyStore ks = InitKeyStore.initJKSKeyStore(is, jksFilePwd);
            char[] password = jksFilePwd.toCharArray();
            return (PrivateKey)ks.getKey(alias, password);
        } catch (Exception var5) {
            throw new PKIException(var5.getMessage());
        }
    }

    public static KeyPair generateKeyPair(Mechanism mechanism, int keyLength, Session session) throws PKIException {
        if ("SM2".equals(mechanism.getMechanismType()) && 256 != keyLength) {
            throw new PKIException("key length must be 256 bits for SM2");
        } else {
            return session.generateKeyPair(mechanism, keyLength);
        }
    }

    public static Key generateKey(Mechanism keyType, Session session) throws PKIException {
        return session.generateKey(keyType);
    }

    public static Key generateKey(Mechanism keyType, byte[] keyData, Session session) throws PKIException {
        return session.generateKey(keyType, keyData);
    }

    public static SM2PublicKey getSM2PublicKey(byte[] pubX, byte[] pubY) {
        return new SM2PublicKey(pubX, pubY);
    }

    public static SM2PrivateKey getSM2PrivateKey(byte[] da, byte[] pubX, byte[] pubY) {
        return new SM2PrivateKey(da, pubX, pubY);
    }
}

 

你可能感兴趣的:(智能模块)