Java:P12加解密方法封装

public class ReadP12Cert {
    /**
     * 加密方法
     *
     * @param signKeyBytes
     * @return
     */
    byte[] encryBody(byte[] signKeyBytes) {
        try {
            KeyStore ks = KeyStore.getInstance("PKCS12");
            FileInputStream fis = new FileInputStream(FileUtil.getResourcePath("cert/SPDB/spdbdtest.p12"));
            char[] nPassword = null;
            nPassword = "inFlXX".toCharArray();
            ks.load(fis, nPassword);
            fis.close();
            Enumeration enuml = ks.aliases();
            String keyAlias = null;
            if (enuml.hasMoreElements()) {
                keyAlias = (String) enuml.nextElement();
            }
            Certificate cert = ks.getCertificate(keyAlias);
            PublicKey pubkey = cert.getPublicKey();
            Cipher c1 = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // 定义算法:RSA
            c1.init(Cipher.ENCRYPT_MODE, pubkey);
            return c1.doFinal(signKeyBytes);
        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage(), ex);
        }
    }

    private String decryBody(String keyValue) {
        try {
            byte[] signKeyBytes = Base64.decodeBase64(keyValue.getBytes("UTF-8"));
            PrivateKey pri = CipherUtil.getPrivateKey(FileUtil.getResourcePath("cert/SPDB/spdbdtest.p12"), "inFlXX",
                    "PKCS12");
            Cipher cipher = Cipher.getInstance(pri.getAlgorithm());
            cipher.init(Cipher.DECRYPT_MODE, pri);
            signKeyBytes = cipher.doFinal(signKeyBytes);
            signKeyBytes = Base64.encodeBase64(signKeyBytes);
            return new String(signKeyBytes, "UTF-8");
        } catch (Exception ex) {
            throw new RuntimeException(ex.getMessage(), ex);
        }
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        try {
            //处理待加密信息,将其把String转换为 byte[]
            //byte[] msg = "OX5genLNhGtBV5myT0mLHA==".getBytes();
            byte[] signKeyBytes = Base64.decodeBase64("OX5genLNhGtBV5".getBytes("UTF-8"));
            System.out.println("signKeyBytes=" + signKeyBytes);
            System.out.println("======================================");
            ReadP12Cert readP12Cert = new ReadP12Cert();
            //加密处理(byte[])
            System.out.println("将key加密处理:");
            System.out.println(readP12Cert.encryBody(signKeyBytes));
            //将byte[]转换为String
            System.out.println("把byte[]转换为String:");
            byte[] transKey = Base64.encodeBase64(readP12Cert.encryBody(signKeyBytes));
            String res = new String(transKey);
            System.out.println(res);
            System.out.println("解密后的报文:");
            System.out.println(readP12Cert.decryBody(res));


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
/**
     * 私钥解密
     * @param keyPath
     * @param keyPawd
     * @param keyType
     * @return
     * @throws UnrecoverableKeyException
     * @throws NoSuchAlgorithmException
     * @throws KeyStoreException
     */
    public static PrivateKey getPrivateKey(String keyPath, String keyPawd, String keyType)
            throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException {
        KeyStore keyStore = getKeyStore(keyPath, keyPawd, keyType);

        Enumeration enum2 = keyStore.aliases();
        String keyAlias = null;
        if (enum2 == null) {
            return null;
        } else {
            if (enum2.hasMoreElements()) {
                keyAlias = (String) enum2.nextElement();
            } else {
                return null;
            }
        }
        return (PrivateKey) keyStore.getKey(keyAlias, keyPawd.toCharArray());
    }

你可能感兴趣的:(java)