Search in sources :

Example 46 with PKCS8EncodedKeySpec

use of java.security.spec.PKCS8EncodedKeySpec in project yyl_example by Relucent.

the class Rsa method getPrivateKey.

/**
	 * 得到私钥
	 * @param key 密钥字符串(经过base64编码)
	 * @throws Exception
	 */
public static PrivateKey getPrivateKey(String key) throws Exception {
    byte[] keyBytes;
    keyBytes = (new sun.misc.BASE64Decoder()).decodeBuffer(key);
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
    return privateKey;
}
Also used : PrivateKey(java.security.PrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 47 with PKCS8EncodedKeySpec

use of java.security.spec.PKCS8EncodedKeySpec in project azure-sdk-for-java by Azure.

the class CertificateOperationsTest method extractPrivateKeyFromPemContents.

/**
     * Extracts private key from PEM contents
     * 
     * @throws InvalidKeySpecException
     * @throws NoSuchAlgorithmException
     */
private static PrivateKey extractPrivateKeyFromPemContents(String pemContents) throws InvalidKeySpecException, NoSuchAlgorithmException {
    Matcher matcher = _privateKey.matcher(pemContents);
    if (!matcher.find()) {
        throw new IllegalArgumentException("No private key found in PEM contents.");
    }
    byte[] privateKeyBytes = _base64.decode(matcher.group(1));
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(ALGO_RSA);
    PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
    return privateKey;
}
Also used : PrivateKey(java.security.PrivateKey) Matcher(java.util.regex.Matcher) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 48 with PKCS8EncodedKeySpec

use of java.security.spec.PKCS8EncodedKeySpec in project Gradle-demo by Arisono.

the class RSAUtils method restorePrivateKey.

/**
	 * 还原私钥,PKCS8EncodedKeySpec 用于构建私钥的规范
	 * 
	 * @param keyBytes
	 * @return
	 */
public static PrivateKey restorePrivateKey(byte[] keyBytes) {
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);
    try {
        KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM);
        PrivateKey privateKey = factory.generatePrivate(pkcs8EncodedKeySpec);
        return privateKey;
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 49 with PKCS8EncodedKeySpec

use of java.security.spec.PKCS8EncodedKeySpec in project CloudStack-archive by CloudStack-extras.

the class CertificateHelper method buildPrivateKey.

public static Key buildPrivateKey(String base64EncodedKeyContent) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PKCS8EncodedKeySpec keysp = new PKCS8EncodedKeySpec(Base64.decodeBase64(base64EncodedKeyContent));
    return kf.generatePrivate(keysp);
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 50 with PKCS8EncodedKeySpec

use of java.security.spec.PKCS8EncodedKeySpec in project graylog2-server by Graylog2.

the class KeyUtil method createKeySpec.

private static PKCS8EncodedKeySpec createKeySpec(byte[] keyBytes, String password) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
    if (Strings.isNullOrEmpty(password)) {
        return new PKCS8EncodedKeySpec(keyBytes);
    }
    final EncryptedPrivateKeyInfo pkInfo = new EncryptedPrivateKeyInfo(keyBytes);
    final SecretKeyFactory kf = SecretKeyFactory.getInstance(pkInfo.getAlgName());
    final PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
    final SecretKey secretKey = kf.generateSecret(keySpec);
    @SuppressWarnings("InsecureCryptoUsage") final Cipher cipher = Cipher.getInstance(pkInfo.getAlgName());
    cipher.init(Cipher.DECRYPT_MODE, secretKey, pkInfo.getAlgParameters());
    return pkInfo.getKeySpec(cipher);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)227 KeyFactory (java.security.KeyFactory)179 PrivateKey (java.security.PrivateKey)148 CertificateFactory (java.security.cert.CertificateFactory)86 ByteArrayInputStream (java.io.ByteArrayInputStream)85 Certificate (java.security.cert.Certificate)72 X509Certificate (java.security.cert.X509Certificate)71 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)59 Entry (java.security.KeyStore.Entry)53 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)53 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)50 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)47 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)40 PublicKey (java.security.PublicKey)39 IOException (java.io.IOException)30 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)30 SecretKey (javax.crypto.SecretKey)28 InvalidKeyException (java.security.InvalidKeyException)26 Key (java.security.Key)24 KeyStoreException (java.security.KeyStoreException)15