List of usage examples for java.security.spec PKCS8EncodedKeySpec PKCS8EncodedKeySpec
public PKCS8EncodedKeySpec(byte[] encodedKey)
From source file:org.artifactory.security.crypto.CryptoHelper.java
static KeyPair createKeyPair(byte[] encodedPrivateKey, byte[] encodedPublicKey) { try {/*from w w w. j a v a 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.waveprotocol.wave.crypto.WaveSignerFactory.java
private PrivateKey getPrivateKey(InputStream privateKeyStream) throws SignatureException { try {/*from w w w . j a v a 2s. c o m*/ PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(readBase64Bytes(privateKeyStream)); KeyFactory keyFac = KeyFactory.getInstance("RSA"); return keyFac.generatePrivate(keySpec); } catch (NoSuchAlgorithmException e) { throw new SignatureException(e); } catch (InvalidKeySpecException e) { throw new SignatureException(e); } catch (IOException e) { throw new SignatureException(e); } }
From source file:com.adeptj.modules.security.jwt.internal.JwtKeys.java
static PrivateKey createSigningKey(JwtConfig jwtConfig, SignatureAlgorithm signatureAlgo) { LOGGER.info("Creating RSA signing key!!"); String keyData = jwtConfig.privateKey(); Assert.isTrue(StringUtils.startsWithAny(keyData, PRIVATE_ENCRYPTED_KEY_HEADER, PRIVATE_KEY_HEADER), INVALID_PRIVATE_KEY_MSG);/*w w w. java 2 s .c o m*/ try { KeyFactory keyFactory = KeyFactory.getInstance(signatureAlgo.getFamilyName()); if (StringUtils.startsWith(keyData, PRIVATE_ENCRYPTED_KEY_HEADER)) { LOGGER.info("Creating PKCS8EncodedKeySpec from private [encrypted] key !!"); Assert.hasText(jwtConfig.privateKeyPassword(), KEYPASS_NULL_MSG); byte[] privateKeyData = decodePrivateKeyData(keyData, true); EncryptedPrivateKeyInfo privateKeyInfo = new EncryptedPrivateKeyInfo(privateKeyData); SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(privateKeyInfo.getAlgName()); PBEKeySpec keySpec = new PBEKeySpec(jwtConfig.privateKeyPassword().toCharArray()); SecretKey secretKey = secretKeyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance(privateKeyInfo.getAlgName()); cipher.init(DECRYPT_MODE, secretKey, privateKeyInfo.getAlgParameters()); return keyFactory.generatePrivate(privateKeyInfo.getKeySpec(cipher)); } LOGGER.info("Creating PKCS8EncodedKeySpec from private key !!"); return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(decodePrivateKeyData(keyData, false))); } catch (GeneralSecurityException | IOException ex) { LOGGER.error(ex.getMessage(), ex); throw new KeyInitializationException(ex.getMessage(), ex); } }
From source file:it.scoppelletti.programmerpower.security.spi.EncodedKeyFactory.java
@Override protected KeySpec getKeySpec(String alg, KeyRep.Type keyType, Properties props, String prefix) { String name, value;/*from w w w . java 2 s .c o m*/ byte[] data; KeySpec keySpec; name = Strings.concat(prefix, EncodedKeyFactory.PROP_DATA); value = props.getProperty(name); if (Strings.isNullOrEmpty(value)) { throw new ArgumentNullException(name); } data = Base64.decodeBase64(value); switch (keyType) { case PUBLIC: keySpec = new X509EncodedKeySpec(data); break; case PRIVATE: keySpec = new PKCS8EncodedKeySpec(data); break; default: throw new EnumConstantNotPresentException(KeyRep.Type.class, keyType.toString()); } return keySpec; }
From source file:org.javaweb.utils.RSAUtils.java
/** * RSA???//from w w w . j a v a 2 s .co m * * @param data ? * @param key ? * @return * @throws Exception */ public static String sign(byte[] data, Key key) throws Exception { byte[] keyBytes = key.getEncoded(); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm()); PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initSign(privateK); signature.update(data); return Base64.encodeBase64String(signature.sign()); }
From source file:mitm.common.security.crl.GenerateTestCRLs.java
private PrivateKey decodePrivateKey(String encoded) throws Exception { byte[] rawKey = Hex.decodeHex(encoded.toCharArray()); KeySpec keySpec = new PKCS8EncodedKeySpec(rawKey); KeyFactory keyFactory = securityFactory.createKeyFactory("RSA"); return keyFactory.generatePrivate(keySpec); }
From source file:com.google.jenkins.plugins.credentials.oauth.JsonServiceAccountConfig.java
@Override public PrivateKey getPrivateKey() { JsonKey jsonKey = getJsonKey();/*from w w w .j a v a2 s . com*/ if (jsonKey != null) { String privateKey = jsonKey.getPrivateKey(); if (privateKey != null && !privateKey.isEmpty()) { PemReader pemReader = new PemReader(new StringReader(privateKey)); try { PemReader.Section section = pemReader.readNextSection(); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(section.getBase64DecodedBytes()); return KeyFactory.getInstance("RSA").generatePrivate(keySpec); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Failed to read private key", e); } catch (InvalidKeySpecException e) { LOGGER.log(Level.SEVERE, "Failed to read private key", e); } catch (NoSuchAlgorithmException e) { LOGGER.log(Level.SEVERE, "Failed to read private key", e); } } } return null; }
From source file:org.sonatype.sisu.encryptor.RsaAesEncryptor.java
protected PrivateKey readPrivateKey(InputStream keyInput) throws IOException, GeneralSecurityException { InputStream input = new Base64InputStream(keyInput); byte[] encKey = IOUtil.toByteArray(input); IOUtil.close(input);//from w ww .ja va 2 s.c o m PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encKey); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); return privateKey; }
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. j a va 2 s. c om*/ * @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.sixsq.slipstream.cookie.CryptoUtils.java
static private void setKeyPairFromDb() throws NoSuchAlgorithmException, InvalidKeySpecException, CertificateException { CookieKeyPair ckp = CookieKeyPair.load(); if (ckp == null) { return;/* w ww.j a va 2 s . com*/ } String privateKeyBase64 = ckp.getPrivateKey(); String publicKeyBase64 = ckp.getPublicKey(); if (privateKeyBase64 == null || publicKeyBase64 == null) { return; } byte[] privateKeyBytes = new Base64().decode(privateKeyBase64); KeyFactory keyFactory = KeyFactory.getInstance(keyPairAlgorithm); KeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes); privateKey = keyFactory.generatePrivate(privateKeySpec); byte[] publicKeyBytes = new Base64().decode(publicKeyBase64); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes); keyFactory = KeyFactory.getInstance(keyPairAlgorithm); publicKey = keyFactory.generatePublic(x509KeySpec); }