List of usage examples for java.security.spec PKCS8EncodedKeySpec PKCS8EncodedKeySpec
public PKCS8EncodedKeySpec(byte[] encodedKey)
From source file:com.adobe.acs.commons.adobeio.service.impl.IntegrationServiceImpl.java
private PrivateKey getPrivateKey() throws NoSuchAlgorithmException, InvalidKeySpecException { PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buildPkcs8Key(jwtServiceConfig.privateKey())); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(keySpec); }
From source file:net.oauth.jsontoken.JsonTokenTestBase.java
@Override protected void setUp() throws Exception { final Verifier hmacVerifier = new HmacSHA256Verifier(SYMMETRIC_KEY); VerifierProvider hmacLocator = new VerifierProvider() { @Override//w w w . j a va2s. c om public List<Verifier> findVerifier(String signerId, String keyId) { return Lists.newArrayList(hmacVerifier); } }; VerifierProvider rsaLocator = new DefaultPublicKeyLocator(new IdentityServerDescriptorProvider(), new ServerInfoResolver() { @Override public ServerInfo resolve(URI uri) { return JsonServerInfo.getDocument(SERVER_INFO_DOCUMENT); } }); locators = new VerifierProviders(); locators.setVerifierProvider(SignatureAlgorithm.HS256, hmacLocator); locators.setVerifierProvider(SignatureAlgorithm.RS256, rsaLocator); EncodedKeySpec spec = new PKCS8EncodedKeySpec(Base64.decodeBase64(PRIVATE_KEY)); KeyFactory fac = KeyFactory.getInstance("RSA"); privateKey = (RSAPrivateKey) fac.generatePrivate(spec); //final Verifier hmacVerifierFromRuby = new HmacSHA256Verifier("R9bPJ_QRlcgK_hDLgu1Klg".getBytes()); final Verifier hmacVerifierFromRuby = new HmacSHA256Verifier("secret".getBytes()); VerifierProvider hmacLocatorFromRuby = new VerifierProvider() { @Override public List<Verifier> findVerifier(String signerId, String keyId) { return Lists.newArrayList(hmacVerifierFromRuby); } }; locatorsFromRuby = new VerifierProviders(); locatorsFromRuby.setVerifierProvider(SignatureAlgorithm.HS256, hmacLocatorFromRuby); }
From source file:com.forsrc.utils.MyRsa2Utils.java
/** * Gets private key.// www.j av a2 s . c om * * @param key the key * @return the private key * @throws RsaException the rsa exception */ public static PrivateKey getPrivateKey(String key) throws RsaException { byte[] keyBytes; try { keyBytes = (new Base64()).decode(key); } catch (Exception e) { throw new RsaException(e); } PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = null; try { keyFactory = KeyFactory.getInstance(RsaKey.ALGORITHM); } catch (NoSuchAlgorithmException e) { throw new RsaException(e); } PrivateKey privateKey = null; try { privateKey = keyFactory.generatePrivate(keySpec); } catch (InvalidKeySpecException e) { throw new RsaException(e); } return privateKey; }
From source file:de.alpharogroup.crypto.key.reader.PrivateKeyReader.java
/** * Read private key./* w w w. j ava 2s. c om*/ * * @param privateKeyBytes * the private key bytes * @param provider * the provider * @param algorithm * the algorithm for the {@link KeyFactory} * @return the private key * @throws NoSuchAlgorithmException * is thrown if instantiation of the cypher object fails. * @throws InvalidKeySpecException * is thrown if generation of the SecretKey object fails. * @throws NoSuchProviderException * is thrown if the specified provider is not registered in the security provider * list. */ public static PrivateKey readPrivateKey(final byte[] privateKeyBytes, final String provider, final String algorithm) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException { final PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); final KeyFactory keyFactory = KeyFactory.getInstance(algorithm); final PrivateKey privateKey = keyFactory.generatePrivate(keySpec); return privateKey; }
From source file:org.openengsb.core.services.internal.security.FileKeySource.java
protected PrivateKey readPrivateKeyFromFile(File keyDirectory) { byte[] keyData; try {//ww w. j a va 2s. co m File file = new File(keyDirectory, DEFAULT_PRIVATE_KEY_FILENAME); LOGGER.trace("reading private key form file: {}", file.getAbsolutePath()); keyData = FileUtils.readFileToByteArray(file); } catch (IOException e) { throw new IllegalStateException(e); } KeySpec keySpec = new PKCS8EncodedKeySpec(keyData); try { return getKeyFactory().generatePrivate(keySpec); } catch (InvalidKeySpecException e) { throw new IllegalStateException(e); } }
From source file:org.thingsboard.rule.engine.mqtt.credentials.CertPemClientCredentials.java
private PrivateKey readPrivateKeyFile(String fileContent) throws Exception { RSAPrivateKey privateKey = null; if (fileContent != null && !fileContent.isEmpty()) { fileContent = fileContent.replaceAll(".*BEGIN.*PRIVATE KEY.*", "") .replaceAll(".*END.*PRIVATE KEY.*", "").replaceAll("\\s", ""); byte[] decoded = Base64.decodeBase64(fileContent); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); privateKey = (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(decoded)); }//from w ww . ja v a 2s. c o m return privateKey; }
From source file:org.jboss.aerogear.cordova.crypto.CryptoPlugin.java
private java.security.KeyPair parseKeyPairFromJson(byte[] encodedPublicKey, byte[] encodedPrivateKey) { try {// ww w . j av a2 s .c o m KeyFactory fact = KeyFactory.getInstance("ECDH", AeroGearCrypto.PROVIDER); PublicKey publicKey2 = fact.generatePublic(new X509EncodedKeySpec(encodedPublicKey)); java.security.PrivateKey privateKey2 = fact.generatePrivate(new PKCS8EncodedKeySpec(encodedPrivateKey)); return new java.security.KeyPair(publicKey2, privateKey2); } catch (InvalidKeySpecException e) { throw new RuntimeException("could not reconstruct key pair from json!", e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("could not reconstruct key pair from json!", e); } catch (NoSuchProviderException e) { throw new RuntimeException("could not reconstruct key pair from json!", e); } }
From source file:org.aon.esolutions.appconfig.client.util.RSAEncryptUtil.java
/** * Generates Private Key from BASE64 encoded string * @param key BASE64 encoded string which represents the key * @return The PrivateKey//from ww w. j ava 2 s .c o m * @throws java.lang.Exception */ public static PrivateKey getPrivateKeyFromString(String key) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decodeBASE64(key)); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); return privateKey; }
From source file:org.ejbca.core.protocol.ws.client.NestedCrmfRequestTestCommand.java
private void init(String args[]) { FileInputStream file_inputstream; try {//from ww w . j ava2 s . co m String pwd = args[ARG_KEYSTOREPASSWORD]; String certNameInKeystore = args[ARG_CERTNAMEINKEYSTORE]; file_inputstream = new FileInputStream(args[ARG_KEYSTOREPATH]); KeyStore keyStore = KeyStore.getInstance("PKCS12"); keyStore.load(file_inputstream, pwd.toCharArray()); System.out.println("Keystore size " + keyStore.size()); Enumeration aliases = keyStore.aliases(); while (aliases.hasMoreElements()) { System.out.println(aliases.nextElement()); } Key key = keyStore.getKey(certNameInKeystore, pwd.toCharArray()); getPrintStream().println("Key information " + key.getAlgorithm() + " " + key.getFormat()); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(key.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); innerSignKey = keyFactory.generatePrivate(keySpec); innerCertificate = keyStore.getCertificate(certNameInKeystore); } catch (FileNotFoundException e2) { e2.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (UnrecoverableKeyException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } try { KeyPair outerSignKeys = KeyTools.genKeys("1024", "RSA"); outerSignKey = outerSignKeys.getPrivate(); X509Certificate signCert = CertTools.genSelfCert("CN=cmpTest,C=SE", 5000, null, outerSignKeys.getPrivate(), outerSignKeys.getPublic(), PKCSObjectIdentifiers.sha256WithRSAEncryption.getId(), true, "BC"); writeCertificate(signCert, "/opt/racerts", "cmpTest.pem"); /* ArrayList<Certificate> certCollection = new ArrayList<Certificate>(); certCollection.add(signCert); byte[] pemRaCert = CertTools.getPEMFromCerts(certCollection); FileOutputStream out = new FileOutputStream(new File("/opt/racerts/cmpStressTest.pem")); out.write(pemRaCert); out.close(); */ } catch (NoSuchAlgorithmException e1) { e1.printStackTrace(); } catch (NoSuchProviderException e1) { e1.printStackTrace(); } catch (InvalidAlgorithmParameterException e1) { e1.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (CertificateEncodingException e) { e.printStackTrace(); } catch (SignatureException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); //} catch (FileNotFoundException e) { // e.printStackTrace(); //} catch (IOException e) { // e.printStackTrace(); //} catch (CertificateException e) { // e.printStackTrace(); } }
From source file:com.cedarsoft.crypt.X509Support.java
/** * Reads a private key form a url/*from www . j a v a 2 s . c om*/ * * @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(); } }