Search in sources :

Example 41 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project gerrit by GerritCodeReview.

the class SshKeyCreatorImpl method create.

@Override
public AccountSshKey create(AccountSshKey.Id id, String encoded) throws InvalidSshKeyException {
    try {
        AccountSshKey key = new AccountSshKey(id, SshUtil.toOpenSshPublicKey(encoded));
        SshUtil.parse(key);
        return key;
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        throw new InvalidSshKeyException();
    } catch (NoSuchProviderException e) {
        log.error("Cannot parse SSH key", e);
        throw new InvalidSshKeyException();
    }
}
Also used : InvalidSshKeyException(com.google.gerrit.common.errors.InvalidSshKeyException) AccountSshKey(com.google.gerrit.reviewdb.client.AccountSshKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 42 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project sling by apache.

the class TopologyRequestValidator method encodeMessage.

/**
     * Encodes a request returning the encoded body
     *
     * @param body
     * @return the encoded body.
     * @throws IOException
     */
public String encodeMessage(String body) throws IOException {
    checkActive();
    if (encryptionEnabled) {
        try {
            JsonObjectBuilder json = Json.createObjectBuilder();
            JsonArrayBuilder array = Json.createArrayBuilder();
            for (String value : encrypt(body)) {
                array.add(value);
            }
            json.add("payload", array);
            StringWriter writer = new StringWriter();
            Json.createGenerator(writer).write(json.build()).close();
            return writer.toString();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (IllegalBlockSizeException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (BadPaddingException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (UnsupportedEncodingException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (NoSuchAlgorithmException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (NoSuchPaddingException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (JsonException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (InvalidKeySpecException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        } catch (InvalidParameterSpecException e) {
            throw new IOException("Unable to Encrypt Message " + e.getMessage());
        }
    }
    return body;
}
Also used : JsonException(javax.json.JsonException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) StringWriter(java.io.StringWriter) JsonArrayBuilder(javax.json.JsonArrayBuilder) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) JsonObjectBuilder(javax.json.JsonObjectBuilder)

Example 43 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project cloudstack by apache.

the class SAMLUtils method savePublicKey.

public static String savePublicKey(PublicKey key) {
    try {
        KeyFactory keyFactory = SAMLUtils.getKeyFactory();
        if (keyFactory == null)
            return null;
        X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.class);
        return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()), Charset.forName("UTF-8"));
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create KeyFactory:" + e.getMessage());
    }
    return null;
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 44 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project cloudstack by apache.

the class SAMLUtils method loadPublicKey.

public static PublicKey loadPublicKey(String publicKey) {
    byte[] sigBytes = org.bouncycastle.util.encoders.Base64.decode(publicKey);
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(sigBytes);
    KeyFactory keyFact = SAMLUtils.getKeyFactory();
    if (keyFact == null)
        return null;
    try {
        return keyFact.generatePublic(x509KeySpec);
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create PrivateKey from privateKey string:" + e.getMessage());
    }
    return null;
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Example 45 with InvalidKeySpecException

use of java.security.spec.InvalidKeySpecException in project robovm by robovm.

the class SecretKeyFactoryTest method test_PBKDF2_required_parameters.

public void test_PBKDF2_required_parameters() throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    // PBEKeySpecs password only constructor
    try {
        KeySpec ks = new PBEKeySpec(null);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(new char[0]);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(PASSWORD);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    // PBEKeySpecs constructor without key length
    try {
        KeySpec ks = new PBEKeySpec(null, SALT, ITERATIONS);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(new char[0], SALT, ITERATIONS);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(PASSWORD, SALT, ITERATIONS);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(null, SALT, ITERATIONS, KEY_LENGTH);
        factory.generateSecret(ks);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(new char[0], SALT, ITERATIONS, KEY_LENGTH);
        factory.generateSecret(ks);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    KeySpec ks = new PBEKeySpec(PASSWORD, SALT, ITERATIONS, KEY_LENGTH);
    factory.generateSecret(ks);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Aggregations

InvalidKeySpecException (java.security.spec.InvalidKeySpecException)483 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)306 KeyFactory (java.security.KeyFactory)199 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)155 InvalidKeyException (java.security.InvalidKeyException)116 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)108 IOException (java.io.IOException)98 PublicKey (java.security.PublicKey)90 PrivateKey (java.security.PrivateKey)77 SecretKeyFactory (javax.crypto.SecretKeyFactory)66 PBEKeySpec (javax.crypto.spec.PBEKeySpec)59 BigInteger (java.math.BigInteger)45 SignatureException (java.security.SignatureException)39 SecretKey (javax.crypto.SecretKey)38 BadPaddingException (javax.crypto.BadPaddingException)36 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)36 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)35 NoSuchProviderException (java.security.NoSuchProviderException)34 KeySpec (java.security.spec.KeySpec)32 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)30