#.pfx数字证书制作及操作使用
- 使用工具
- .pfx创建
- 从*.pfx文件提取密钥
- Java代码操作
#工具下载地址
链接:https://pan.baidu.com/s/1IWA2-Gtws36NQm-UCXGqHg 提取码:kwg9 复制这段内容后打开百度网盘手机App,操作更方便哦
#创建.pfx
CN=名称(一般填公司名称)
OU=单位名称
O=作者名称
L=地区
C=国家第一步打开CertManager.exe
第二步
第三步
以上步骤操作完成之后会生成以下文件
#从.pfx文件提取密钥*
方法:
1:通过keytool密钥库导入命令importkeystore,将密钥库格式由 PKCS#12转换为JKS。
2:检索新生成的密钥库文件,提取别名信息。
3:由密钥库文件导出数字证书(这里将用到别名)。
4:通过代码提取公钥/私钥、签名算法等.
CMD代码
keytool -importkeystore -v -srckeystore zcs.pfx -srcstoretype pkcs12 -srcstorepass 12345678 -destkeystore zcs.keystore -deststoretype jks -deststorepass 12345678
######=================================================================
**-importkeystore:**导入密钥库,通过格式设定,我们可以将PKCS#12文件转换为JKS格式。
-v显示详情
**-srckeystore:**源密钥库,这里是zcs.pfx
**-srcstoretype:**源密钥库格式,这里为pkcs12
**-srcstorepass:**源密钥库密码,这里为12345678
**-destkeystore:**目标密钥库,这里为zcs.keystore
**-deststoretype:**目标密钥库格式,这里为jks,默认值也如此
**-deststorepass:**目标密钥库密码,这里为12345678
通过这个操作,我们能够获得所需的密钥库文件zlex.keystore。
在output目录下按住shift键右击鼠标打开命令行
这时,我们已经获得了密钥库文件,只要确定对应的别名信息,就可以提取公钥/私钥,以及数字证书,进行加密交互了!
CMD代码
keytool -list -keystore zcs.keystore -storepass 12345678 -v
**-list:**列举密钥库
**-keystore:**密钥库,这里是zcs.keystore
**-storepass:**密钥库密码,这里是12345678
**-v:**显示详情
现在可以导出证书了
CMD代码
keytool -exportcert -alias pvktmp:4c306f24-9992-446d-a334-82e8be541ca5 -keystore zcs.keystore -file zcs.crt -storepass 12345678
**-exportcert:**导出证书
**-alias:**别名,这里是1
**-keystore:**密钥库,这里是zcs.keystore
**-file:**证书文件,这里是zcs.crt
**-storepass:**密钥库密码,这里是12345678
最后上Java代码:
package com.charsen.test;import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Date;import javax.crypto.Cipher;public class CertificateCoder {/*** Java密钥库(Java Key Store,JKS)KEY_STORE*/public static final String KEY_STORE = "JKS";public static final String X509 = "X.509";/*** 由 KeyStore获得私钥* * @param keyStorePath* @param keyStorePassword* @param alias* @param aliasPassword* @return* @throws Exception*/private static PrivateKey getPrivateKey(String keyStorePath,String keyStorePassword, String alias, String aliasPassword)throws Exception {KeyStore ks = getKeyStore(keyStorePath, keyStorePassword);PrivateKey key = (PrivateKey) ks.getKey(alias,aliasPassword.toCharArray());return key;}/*** 由 Certificate获得公钥* * @param certificatePath* @return* @throws Exception*/private static PublicKey getPublicKey(String certificatePath)throws Exception {Certificate certificate = getCertificate(certificatePath);PublicKey key = certificate.getPublicKey();return key;}/*** 获得Certificate* * @param certificatePath* @return* @throws Exception*/private static Certificate getCertificate(String certificatePath)throws Exception {CertificateFactory certificateFactory = CertificateFactory.getInstance(X509);FileInputStream in = new FileInputStream(certificatePath);Certificate certificate = certificateFactory.generateCertificate(in);in.close();return certificate;}/*** 获得Certificate* * @param keyStorePath* @param keyStorePassword* @param alias* @return* @throws Exception*/private static Certificate getCertificate(String keyStorePath,String keyStorePassword, String alias) throws Exception {KeyStore ks = getKeyStore(keyStorePath, keyStorePassword);Certificate certificate = ks.getCertificate(alias);return certificate;}/*** 获得KeyStore* * @param keyStorePath* @param password* @return* @throws Exception*/private static KeyStore getKeyStore(String keyStorePath, String password)throws Exception {FileInputStream is = new FileInputStream(keyStorePath);KeyStore ks = KeyStore.getInstance(KEY_STORE);ks.load(is, password.toCharArray());is.close();return ks;}/*** 私钥加密* * @param data* @param keyStorePath* @param keyStorePassword* @param alias* @param aliasPassword* @return* @throws Exception*/public static byte[] encryptByPrivateKey(byte[] data, String keyStorePath,String keyStorePassword, String alias, String aliasPassword)throws Exception {// 取得私钥PrivateKey privateKey = getPrivateKey(keyStorePath, keyStorePassword,alias, aliasPassword);// 对数据加密Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());cipher.init(Cipher.ENCRYPT_MODE, privateKey);return cipher.doFinal(data);}/*** 私钥解密* * @param data* @param keyStorePath* @param alias* @param keyStorePassword* @param aliasPassword* @return* @throws Exception*/public static byte[] decryptByPrivateKey(byte[] data, String keyStorePath,String alias, String keyStorePassword, String aliasPassword)throws Exception {// 取得私钥PrivateKey privateKey = getPrivateKey(keyStorePath, keyStorePassword,alias, aliasPassword);// 对数据加密Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());cipher.init(Cipher.DECRYPT_MODE, privateKey);return cipher.doFinal(data);}/*** 公钥加密* * @param data* @param certificatePath* @return* @throws Exception*/public static byte[] encryptByPublicKey(byte[] data, String certificatePath)throws Exception {// 取得公钥PublicKey publicKey = getPublicKey(certificatePath);// 对数据加密Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());cipher.init(Cipher.ENCRYPT_MODE, publicKey);return cipher.doFinal(data);}/*** 公钥解密* * @param data* @param certificatePath* @return* @throws Exception*/public static byte[] decryptByPublicKey(byte[] data, String certificatePath)throws Exception {// 取得公钥PublicKey publicKey = getPublicKey(certificatePath);// 对数据加密Cipher cipher = Cipher.getInstance(publicKey.getAlgorithm());cipher.init(Cipher.DECRYPT_MODE, publicKey);return cipher.doFinal(data);}/*** 验证Certificate* * @param certificatePath* @return*/public static boolean verifyCertificate(String certificatePath) {return verifyCertificate(new Date(), certificatePath);}/*** 验证Certificate是否过期或无效* * @param date* @param certificatePath* @return*/public static boolean verifyCertificate(Date date, String certificatePath) {boolean status = true;try {// 取得证书Certificate certificate = getCertificate(certificatePath);// 验证证书是否过期或无效status = verifyCertificate(date, certificate);} catch (Exception e) {status = false;}return status;}/*** 验证证书是否过期或无效* * @param date* @param certificate* @return*/private static boolean verifyCertificate(Date date, Certificate certificate) {boolean status = true;try {X509Certificate x509Certificate = (X509Certificate) certificate;x509Certificate.checkValidity(date);} catch (Exception e) {status = false;}return status;}/*** 签名* * @param keyStorePath* @param alias* @param keyStorePassword* @param aliasPassword* @return* @throws Exception*/public static byte[] sign(byte[] sign, String keyStorePath, String alias,String keyStorePassword, String aliasPassword) throws Exception {// 获得证书X509Certificate x509Certificate = (X509Certificate) getCertificate(keyStorePath, keyStorePassword, alias);// 取得私钥PrivateKey privateKey = getPrivateKey(keyStorePath, keyStorePassword,alias, aliasPassword);// 构建签名Signature signature = Signature.getInstance(x509Certificate.getSigAlgName());signature.initSign(privateKey);signature.update(sign);return signature.sign();}/*** 验证签名* * @param data* @param sign* @param certificatePath* @return* @throws Exception*/public static boolean verify(byte[] data, byte[] sign,String certificatePath) throws Exception {// 获得证书X509Certificate x509Certificate = (X509Certificate) getCertificate(certificatePath);// 获得公钥PublicKey publicKey = x509Certificate.getPublicKey();// 构建签名Signature signature = Signature.getInstance(x509Certificate.getSigAlgName());signature.initVerify(publicKey);signature.update(data);return signature.verify(sign);}/*** 验证Certificate* * @param keyStorePath* @param keyStorePassword* @param alias* @return*/public static boolean verifyCertificate(Date date, String keyStorePath,String keyStorePassword, String alias) {boolean status = true;try {Certificate certificate = getCertificate(keyStorePath,keyStorePassword, alias);status = verifyCertificate(date, certificate);} catch (Exception e) {status = false;}return status;}/*** 验证Certificate* * @param keyStorePath* @param keyStorePassword* @param alias* @return*/public static boolean verifyCertificate(String keyStorePath,String keyStorePassword, String alias) {return verifyCertificate(new Date(), keyStorePath, keyStorePassword,alias);}
}
测试类
package com.charsen.test;import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;import java.util.Date;import org.apache.commons.codec.binary.Hex;
import org.junit.Test;public class CertificateCoderTest {private String certificatePath = "D:\\MyWorkSpace\\UBSS\\test\\src\\sdk.crt";private String keyStorePath = "D:\\MyWorkSpace\\UBSS\\test\\src\\sdk.keystore";private String keyStorePassword = "12345678";private String aliasPassword = "12345678";private String alias = "pvktmp:4c306f24-9992-446d-a334-82e8be541ca5";@Testpublic void test() throws Exception {System.err.println("公钥加密——私钥解密");String inputStr = "Ceritifcate";byte[] data = inputStr.getBytes();byte[] encrypt = CertificateCoder.encryptByPublicKey(data,certificatePath);byte[] decrypt = CertificateCoder.decryptByPrivateKey(encrypt,keyStorePath, alias, keyStorePassword, aliasPassword);String outputStr = new String(decrypt);System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);// 验证数据一致assertArrayEquals(data, decrypt);// 验证证书有效assertTrue(CertificateCoder.verifyCertificate(certificatePath));}@Testpublic void testSign() throws Exception {System.err.println("私钥加密——公钥解密");String inputStr = "sign";byte[] data = inputStr.getBytes();byte[] encodedData = CertificateCoder.encryptByPrivateKey(data,keyStorePath, keyStorePassword, alias, aliasPassword);byte[] decodedData = CertificateCoder.decryptByPublicKey(encodedData,certificatePath);String outputStr = new String(decodedData);System.err.println("加密前: " + inputStr + "\n\r" + "解密后: " + outputStr);assertEquals(inputStr, outputStr);System.err.println("私钥签名——公钥验证签名");// 产生签名byte[] sign = CertificateCoder.sign(encodedData, keyStorePath, alias,keyStorePassword, aliasPassword);System.err.println("签名:\r" + Hex.encodeHexString(sign));// 验证签名boolean status = CertificateCoder.verify(encodedData, sign,certificatePath);System.err.println("状态:\r" + status);assertTrue(status);}@Testpublic void testVerify() throws Exception {System.err.println("密钥库证书有效期验证");boolean status = CertificateCoder.verifyCertificate(new Date(),keyStorePath, keyStorePassword, alias);System.err.println("证书状态:\r" + status);assertTrue(status);}
}