List of usage examples for java.security.spec PKCS8EncodedKeySpec PKCS8EncodedKeySpec
public PKCS8EncodedKeySpec(byte[] encodedKey)
From source file:cn.mrdear.pay.util.RSAUtils.java
/** * ??/* w ww . j ava2 s . 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:com.github.ibole.infrastructure.security.key.PemUtils.java
private static PrivateKey getPrivateKey(byte[] keyBytes, String algorithm) { PrivateKey privateKey = null; try {/*from www . j ava 2 s . c o m*/ 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:org.apache.camel.component.gae.auth.GAuthPk8Loader.java
/** * Loads a private key from a PKCS#8 file. *//*from w ww . j a v a 2s.c o m*/ public PrivateKey loadPrivateKey() throws Exception { String str = IOUtils.toString(keyLocation.getInputStream()); if (str.contains(BEGIN) && str.contains(END)) { str = str.substring(BEGIN.length(), str.lastIndexOf(END)); } KeyFactory factory = KeyFactory.getInstance("RSA"); return factory.generatePrivate(new PKCS8EncodedKeySpec(Base64.decode(str))); }
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;// www . j a v a 2 s. c o m try { 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); }
From source file:CA.InternalCA.java
/** * Method to read private key from file. * * @param inputStream/*from w ww . ja v a2 s . c om*/ * * @return */ private PrivateKey readPrivateKey(InputStream inputStream) { try { PKCS8EncodedKeySpec kspec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(inputStream)); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey privKey = kf.generatePrivate(kspec); return privKey; } catch (Exception e) { LOG.info("Cannot load private key: " + e.getMessage()); return null; } }
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
/** * ??/* www. j av a 2s . c o 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:my.adam.smo.common.AsymmetricEncryptionBox.java
@PostConstruct public void init() throws NoSuchAlgorithmException { keyGen = KeyPairGenerator.getInstance("RSA"); try {//from ww w . j a v a 2 s . co m PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(Base64.decode(this.privKeyS)); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); prvKey = keyFactory.generatePrivate(privKeySpec); } catch (InvalidKeySpecException e) { logger.error("invalid private key", e); } try { X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(Base64.decode(this.pubKeyS)); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); pubKey = keyFactory.generatePublic(pubKeySpec); } catch (InvalidKeySpecException e) { logger.error("invalid public key", e); } }
From source file:org.haox.pki.Pkix.java
public static PrivateKey getPrivateKey(InputStream inputStream, String password) throws GeneralSecurityException, IOException { if (password == null) password = ""; // If the provided InputStream is encrypted, we need a password to decrypt // it. If the InputStream is not encrypted, then the password is ignored // (can be null). The InputStream can be DER (raw ASN.1) or PEM (base64). PKCS8Key pkcs8 = new PKCS8Key(inputStream, password.toCharArray()); // If an unencrypted PKCS8 key was provided, then this actually returns // exactly what was originally passed inputStream (with no changes). If an OpenSSL // key was provided, it gets reformatted as PKCS #8 first, and so these // bytes will still be PKCS #8, not OpenSSL. byte[] decrypted = pkcs8.getDecryptedBytes(); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decrypted); // A Java PrivateKey object is born. PrivateKey pk = null;//from ww w. j a v a 2 s .c om if (pkcs8.isDSA()) { pk = KeyFactory.getInstance("DSA").generatePrivate(spec); } else if (pkcs8.isRSA()) { pk = KeyFactory.getInstance("RSA").generatePrivate(spec); } // For lazier types: pk = pkcs8.getPrivateKey(); return pk; }
From source file:net.nicholaswilliams.java.licensing.encryption.KeyFileUtilities.java
public static PrivateKey readEncryptedPrivateKey(byte[] fileContents, char[] passphrase) { PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec( Encryptor.decryptRaw(fileContents, passphrase)); try {// w w w . ja v a2 s .c o m return KeyFactory.getInstance(KeyFileUtilities.keyAlgorithm).generatePrivate(privateKeySpec); } catch (NoSuchAlgorithmException e) { throw new AlgorithmNotSupportedException(KeyFileUtilities.keyAlgorithm, e); } catch (InvalidKeySpecException e) { throw new InappropriateKeySpecificationException(e); } }