Search in sources :

Example 81 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project cas by apereo.

the class PublicKeyFactoryBean method createInstance.

@Override
protected PublicKey createInstance() throws Exception {
    LOGGER.debug("Creating public key instance from [{}] using [{}]", this.resource.getFilename(), this.algorithm);
    try (InputStream pubKey = this.resource.getInputStream()) {
        final byte[] bytes = new byte[(int) this.resource.contentLength()];
        pubKey.read(bytes);
        final X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(bytes);
        final KeyFactory factory = KeyFactory.getInstance(this.algorithm);
        return factory.generatePublic(pubSpec);
    }
}
Also used : InputStream(java.io.InputStream) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory)

Example 82 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project Payara by payara.

the class SignedJWTIdentityStore method readPublicKey.

public PublicKey readPublicKey(String resourceName) throws Exception {
    byte[] byteBuffer = new byte[16384];
    int length = currentThread().getContextClassLoader().getResource(resourceName).openStream().read(byteBuffer);
    String key = new String(byteBuffer, 0, length).replaceAll("-----BEGIN (.*)-----", "").replaceAll("-----END (.*)----", "").replaceAll("\r\n", "").replaceAll("\n", "").trim();
    return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(Base64.getDecoder().decode(key)));
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec)

Example 83 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project scheduling by ow2-proactive.

the class Credentials method getPublicKey.

/**
 * Retrieves a public key stored in a local file
 * <p>
 *
 * @param pubPath path to the public key on the local filesystem
 * @return the key encapsulated in a regular JCE container
 * @throws KeyException the key could not be retrieved or is malformed
 */
public static PublicKey getPublicKey(String pubPath) throws KeyException {
    String algo = "", tmp = "";
    byte[] bytes;
    File f = new File(pubPath);
    // recover public key bytes
    try (DataInputStream in = new DataInputStream(new FileInputStream(f))) {
        int read, tot = 0;
        while ((read = in.read()) != '\n') {
            algo += (char) read;
            tot++;
        }
        tot++;
        while ((read = in.read()) != '\n') {
            tmp += (char) read;
            tot++;
        }
        tot++;
        bytes = new byte[(int) f.length() - tot];
        in.readFully(bytes);
    } catch (Exception e) {
        throw new KeyException("Could not retrieve public key from " + pubPath, e);
    }
    // reconstruct public key
    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(bytes);
    PublicKey pubKey;
    KeyFactory keyFactory;
    try {
        keyFactory = KeyFactory.getInstance(algo);
    } catch (NoSuchAlgorithmException e) {
        throw new KeyException("Cannot initialize key factory", e);
    }
    try {
        pubKey = keyFactory.generatePublic(pubKeySpec);
    } catch (InvalidKeySpecException e) {
        throw new KeyException("Cannot re-generate public key", e);
    }
    return pubKey;
}
Also used : PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) DHPublicKey(javax.crypto.interfaces.DHPublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) KeyException(java.security.KeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyException(java.security.KeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) File(java.io.File) KeyFactory(java.security.KeyFactory)

Example 84 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project GNS by MobilityFirst.

the class KeyPairUtilsAndroid method readGuidEntryFromFile.

private static GuidEntry readGuidEntryFromFile(String gnsName, String username) {
    File gnsFolder = new File(GNS_KEY_DIR);
    // Save the path as a string value
    String extStorageDirectory = gnsFolder.toString();
    File file = new File(extStorageDirectory, GNS_KEYS_FILENAME);
    String aliasKey = gnsName + "@" + username;
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            if (line.equals(aliasKey)) {
                // found the correct alias, read next three lines for guid, public
                // key,
                // private key
                String guid = br.readLine();
                String publicString = br.readLine();
                String privateString = br.readLine();
                if (!publicString.isEmpty() && !privateString.isEmpty()) {
                    try {
                        byte[] encodedPublicKey = DatatypeConverter.parseHexBinary(publicString);
                        byte[] encodedPrivateKey = DatatypeConverter.parseHexBinary(privateString);
                        //              byte[] encodedPublicKey = ByteUtils.hexStringToByteArray(publicString);
                        //              byte[] encodedPrivateKey = ByteUtils.hexStringToByteArray(privateString);
                        KeyFactory keyFactory = KeyFactory.getInstance(GNSProtocol.RSA_ALGORITHM.toString());
                        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
                        PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
                        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
                        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
                        return new GuidEntry(username, guid, publicKey, privateKey);
                    } catch (NoSuchAlgorithmException | InvalidKeySpecException | EncryptionException e) {
                        Log.e(KeyPairUtilsAndroid.class.getName(), "Cannot decode keys", e);
                    }
                }
                break;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    // You'll need to add proper error handling here
    }
    return null;
}
Also used : PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BufferedReader(java.io.BufferedReader) EncryptionException(edu.umass.cs.gnscommon.exceptions.client.EncryptionException) FileReader(java.io.FileReader) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) File(java.io.File) KeyFactory(java.security.KeyFactory)

Example 85 with X509EncodedKeySpec

use of java.security.spec.X509EncodedKeySpec in project xabber-android by redsolution.

the class AccountRealm method getKeyPair.

public KeyPair getKeyPair() {
    if (this.privateKeyBytes == null || this.publicKeyBytes == null) {
        return null;
    }
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
    PublicKey publicKey;
    PrivateKey privateKey;
    KeyFactory keyFactory;
    try {
        keyFactory = KeyFactory.getInstance("DSA");
        publicKey = keyFactory.generatePublic(publicKeySpec);
        privateKey = keyFactory.generatePrivate(privateKeySpec);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        throw new RuntimeException(e);
    }
    return new KeyPair(publicKey, privateKey);
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Aggregations

X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)162 KeyFactory (java.security.KeyFactory)112 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)93 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)80 PublicKey (java.security.PublicKey)65 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)45 InvalidKeyException (java.security.InvalidKeyException)30 PrivateKey (java.security.PrivateKey)27 IOException (java.io.IOException)26 RSAPublicKey (java.security.interfaces.RSAPublicKey)20 Signature (java.security.Signature)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 BigInteger (java.math.BigInteger)11 CertificateException (java.security.cert.CertificateException)10 X509Certificate (java.security.cert.X509Certificate)10 EncodedKeySpec (java.security.spec.EncodedKeySpec)10 SecretKey (javax.crypto.SecretKey)9 KeyPair (java.security.KeyPair)8 ECPublicKey (java.security.interfaces.ECPublicKey)8 EncryptionException (edu.umass.cs.gnscommon.exceptions.client.EncryptionException)7