List of usage examples for java.security KeyFactory generatePrivate
public final PrivateKey generatePrivate(KeySpec keySpec) throws InvalidKeySpecException
From source file:io.kubernetes.client.util.SSLUtils.java
public static KeyStore createKeyStore(InputStream certInputStream, InputStream keyInputStream, String clientKeyAlgo, char[] clientKeyPassphrase, String keyStoreFile, char[] keyStorePassphrase) throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException, KeyStoreException {// w w w.j a v a 2 s . c om CertificateFactory certFactory = CertificateFactory.getInstance("X509"); X509Certificate cert = (X509Certificate) certFactory.generateCertificate(certInputStream); byte[] keyBytes = decodePem(keyInputStream); PrivateKey privateKey; KeyFactory keyFactory = KeyFactory.getInstance(clientKeyAlgo); try { // First let's try PKCS8 privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyBytes)); } catch (InvalidKeySpecException e) { // Otherwise try PKCS8 RSAPrivateCrtKeySpec keySpec = decodePKCS1(keyBytes); privateKey = keyFactory.generatePrivate(keySpec); } KeyStore keyStore = KeyStore.getInstance("JKS"); if (keyStoreFile != null && keyStoreFile.length() > 0) { keyStore.load(new FileInputStream(keyStoreFile), keyStorePassphrase); } else { loadDefaultKeyStoreFile(keyStore, keyStorePassphrase); } String alias = cert.getSubjectX500Principal().getName(); keyStore.setKeyEntry(alias, privateKey, clientKeyPassphrase, new Certificate[] { cert }); return keyStore; }
From source file:com.cedarsoft.crypt.X509Support.java
/** * Reads a private key form a url//from w w w . j a v a 2s. c o m * * @param privateKeyUrl the url containing the private key * @return the read private key * * @throws IOException if any. * @throws GeneralSecurityException * if any. */ @Nullable public static RSAPrivateKey readPrivateKey(@Nullable URL privateKeyUrl) throws IOException, GeneralSecurityException { //If a null url is given - just return null if (privateKeyUrl == null) { return null; } //We have an url --> return it DataInputStream in = new DataInputStream(privateKeyUrl.openStream()); try { byte[] keyBytes = IOUtils.toByteArray(in); KeyFactory keyFactory = KeyFactory.getInstance(RSA); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(keyBytes); return (RSAPrivateKey) keyFactory.generatePrivate(privSpec); } finally { in.close(); } }
From source file:nextflow.k8s.client.SSLUtils.java
public static KeyStore createKeyStore(InputStream certInputStream, InputStream keyInputStream, String clientKeyAlgo, char[] clientKeyPassphrase, String keyStoreFile, char[] keyStorePassphrase) throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException, KeyStoreException {//from ww w .ja va2s. c o m CertificateFactory certFactory = CertificateFactory.getInstance("X509"); X509Certificate cert = (X509Certificate) certFactory.generateCertificate(certInputStream); byte[] keyBytes = decodePem(keyInputStream); PrivateKey privateKey; KeyFactory keyFactory = KeyFactory.getInstance(clientKeyAlgo); try { // First let's try PKCS8 privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyBytes)); } catch (InvalidKeySpecException e) { // Otherwise try PKCS1 RSAPrivateCrtKeySpec keySpec = decodePKCS1(keyBytes); privateKey = keyFactory.generatePrivate(keySpec); } KeyStore keyStore = KeyStore.getInstance("JKS"); if (keyStoreFile != null && keyStoreFile.length() > 0) { keyStore.load(new FileInputStream(keyStoreFile), keyStorePassphrase); } else { loadDefaultKeyStoreFile(keyStore, keyStorePassphrase); } String alias = cert.getSubjectX500Principal().getName(); keyStore.setKeyEntry(alias, privateKey, clientKeyPassphrase, new Certificate[] { cert }); return keyStore; }
From source file:org.bankinterface.util.KeyStoreUtil.java
public static void importPKCS8CertChain(KeyStore ks, String alias, byte[] keyBytes, String keyPass, byte[] certChain) throws InvalidKeySpecException, NoSuchAlgorithmException, CertificateException, KeyStoreException { // load the private key KeyFactory kf = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(keyBytes); PrivateKey pk = kf.generatePrivate(keysp); // load the cert chain CertificateFactory cf = CertificateFactory.getInstance("X.509"); ByteArrayInputStream bais = new ByteArrayInputStream(certChain); Collection<? extends Certificate> certCol = cf.generateCertificates(bais); Certificate[] certs = new Certificate[certCol.toArray().length]; if (certCol.size() == 1) { logger.info("Single certificate; no chain"); bais = new ByteArrayInputStream(certChain); Certificate cert = cf.generateCertificate(bais); certs[0] = cert;//from w w w . jav a2s. c o m } else { logger.info("Certificate chain length : " + certCol.size()); certs = certCol.toArray(new Certificate[certCol.size()]); } ks.setKeyEntry(alias, pk, keyPass.toCharArray(), certs); }
From source file:edu.internet2.middleware.openid.message.encoding.EncodingUtils.java
/** * Decode a DH private key.//from ww w .j a v a 2 s .c om * * @param encodedKey private key to decode * @param parameters DH parameters used in decoding * @return decoded private key * @throws NoSuchAlgorithmException if DH algorithm is unavailable * @throws InvalidKeySpecException if unable to build a valid DH key spec */ public static DHPrivateKey decodePrivateKey(String encodedKey, DHParameterSpec parameters) throws NoSuchAlgorithmException, InvalidKeySpecException { byte[] keyBytes = Base64.decodeBase64(encodedKey.getBytes()); DHPrivateKeySpec keySpec = new DHPrivateKeySpec(new BigInteger(keyBytes), parameters.getP(), parameters.getG()); KeyFactory keyFactory = KeyFactory.getInstance("DH"); return (DHPrivateKey) keyFactory.generatePrivate(keySpec); }
From source file:cn.mrdear.pay.util.RSAUtils.java
/** * ??/*from w w w.j a v a2s .c o m*/ * * @param encodedKey * ? * @return ? */ public static PrivateKey generatePrivateKey(byte[] encodedKey) { try { KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM, PROVIDER); return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvalidKeySpecException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:license.TestWakeLicense.java
/** * ??// w w w .j a v a 2 s . com * @return * @throws Exception */ private static PrivateKey readPrivateKeyFromFile() throws Exception { //?? ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream( new FileInputStream(new File("E:\\workspace\\TestProject\\src\\license\\private.key")))); try { BigInteger m = (BigInteger) oin.readObject(); BigInteger e = (BigInteger) oin.readObject(); RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(m, e); KeyFactory fact = KeyFactory.getInstance("RSA"); return fact.generatePrivate(keySpec); } finally { // oin.close(); } }
From source file:org.artifactory.security.crypto.CryptoHelper.java
static KeyPair createKeyPair(byte[] encodedPrivateKey, byte[] encodedPublicKey) { try {//from w ww. j a va 2 s . co m EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); KeyFactory generator = KeyFactory.getInstance(ASYM_ALGORITHM); PrivateKey privateKey = generator.generatePrivate(privateKeySpec); EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey); PublicKey publicKey = generator.generatePublic(publicKeySpec); return new KeyPair(publicKey, privateKey); } catch (Exception e) { throw new IllegalArgumentException("Failed to create KeyPair from provided encoded keys", e); } }
From source file:org.apache.cloudstack.utils.auth.SAMLUtils.java
public static PrivateKey loadPrivateKey(String privateKey) { byte[] sigBytes = org.bouncycastle.util.encoders.Base64.decode(privateKey); PKCS8EncodedKeySpec pkscs8KeySpec = new PKCS8EncodedKeySpec(sigBytes); KeyFactory keyFact = SAMLUtils.getKeyFactory(); if (keyFact == null) return null; try {//from w w w.ja v a2s.com return keyFact.generatePrivate(pkscs8KeySpec); } catch (InvalidKeySpecException e) { s_logger.error("Unable to create PrivateKey from privateKey string:" + e.getMessage()); } return null; }
From source file:br.edu.ufcg.lsd.commune.network.signature.Util.java
public static PrivateKey decodePrivateKey(String privKeyStr) throws InvalidKeySpecException { byte[] binaryArray = decodeStringOnBase64(privKeyStr); KeyFactory keyFactory; try {//from ww w. jav a 2 s . c o m keyFactory = KeyFactory.getInstance(SignatureConstants.KEY_GEN_ALGORITHM); } catch (NoSuchAlgorithmException e) { //We're assuming that we are always instantiating a valid algorithm throw new CommuneRuntimeException(e); } EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(binaryArray); return keyFactory.generatePrivate(privateKeySpec); }