Search in sources :

Example 71 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project android-async-http by loopj.

the class CustomCASample method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        InputStream is = null;
        try {
            // Configure the library to use a custom 'bks' file to perform
            // SSL negotiation.
            KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
            is = getResources().openRawResource(R.raw.store);
            store.load(is, STORE_PASS.toCharArray());
            getAsyncHttpClient().setSSLSocketFactory(new SecureSocketFactory(store, STORE_ALIAS));
        } catch (IOException e) {
            throw new KeyStoreException(e);
        } catch (CertificateException e) {
            throw new KeyStoreException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new KeyStoreException(e);
        } catch (KeyManagementException e) {
            throw new KeyStoreException(e);
        } catch (UnrecoverableKeyException e) {
            throw new KeyStoreException(e);
        } finally {
            AsyncHttpClient.silentCloseInputStream(is);
        }
    } catch (KeyStoreException e) {
        Log.e(LOG_TAG, "Unable to initialize key store", e);
        showCustomCAHelp();
    }
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) InputStream(java.io.InputStream) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) SecureSocketFactory(com.loopj.android.http.sample.util.SecureSocketFactory) KeyManagementException(java.security.KeyManagementException)

Example 72 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project camel by apache.

the class XMLSecurityDataFormat method generateKeyEncryptionKey.

private Key generateKeyEncryptionKey(String algorithm) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
    DESedeKeySpec keySpec;
    Key secretKey;
    try {
        if (algorithm.equalsIgnoreCase("DESede")) {
            keySpec = new DESedeKeySpec(passPhrase);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
            secretKey = keyFactory.generateSecret(keySpec);
        } else if (algorithm.equalsIgnoreCase("SEED")) {
            secretKey = new SecretKeySpec(passPhrase, "SEED");
        } else if (algorithm.equalsIgnoreCase("CAMELLIA")) {
            secretKey = new SecretKeySpec(passPhrase, "CAMELLIA");
        } else {
            secretKey = new SecretKeySpec(passPhrase, "AES");
        }
        if (Arrays.equals(passPhrase, DEFAULT_KEY.getBytes())) {
            LOG.warn("Using the default encryption key is not secure");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeyException("InvalidKeyException due to invalid passPhrase: " + Arrays.toString(passPhrase));
    } catch (NoSuchAlgorithmException e) {
        throw new NoSuchAlgorithmException("NoSuchAlgorithmException while using algorithm: " + algorithm);
    } catch (InvalidKeySpecException e) {
        throw new InvalidKeySpecException("Invalid Key generated while using passPhrase: " + Arrays.toString(passPhrase));
    }
    return secretKey;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException) SecretKeyFactory(javax.crypto.SecretKeyFactory) PublicKey(java.security.PublicKey) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey)

Example 73 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project camel by apache.

the class SignatureDigestMethodTest method getKeyPair.

public static KeyPair getKeyPair(String algorithm, int keylength) {
    KeyPairGenerator keyGen;
    try {
        keyGen = KeyPairGenerator.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }
    keyGen.initialize(keylength, new SecureRandom());
    return keyGen.generateKeyPair();
}
Also used : SecureRandom(java.security.SecureRandom) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 74 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project camel by apache.

the class DefaultKeySelector method select.

public KeySelectorResult select(KeyInfo keyInfo, KeySelector.Purpose purpose, AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException {
    if (keyStoreAndAlias.getKeyStore() == null) {
        return getNullKeyResult();
    }
    if (keyStoreAndAlias.getAlias() == null) {
        return getNullKeyResult();
    }
    if (KeySelector.Purpose.VERIFY.equals(purpose)) {
        Certificate cert;
        try {
            cert = keyStoreAndAlias.getKeyStore().getCertificate(keyStoreAndAlias.getAlias());
        } catch (KeyStoreException e) {
            throw new KeySelectorException(e);
        }
        if (cert == null) {
            return getNullKeyResult();
        }
        final Key key = cert.getPublicKey();
        return getKeySelectorResult(key);
    } else if (KeySelector.Purpose.SIGN.equals(purpose)) {
        if (keyStoreAndAlias.getPassword() == null) {
            return getNullKeyResult();
        }
        Key key;
        try {
            if (this.getCamelContext() != null && keyStoreAndAlias.getPassword() != null) {
                try {
                    String passwordProperty = this.getCamelContext().resolvePropertyPlaceholders(new String(keyStoreAndAlias.getPassword()));
                    key = keyStoreAndAlias.getKeyStore().getKey(keyStoreAndAlias.getAlias(), passwordProperty.toCharArray());
                } catch (Exception e) {
                    throw new RuntimeCamelException("Error parsing property value: " + new String(keyStoreAndAlias.getPassword()), e);
                }
            } else {
                key = keyStoreAndAlias.getKeyStore().getKey(keyStoreAndAlias.getAlias(), keyStoreAndAlias.getPassword());
            }
        } catch (UnrecoverableKeyException e) {
            throw new KeySelectorException(e);
        } catch (KeyStoreException e) {
            throw new KeySelectorException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new KeySelectorException(e);
        }
        return getKeySelectorResult(key);
    } else {
        throw new IllegalStateException("Purpose " + purpose + " not supported");
    }
}
Also used : KeySelectorException(javax.xml.crypto.KeySelectorException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Key(java.security.Key) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) KeySelectorException(javax.xml.crypto.KeySelectorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) Certificate(java.security.cert.Certificate)

Example 75 with NoSuchAlgorithmException

use of java.security.NoSuchAlgorithmException in project databus by linkedin.

the class RegistrationIdGenerator method generateByteHash.

/**
	 * Generate a hash out of the String id
	 *
	 * @param id
	 * @return
	 */
private static String generateByteHash(String id) {
    try {
        final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();
        messageDigest.update(id.getBytes(Charset.forName("UTF8")));
        final byte[] resultsByte = messageDigest.digest();
        String hash = new String(Hex.encodeHex(resultsByte));
        final int length = 8;
        if (hash.length() > length)
            hash = hash.substring(0, length);
        return hash;
    } catch (NoSuchAlgorithmException nse) {
        LOG.error("Unexpected error : Got NoSuchAlgorithm exception for MD5");
        return "";
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1557 MessageDigest (java.security.MessageDigest)590 IOException (java.io.IOException)374 InvalidKeyException (java.security.InvalidKeyException)266 KeyStoreException (java.security.KeyStoreException)200 CertificateException (java.security.cert.CertificateException)163 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)162 UnsupportedEncodingException (java.io.UnsupportedEncodingException)141 KeyManagementException (java.security.KeyManagementException)130 KeyFactory (java.security.KeyFactory)105 NoSuchProviderException (java.security.NoSuchProviderException)102 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)96 SSLContext (javax.net.ssl.SSLContext)91 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)90 KeyStore (java.security.KeyStore)89 UnrecoverableKeyException (java.security.UnrecoverableKeyException)88 InputStream (java.io.InputStream)82 SecureRandom (java.security.SecureRandom)82 Cipher (javax.crypto.Cipher)79 BadPaddingException (javax.crypto.BadPaddingException)75