List of usage examples for java.security KeyFactory generatePrivate
public final PrivateKey generatePrivate(KeySpec keySpec) throws InvalidKeySpecException
From source file:com.alliander.osgp.shared.security.CertificateHelper.java
/** * Create private key from private key file on disk * * @param keyPath//from ww w. j a v a 2 s. c o m * path to key * @param keyType * type of key * @return instance of public key * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws IOException * @throws NoSuchProviderException */ public static PrivateKey createPrivateKey(final String keyPath, final String keyType, final String provider) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, NoSuchProviderException { final byte[] key = readKeyFromDisk(keyPath); final PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(key); KeyFactory privateKeyFactory; privateKeyFactory = KeyFactory.getInstance(keyType, provider); return privateKeyFactory.generatePrivate(privateKeySpec); }
From source file:com.alliander.osgp.shared.security.CertificateHelper.java
public static PrivateKey createPrivateKeyFromBase64(final String keyBase64, final String keyType, final String provider) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, NoSuchProviderException { final byte[] key = Base64.decodeBase64(keyBase64); final PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(key); KeyFactory privateKeyFactory; privateKeyFactory = KeyFactory.getInstance(keyType, provider); return privateKeyFactory.generatePrivate(privateKeySpec); }
From source file:hh.learnj.test.license.test.rsa.RSATest.java
/** * ??/*from ww w .ja v a 2 s . co m*/ * * @return * @throws Exception */ static PrivateKey getPrivateKey() throws Exception { PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decodeBase64(privateKey)); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM_RSA); return keyFactory.generatePrivate(privateKeySpec); }
From source file:com.aqnote.shared.cryptology.cert.tool.PrivateKeyTool.java
public static PrivateKey coverString2PrivateKey(String base64PrivateKey) throws CertException { try {//w ww . j av a 2 s .c om byte[] priEncoded = getKeyEncoded(base64PrivateKey); KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALG); EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(priEncoded); return keyFactory.generatePrivate(privateKeySpec); } catch (NoSuchAlgorithmException e) { throw new CertException(e); } catch (InvalidKeySpecException e) { throw new CertException(e); } }
From source file:com.oneis.common.utils.SSLCertificates.java
private static PrivateKey readPEMPrivateKey(String filename) throws java.io.IOException, java.security.GeneralSecurityException { ByteArrayInputStream bIn = readPEM(filename); ASN1InputStream aIn = new ASN1InputStream(bIn); ASN1Sequence seq = (ASN1Sequence) aIn.readObject(); if (!(seq.getObjectAt(1) instanceof DERInteger)) { throw new RuntimeException("Can't read RSA private key from " + filename + " - if file starts '-----BEGIN PRIVATE KEY-----' then it needs converting to RSA format with 'openssl rsa -in server-in.key -out server.key'."); }/*from ww w .jav a 2s. c om*/ DERInteger mod = (DERInteger) seq.getObjectAt(1); DERInteger pubExp = (DERInteger) seq.getObjectAt(2); DERInteger privExp = (DERInteger) seq.getObjectAt(3); DERInteger p1 = (DERInteger) seq.getObjectAt(4); DERInteger p2 = (DERInteger) seq.getObjectAt(5); DERInteger exp1 = (DERInteger) seq.getObjectAt(6); DERInteger exp2 = (DERInteger) seq.getObjectAt(7); DERInteger crtCoef = (DERInteger) seq.getObjectAt(8); RSAPrivateCrtKeySpec privSpec = new RSAPrivateCrtKeySpec(mod.getValue(), pubExp.getValue(), privExp.getValue(), p1.getValue(), p2.getValue(), exp1.getValue(), exp2.getValue(), crtCoef.getValue()); KeyFactory factory = KeyFactory.getInstance("RSA"); return factory.generatePrivate(privSpec); }
From source file:com.github.ibole.infrastructure.security.key.PemUtils.java
private static PrivateKey getPrivateKey(byte[] keyBytes, String algorithm) { PrivateKey privateKey = null; try {// w ww .j a v a 2 s . com KeyFactory kf = KeyFactory.getInstance(algorithm); EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); privateKey = kf.generatePrivate(keySpec); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { MoreThrowables.throwIfUnchecked(e); } return privateKey; }
From source file:com.cloudbees.tftwoway.Client.java
private static PrivateKey loadRSAKey(String path) throws Exception { try (InputStream fis = new FileInputStream(path)) { try (PemReader pemReader = new PemReader(new InputStreamReader(fis))) { byte[] pemBytes = pemReader.readPemObject().getContent(); KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC"); KeySpec spec = new PKCS8EncodedKeySpec(pemBytes); return keyFactory.generatePrivate(spec); }/* w w w . j a v a 2s . c o m*/ } }
From source file:org.apache.james.jdkim.IscheduleDKIMSigner.java
/** * Generate a PrivateKey from a Base64 encoded private key. * * In order to generate a valid PKCS8 key when you have a PEM key you can do * this: <code>// w ww . j a v a 2s.com * openssl pkcs8 -topk8 -inform PEM -in rsapriv.pem -outform DER -nocrypt -out rsapriv.der * </code> And then base64 encode the content. * * @param privateKeyPKCS8 * a Base64 encoded string of the RSA key in PKCS8 format * @return the PrivateKey * @throws NoSuchAlgorithmException * if RSA is unknown * @throws InvalidKeySpecException * on bad input key */ public static PrivateKey getPrivateKey(final String privateKeyPKCS8) throws NoSuchAlgorithmException, InvalidKeySpecException { byte[] encKey = Base64.decodeBase64(privateKeyPKCS8.getBytes()); // byte[] encKey = privateKey.getBytes(); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(encKey); KeyFactory keyFactory; keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privKey = keyFactory.generatePrivate(privSpec); return privKey; }
From source file:com.vmware.identity.rest.idm.data.PrivateKeyDTO.java
private static PrivateKey decodePrivateKey(String encoded, String algorithm) throws InvalidKeySpecException, NoSuchAlgorithmException { if (encoded == null) { return null; }// w w w .j a v a 2 s. co m byte[] clear = Base64.decodeBase64(encoded); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear); KeyFactory fact = KeyFactory.getInstance(algorithm); PrivateKey privateKey = fact.generatePrivate(keySpec); Arrays.fill(clear, (byte) 0); return privateKey; }
From source file:org.apache.james.jdkim.DKIMSigner.java
/** * Generate a PrivateKey from a Base64 encoded private key. * /*w w w . j a va2s . c o m*/ * In order to generate a valid PKCS8 key when you have a PEM key you can do * this: <code> * openssl pkcs8 -topk8 -inform PEM -in rsapriv.pem -outform DER -nocrypt -out rsapriv.der * </code> And then base64 encode the content. * * @param privateKeyPKCS8 * a Base64 encoded string of the RSA key in PKCS8 format * @return the PrivateKey * @throws NoSuchAlgorithmException * if RSA is unknown * @throws InvalidKeySpecException * on bad input key */ public static PrivateKey getPrivateKey(String privateKeyPKCS8) throws NoSuchAlgorithmException, InvalidKeySpecException { byte[] encKey = Base64.decodeBase64(privateKeyPKCS8.getBytes()); // byte[] encKey = privateKey.getBytes(); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(encKey); KeyFactory keyFactory; keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privKey = keyFactory.generatePrivate(privSpec); return privKey; }