Java tutorial
/* * Copyright 2013-2023 "Peng Li"<aqnote@qq.com> * Licensed under the AQNote License, Version 1.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.aqnote.com/licenses/LICENSE-1.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.aqnote.shared.cryptology.cert.util; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; /** * KeyStoreFileTool.java???? * * @author "Peng Li"<aqnote@qq.com> Nov 18, 2013 12:01:35 PM */ public class KeyStoreFileUtil { public static void writePkcsFile(String b64P12, String p12fileName) throws IOException { if (StringUtils.isBlank(p12fileName) || StringUtils.isBlank(b64P12)) { return; } byte[] p12File = Base64.decodeBase64(b64P12); FileOutputStream fos = new FileOutputStream(p12fileName); fos.write(p12File); fos.flush(); fos.close(); } /** * ?KeyStore??? * * @param alias * @param pfxPath * @param password * @return * @throws UnrecoverableKeyException * @throws KeyStoreException * @throws NoSuchAlgorithmException * @throws CertificateException * @throws IOException */ public static String readPrivateKeyStr(String alias, String pfxPath, String password) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { PrivateKey privateKey = readPrivateKey(alias, pfxPath, password); return privateKey.toString().replace(" ", ""); } /** * ?KeyStore?N * * @param alias * @param pfxPath * @param password * @return */ public static String getModulusByPfx(String alias, String pfxPath, String password) { String pfx = ""; try { pfx = readPrivateKeyStr(alias, pfxPath, password); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String modulus = pfx.substring(pfx.indexOf("modulus:") + "modulus:".length(), pfx.indexOf("publicexponent:")); return modulus.trim().replace(" ", ""); } /** * ?KeyStore?e * * @param alias * @param pfxPath * @param password * @return */ public static String getPubExponentByPfx(String alias, String pfxPath, String password) { String pfx = ""; try { pfx = readPrivateKeyStr(alias, pfxPath, password); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String modulus = pfx.substring(pfx.indexOf("publicexponent:") + "publicexponent:".length(), pfx.indexOf("privateexponent:")); return modulus.trim().replace(" ", ""); } /** * ?KeyStore??d * * @param alias * @param pfxPath * @param password * @return */ public static String getPriExponentByPfx(String alias, String pfxPath, String password) { String pfx = ""; try { pfx = readPrivateKeyStr(alias, pfxPath, password); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String modulus = pfx.substring(pfx.indexOf("privateexponent:") + "privateexponent:".length(), pfx.indexOf("primep:")); return modulus.trim().replace(" ", ""); } /** * ?KeyStore?p * * @param alias * @param pfxPath * @param password * @return */ public static String getpByPfx(String alias, String pfxPath, String password) { String pfx = ""; try { pfx = readPrivateKeyStr(alias, pfxPath, password); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String modulus = pfx.substring(pfx.indexOf("primep:") + "primep:".length(), pfx.indexOf("primeq:")); return modulus.trim().replace(" ", ""); } /** * ?KeyStore?q * * @param alias * @param pfxPath * @param password * @return */ public static String getqByPfx(String alias, String pfxPath, String password) { String pfx = ""; try { pfx = readPrivateKeyStr(alias, pfxPath, password); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String modulus = pfx.substring(pfx.indexOf("primeq:") + "primeq:".length(), pfx.indexOf("primeexponentp:")); return modulus.trim().replace(" ", ""); } /** * ?KeyStore?dp * * @param alias * @param pfxPath * @param password * @return */ public static String getdpByPfx(String alias, String pfxPath, String password) { String pfx = ""; try { pfx = readPrivateKeyStr(alias, pfxPath, password); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String modulus = pfx.substring(pfx.indexOf("primeexponentp:") + "primeexponentp:".length(), pfx.indexOf("primeexponentq:")); return modulus.trim().replace(" ", ""); } /** * ?KeyStore?dq * * @param alias * @param pfxPath * @param password * @return */ public static String getdqByPfx(String alias, String pfxPath, String password) { String pfx = ""; try { pfx = readPrivateKeyStr(alias, pfxPath, password); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String modulus = pfx.substring(pfx.indexOf("primeexponentq:") + "primeexponentq:".length(), pfx.indexOf("crtcoefficient:")); return modulus.trim().replace(" ", ""); } /** * ?KeyStore?qInv * * @param alias * @param pfxPath * @param password * @return */ public static String getqInvByPfx(String alias, String pfxPath, String password) { String pfx = ""; try { pfx = readPrivateKeyStr(alias, pfxPath, password); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String modulus = pfx.substring(pfx.indexOf("crtcoefficient:") + "crtcoefficient:".length(), pfx.length()); return modulus.trim().replace(" ", ""); } /** * ?PFX? * * @param alias ?? * @param pfxPath PFX * @param password ? * @return * @throws KeyStoreException * @throws NoSuchAlgorithmException * @throws CertificateException * @throws IOException * @throws UnrecoverableKeyException */ public static PrivateKey readPrivateKey(String alias, String pfxPath, String password) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException { KeyStore keyStore = KeyStore.getInstance("pkcs12"); FileInputStream fis = null; fis = new FileInputStream(pfxPath); keyStore.load(fis, password.toCharArray()); fis.close(); return (PrivateKey) keyStore.getKey(alias, password.toCharArray()); } }