List of usage examples for java.security KeyFactory generatePrivate
public final PrivateKey generatePrivate(KeySpec keySpec) throws InvalidKeySpecException
From source file:net.padlocksoftware.padlock.KeyManager.java
/** * Import a Padlock 2.x (DSA based) KeyPair from an InputStream. The stream is * assumed to have been previously exported in a supported format using the * exportKeyPair methods./* w ww . j a va2s .c om*/ * @param stream The KeyPair stream to import. * @return The DSA KeyPair contained in the specified file. * @throws java.io.IOException If file is missing or contain invalid data. * @since 2.0 */ public static KeyPair importKeyPair(InputStream stream) throws IOException { if (stream == null) throw new IllegalArgumentException("Stream cannot be null"); KeyPair pair = null; Properties p = new Properties(); p.load(stream); stream.close(); String pri = p.getProperty("private"); String pub = p.getProperty("public"); if (pri == null || pub == null) { throw new IOException("Stream data is invalid"); } // Load the keys try { PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(Hex.decodeHex(pri.toCharArray())); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(Hex.decodeHex(pub.toCharArray())); PublicKey publicKey = keyFactory.generatePublic(pubSpec); pair = new KeyPair(publicKey, privateKey); } catch (Exception e) { throw new RuntimeException("Invalid stream: " + e.getMessage()); } return pair; }
From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * ?/* w w w .jav a2 s . c o m*/ * * @param data * ? * @param key * * @return byte[] ? */ public static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception { // ?? PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ?? PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); }
From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * ?/* w ww.j av a 2 s.c om*/ * * @param data? * @param key * * @return byte[] ? */ public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception { // ?? PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ?? PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(data); }
From source file:org.zaproxy.zap.extension.dynssl.SslCertificateUtils.java
private static RSAPrivateKey generatePrivateKeyFromDER(byte[] keyBytes) throws InvalidKeySpecException, NoSuchAlgorithmException { PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory factory = KeyFactory.getInstance("RSA"); return (RSAPrivateKey) factory.generatePrivate(spec); }
From source file:com.emc.vipr.services.s3.S3ClientFactory.java
/** * Creates an EncryptionClient for testing. Loads the public and private keys from * the properties file (not suitable for production). * * @return/*from ww w .ja va2s .c o m*/ * @throws IOException */ public static AmazonS3EncryptionClient getEncryptionClient() throws IOException { try { Properties props = ViprConfig.getProperties(); String accessKey = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_S3_ACCESS_KEY_ID); String secretKey = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_S3_SECRET_KEY); String endpoint = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_S3_ENDPOINT); String publicKey = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_PUBLIC_KEY); String privateKey = ViprConfig.getPropertyNotEmpty(props, ViprConfig.PROP_PRIVATE_KEY); byte[] pubKeyBytes = Base64.decodeBase64(publicKey.getBytes("US-ASCII")); byte[] privKeyBytes = Base64.decodeBase64(privateKey.getBytes("US-ASCII")); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyBytes); PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privKeyBytes); PublicKey pubKey; PrivateKey privKey; try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); pubKey = keyFactory.generatePublic(pubKeySpec); privKey = keyFactory.generatePrivate(privKeySpec); } catch (GeneralSecurityException e) { throw new RuntimeException("Could not load key pair: " + e, e); } EncryptionMaterials keys = new EncryptionMaterials(new KeyPair(pubKey, privKey)); BasicAWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey); AmazonS3EncryptionClient client = new AmazonS3EncryptionClient(creds, keys); client.setEndpoint(endpoint); checkProxyConfig(client, props); return client; } catch (Exception e) { log.info("Could not load configuration: " + e); return null; } }
From source file:com.launchkey.sdk.crypto.JCECrypto.java
/** * Get an RSA private key utilizing the provided provider and PEM formatted string * @param provider Provider to generate the key * @param pem PEM formatted key string//from w w w . j a va 2s . co m * @return */ public static RSAPrivateKey getRSAPrivateKeyFromPEM(Provider provider, String pem) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA", provider); return (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(getKeyBytesFromPEM(pem))); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException("Algorithm SHA256withRSA is not available", e); } catch (InvalidKeySpecException e) { throw new IllegalArgumentException("Invalid PEM provided", e); } }
From source file:com.thoughtworks.go.security.RegistrationJSONizer.java
public static Registration fromJson(String json) { Map map = GSON.fromJson(json, Map.class); if (map.isEmpty()) { return Registration.createNullPrivateKeyEntry(); }//from ww w. j a v a 2s . com List<X509Certificate> chain = new ArrayList<>(); try { PemReader reader = new PemReader(new StringReader((String) map.get("agentPrivateKey"))); KeyFactory kf = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent()); PrivateKey privateKey = kf.generatePrivate(spec); String agentCertificate = (String) map.get("agentCertificate"); PemReader certReader = new PemReader(new StringReader(agentCertificate)); while (true) { PemObject obj = certReader.readPemObject(); if (obj == null) { break; } chain.add((X509Certificate) CertificateFactory.getInstance("X.509") .generateCertificate(new ByteArrayInputStream(obj.getContent()))); } return new Registration(privateKey, chain.toArray(new X509Certificate[0])); } catch (IOException | NoSuchAlgorithmException | CertificateException | InvalidKeySpecException e) { throw bomb(e); } }
From source file:org.samlsnort.util.KeyStoreTool.java
public static void importIntoKeystore(File certificateChainFile, File privateKeyFile, String alias) throws Exception { FileInputStream certificateStream = new FileInputStream(certificateChainFile); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); Certificate[] chain = {};//from w ww .j a v a2 s .c o m chain = certificateFactory.generateCertificates(certificateStream).toArray(chain); certificateStream.close(); byte[] encodedKey = new byte[(int) privateKeyFile.length()]; FileInputStream keyInputStream = new FileInputStream(privateKeyFile); keyInputStream.read(encodedKey); keyInputStream.close(); KeyFactory rSAKeyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = rSAKeyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey)); keyStore.setEntry(alias, new KeyStore.PrivateKeyEntry(privateKey, chain), new KeyStore.PasswordProtection(Configuration.getInstance().getKeystorePassword().toCharArray())); saveKeystore(); }
From source file:com.vmware.o11n.plugin.crypto.model.CryptoUtil.java
/** * Generate a RSA Private Key from a KeySpec * * @param keySpec// w w w . j ava2s . c om * @return RSA Private Key * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ private static PrivateKey getPrivateKey(KeySpec keySpec) throws NoSuchAlgorithmException, InvalidKeySpecException { KeyFactory fac = KeyFactory.getInstance(KEYFACTORY_ALGORITHM); return fac.generatePrivate(keySpec); }
From source file:io.fabric8.utils.cxf.WebClients.java
public static KeyStore createKeyStore(String clientCertData, File clientCertFile, String clientKeyData, File clientKeyFile, String clientKeyAlgo, char[] clientKeyPassword) throws Exception { try (InputStream certInputStream = getInputStreamFromDataOrFile(clientCertData, clientCertFile)) { CertificateFactory certFactory = CertificateFactory.getInstance("X509"); X509Certificate cert = (X509Certificate) certFactory.generateCertificate(certInputStream); InputStream keyInputStream = getInputStreamFromDataOrFile(clientKeyData, clientKeyFile); PEMReader reader = new PEMReader(keyInputStream); RSAPrivateCrtKeySpec keySpec = new PKCS1EncodedKeySpec(reader.getDerBytes()).getKeySpec(); KeyFactory kf = KeyFactory.getInstance(clientKeyAlgo); RSAPrivateKey privKey = (RSAPrivateKey) kf.generatePrivate(keySpec); KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(null);/* w w w .jav a 2 s . c o m*/ String alias = cert.getSubjectX500Principal().getName(); keyStore.setKeyEntry(alias, privKey, clientKeyPassword, new Certificate[] { cert }); return keyStore; } }