Search in sources :

Example 21 with KeySpec

use of java.security.spec.KeySpec in project j2objc by google.

the class SubjectPublicKeyInfo method getPublicKey.

/**
     * Returns the PublicKey corresponding to this SubjectPublicKeyInfo
     * instance.
     */
public PublicKey getPublicKey() {
    if (publicKey == null) {
        final byte[] encoded = getEncoded();
        final KeySpec keySpec = new X509EncodedKeySpec(encoded);
        /* Try using the algorithm name first. */
        final String algName = algorithmID.getAlgorithmName();
        publicKey = generateKeyForAlgorithm(keySpec, algName);
        /*
             * Fall back to using the algorithm OID if it's not the same as the
             * algorithm name.
             */
        final String algOid = algorithmID.getAlgorithm();
        if (publicKey == null && !algOid.equals(algName)) {
            publicKey = generateKeyForAlgorithm(keySpec, algOid);
        }
        /*
             * Encode this as an X.509 public key since we didn't have any
             * KeyFactory that could handle this algorithm name or OID. Perhaps
             * the thing that's using this can decode it.
             */
        if (publicKey == null) {
            publicKey = new X509PublicKey(algOid, encoded, subjectPublicKey);
        }
    }
    return publicKey;
}
Also used : KeySpec(java.security.spec.KeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) ASN1BitString(org.apache.harmony.security.asn1.ASN1BitString) BitString(org.apache.harmony.security.asn1.BitString)

Example 22 with KeySpec

use of java.security.spec.KeySpec in project bigbluebutton by bigbluebutton.

the class RSA_SHA1 method getPublicKeyFromPem.

private PublicKey getPublicKeyFromPem(String pem) throws GeneralSecurityException, IOException {
    InputStream stream = new ByteArrayInputStream(pem.getBytes("UTF-8"));
    PEMReader reader = new PEMReader(stream);
    byte[] bytes = reader.getDerBytes();
    PublicKey pubKey;
    if (PEMReader.PUBLIC_X509_MARKER.equals(reader.getBeginMarker())) {
        KeySpec keySpec = new X509EncodedKeySpec(bytes);
        KeyFactory fac = KeyFactory.getInstance("RSA");
        pubKey = fac.generatePublic(keySpec);
    } else if (PEMReader.CERTIFICATE_X509_MARKER.equals(reader.getBeginMarker())) {
        pubKey = getPublicKeyFromDerCert(bytes);
    } else {
        throw new IOException("Invalid PEM fileL: Unknown marker for " + " public key or cert " + reader.getBeginMarker());
    }
    return pubKey;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PEMReader(net.oauth.signature.pem.PEMReader) PublicKey(java.security.PublicKey) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS1EncodedKeySpec(net.oauth.signature.pem.PKCS1EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) IOException(java.io.IOException) KeyFactory(java.security.KeyFactory)

Example 23 with KeySpec

use of java.security.spec.KeySpec in project bigbluebutton by bigbluebutton.

the class RSA_SHA1 method getPrivateKeyFromPem.

private PrivateKey getPrivateKeyFromPem(String pem) throws GeneralSecurityException, IOException {
    InputStream stream = new ByteArrayInputStream(pem.getBytes("UTF-8"));
    PEMReader reader = new PEMReader(stream);
    byte[] bytes = reader.getDerBytes();
    KeySpec keySpec;
    if (PEMReader.PRIVATE_PKCS1_MARKER.equals(reader.getBeginMarker())) {
        keySpec = (new PKCS1EncodedKeySpec(bytes)).getKeySpec();
    } else if (PEMReader.PRIVATE_PKCS8_MARKER.equals(reader.getBeginMarker())) {
        keySpec = new PKCS8EncodedKeySpec(bytes);
    } else {
        throw new IOException("Invalid PEM file: Unknown marker " + "for private key " + reader.getBeginMarker());
    }
    KeyFactory fac = KeyFactory.getInstance("RSA");
    return fac.generatePrivate(keySpec);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PEMReader(net.oauth.signature.pem.PEMReader) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS1EncodedKeySpec(net.oauth.signature.pem.PKCS1EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) IOException(java.io.IOException) PKCS1EncodedKeySpec(net.oauth.signature.pem.PKCS1EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 24 with KeySpec

use of java.security.spec.KeySpec in project pulsar by yahoo.

the class SecurityUtility method loadPrivateKeyFromPemFile.

public static PrivateKey loadPrivateKeyFromPemFile(String keyFilePath) throws KeyManagementException {
    PrivateKey privateKey = null;
    if (keyFilePath == null || keyFilePath.isEmpty()) {
        return privateKey;
    }
    try (BufferedReader reader = new BufferedReader(new FileReader(keyFilePath))) {
        StringBuilder sb = new StringBuilder();
        String previousLine = "";
        String currentLine = null;
        // Skip the first line (-----BEGIN RSA PRIVATE KEY-----)
        reader.readLine();
        while ((currentLine = reader.readLine()) != null) {
            sb.append(previousLine);
            previousLine = currentLine;
        }
        // Skip the last line (-----END RSA PRIVATE KEY-----)
        KeyFactory kf = KeyFactory.getInstance("RSA");
        KeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(sb.toString()));
        privateKey = kf.generatePrivate(keySpec);
    } catch (GeneralSecurityException | IOException e) {
        throw new KeyManagementException("Private key loading error", e);
    }
    return privateKey;
}
Also used : PrivateKey(java.security.PrivateKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) KeyFactory(java.security.KeyFactory) KeyManagementException(java.security.KeyManagementException)

Example 25 with KeySpec

use of java.security.spec.KeySpec in project hutool by looly.

the class SecureUtil method generateDESKey.

/**
 * 生成 {@link SecretKey}
 *
 * @param algorithm DES算法,包括DES、DESede等
 * @param key 密钥
 * @return {@link SecretKey}
 */
public static SecretKey generateDESKey(String algorithm, byte[] key) {
    if (StrUtil.isBlank(algorithm) || false == algorithm.startsWith("DES")) {
        throw new CryptoException("Algorithm [{}] is not a DES algorithm!");
    }
    SecretKey secretKey = null;
    if (null == key) {
        secretKey = generateKey(algorithm);
    } else {
        KeySpec keySpec;
        try {
            if (algorithm.startsWith("DESede")) {
                // DESede兼容
                keySpec = new DESedeKeySpec(key);
            } else {
                keySpec = new DESKeySpec(key);
            }
        } catch (InvalidKeyException e) {
            throw new CryptoException(e);
        }
        secretKey = generateKey(algorithm, keySpec);
    }
    return secretKey;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

KeySpec (java.security.spec.KeySpec)149 PBEKeySpec (javax.crypto.spec.PBEKeySpec)66 SecretKeyFactory (javax.crypto.SecretKeyFactory)62 KeyFactory (java.security.KeyFactory)46 SecretKeySpec (javax.crypto.spec.SecretKeySpec)46 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)39 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 SecretKey (javax.crypto.SecretKey)35 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)34 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)28 BigInteger (java.math.BigInteger)25 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)25 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)23 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)21 PrivateKey (java.security.PrivateKey)19 Cipher (javax.crypto.Cipher)16 IOException (java.io.IOException)15 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)14 PublicKey (java.security.PublicKey)13 InvalidKeyException (java.security.InvalidKeyException)12