第五十篇: JAVA加密解密之数字证书

系统之间在进行交互的时候,我们经常会用到数字证书,数字证书可以帮我们验证身份等,下面我们就来看一下在Java中如何使用数字证书。 
我们先使用keytool工具生成密钥库并导出公钥证书。 
第一步:生成keyStroe文件 
执行如下命令:

keytool -genkey -validity 36000 -alias www.jianggujin.com -keyalg RSA -keystore test.keystore

该命令相关参数如下: 
这里写图片描述

输入完后,我们需要按照提示完成后续信息的输入,这里面我们使用的密码为:123456

第二步:导出公钥证书 
生成完密钥库后,我们就可以导出公钥文件了,执行如下命令:

keytool -export -keystore test.keystore -alias www.jianggujin.com -file test.cer -rfc

该命令相关参数如下: 
这里写图片描述

完整操作过程如下: 
这里写图片描述

经过这两步后,我们就有了密钥库和证书文件,和之前的加密解密工具类一样,我们再来编写一个用于操作数字证书的工具类:

package com.jianggujin.codec;

import java.io.FileInputStream;
import java.io.InputStream;
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;

/**
 * 数字证书
 * 
 * @author jianggujin
 *
 */
public class HQCertificate
{
   
   private static HQCertificate certificate = new HQCertificate();

   public static HQCertificate getInstance()
   {
      return certificate;
   }

   private HQCertificate()
   {
   }

   /**
    * 密钥库
    * 
    * @author jianggujin
    *
    */
   public static enum HQKeyStore
   {

      JCEKS("jceks"), JKS("jks"), DKS("dks"), PKCS11("pkcs11"), PKCS12("pkcs12");
      private String name;

      private HQKeyStore(String name)
      {
         this.name = name;
      }

      public String getName()
      {
         return this.name;
      }
   }

   /**
    * Java密钥库(Java Key Store,JKS)KEY_STORE
    */
   // public final String KEY_STORE = "JKS";

   public final String X509 = "X.509";

   /**
    * 由KeyStore获得私钥
    * 
    * @param keyStorePath
    * @param alias
    * @param password
    * @return
    * @throws Exception
    */
   private PrivateKey getPrivateKey(String keyStorePath, String alias, char[] password, HQKeyStore keyStore)
         throws Exception
   {
      KeyStore ks = getKeyStore(keyStorePath, password, keyStore);
      PrivateKey key = (PrivateKey) ks.getKey(alias, password);
      return key;
   }

   /**
    * 由Certificate获得公钥
    * 
    * @param certificatePath
    * @return
    * @throws Exception
    */
   private PublicKey getPublicKey(String certificatePath) throws Exception
   {
      Certificate certificate = getCertificate(certificatePath);
      PublicKey key = certificate.getPublicKey();
      return key;
   }

   /**
    * 获得Certificate
    * 
    * @param certificatePath
    * @return
    * @throws Exception
    */
   private 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 alias
    * @param password
    * @return
    * @throws Exception
    */
   private Certificate getCertificate(String keyStorePath, String alias, char[] password, HQKeyStore keyStore)
         throws Exception
   {
      KeyStore ks = getKeyStore(keyStorePath, password, keyStore);
      return getCertificate(ks, alias);
   }

   private Certificate getCertificate(KeyStore keyStore, String alias) throws Exception
   {
      Certificate certificate = keyStore.getCertificate(alias);
      return certificate;
   }

   /**
    * 获得KeyStore
    * 
    * @param keyStorePath
    * @param password
    * @return
    * @throws Exception
    */
   public KeyStore getKeyStore(String keyStorePath, char[] password, HQKeyStore keyStore) throws Exception
   {
      KeyStore store = null;
      FileInputStream is = new FileInputStream(keyStorePath);
      store = getKeyStore(is, password, keyStore);
      is.close();
      return store;
   }

   public KeyStore getKeyStore(InputStream in, char[] password, HQKeyStore keyStore) throws Exception
   {
      KeyStore ks = KeyStore.getInstance(keyStore.getName());
      ks.load(in, password);
      return ks;
   }

   /**
    * 私钥加密
    * 
    * @param data
    * @param keyStorePath
    * @param alias
    * @param password
    * @return
    * @throws Exception
    */
   public byte[] encrypt(byte[] data, String keyStorePath, String alias, char[] password, HQKeyStore keyStore)
         throws Exception
   {
      // 取得私钥
      PrivateKey privateKey = getPrivateKey(keyStorePath, alias, password, keyStore);

      return encrypt(data, privateKey);

   }

   public byte[] encrypt(byte[] data, PrivateKey privateKey) throws Exception
   {
      // 对数据加密
      Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm());
      cipher.init(Cipher.ENCRYPT_MODE, privateKey);
      return cipher.doFinal(data);
   }

   /**
    * 公钥加密
    * 
    * @param data
    * @param certificatePath
    * @return
    * @throws Exception
    */
   public byte[] encrypt<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值