Search in sources :

Example 86 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project HL4A by HL4A.

the class AES method encrypt.

private byte[] encrypt(String cmp, SecretKey sk, IvParameterSpec IV, byte[] msg) {
    try {
        Cipher c = Cipher.getInstance(cmp);
        c.init(Cipher.ENCRYPT_MODE, sk, IV);
        return c.doFinal(msg);
    } catch (NoSuchAlgorithmException nsae) {
        Log.e("AESdemo", "no cipher getinstance support for " + cmp);
    } catch (NoSuchPaddingException nspe) {
        Log.e("AESdemo", "no cipher getinstance support for padding " + cmp);
    } catch (InvalidKeyException e) {
        Log.e("AESdemo", "invalid key exception");
    } catch (InvalidAlgorithmParameterException e) {
        Log.e("AESdemo", "invalid algorithm parameter exception");
    } catch (IllegalBlockSizeException e) {
        Log.e("AESdemo", "illegal block size exception");
    } catch (BadPaddingException e) {
        Log.e("AESdemo", "bad padding exception");
    }
    return null;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 87 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project spring-security by spring-projects.

the class CryptoAssumptions method assumeAes256.

private static void assumeAes256(CipherAlgorithm cipherAlgorithm) {
    boolean aes256Available = false;
    try {
        Cipher.getInstance(cipherAlgorithm.toString());
        aes256Available = Cipher.getMaxAllowedKeyLength("AES") >= 256;
    } catch (NoSuchAlgorithmException ex) {
        throw new TestAbortedException(cipherAlgorithm + " not available, skipping test", ex);
    } catch (NoSuchPaddingException ex) {
        throw new TestAbortedException(cipherAlgorithm + " padding not available, skipping test", ex);
    }
    Assumptions.assumeTrue(aes256Available, "AES key length of 256 not allowed, skipping test");
}
Also used : TestAbortedException(org.opentest4j.TestAbortedException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 88 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project orientdb by orientechnologies.

the class OSymmetricKey method encrypt.

/**
 * This method encrypts an array of bytes.
 *
 * @param transform The cipher transformation to use.
 * @param bytes The array of bytes to be encrypted.
 *
 * @return The encrypted bytes as a Base64-encoded JSON document or null if unsuccessful.
 */
public String encrypt(final String transform, final byte[] bytes) {
    String encodedJSON = null;
    if (secretKey == null)
        throw new OSecurityException("OSymmetricKey.encrypt() SecretKey is null");
    if (transform == null)
        throw new OSecurityException("OSymmetricKey.encrypt() Cannot determine cipher transformation");
    try {
        // Throws NoSuchAlgorithmException and NoSuchPaddingException.
        Cipher cipher = Cipher.getInstance(transform);
        // If the cipher transformation requires an initialization vector then init() will create a random one.
        // (Use cipher.getIV() to retrieve the IV, if it exists.)
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        // If the cipher does not use an IV, this will be null.
        byte[] initVector = cipher.getIV();
        // byte[] initVector = encCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
        byte[] encrypted = cipher.doFinal(bytes);
        encodedJSON = encodeJSON(encrypted, initVector);
    } catch (Exception ex) {
        throw new OSecurityException("OSymmetricKey.encrypt() Exception: " + ex.getMessage());
    }
    return encodedJSON;
}
Also used : OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) Cipher(javax.crypto.Cipher) OException(com.orientechnologies.common.exception.OException) KeyStoreException(java.security.KeyStoreException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 89 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project LuaViewSDK by alibaba.

the class DecryptUtil method aes.

/**
 * 使用aes256进行解密
 *
 * @param encrypted
 * @return
 */
public static byte[] aes(final byte[] keys, final byte[] encrypted) {
    try {
        // get cipher
        Cipher cipher = AppCache.getCache(CACHE_PUBLIC_KEY).get(Constants.PUBLIC_KEY_PATH_CIPHER);
        if (cipher == null) {
            final SecretKeySpec skeySpec = new SecretKeySpec(keys, ALGORITHM_AES);
            final IvParameterSpec ivParameterSpec = new IvParameterSpec(cIv);
            cipher = Cipher.getInstance(ALGORITHM_AES);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
            // cache cipher
            AppCache.getCache(CACHE_PUBLIC_KEY).put(Constants.PUBLIC_KEY_PATH_CIPHER, cipher);
        }
        return cipher.doFinal(encrypted);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 90 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project jgnash by ccavanaugh.

the class EncryptionManager method decrypt.

/**
 * Decrypts the supplied string.
 *
 * @param encrypted String to decrypt
 * @return The decrypted string of {@code DECRYPTION_ERROR_TAG} if decryption fails
 * @see #DECRYPTION_ERROR_TAG
 */
public String decrypt(final String encrypted) {
    try {
        final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(Base64.getDecoder().decode(encrypted)), StandardCharsets.UTF_8);
    } catch (final InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
        logger.log(Level.SEVERE, "Invalid password");
        return DECRYPTION_ERROR_TAG;
    }
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

NoSuchPaddingException (javax.crypto.NoSuchPaddingException)259 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)237 InvalidKeyException (java.security.InvalidKeyException)216 Cipher (javax.crypto.Cipher)187 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)181 BadPaddingException (javax.crypto.BadPaddingException)180 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)119 SecretKeySpec (javax.crypto.spec.SecretKeySpec)91 IOException (java.io.IOException)83 IvParameterSpec (javax.crypto.spec.IvParameterSpec)66 SecretKey (javax.crypto.SecretKey)45 KeyStoreException (java.security.KeyStoreException)40 CertificateException (java.security.cert.CertificateException)40 UnrecoverableKeyException (java.security.UnrecoverableKeyException)35 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)30 UnsupportedEncodingException (java.io.UnsupportedEncodingException)27 NoSuchProviderException (java.security.NoSuchProviderException)27 GCMParameterSpec (javax.crypto.spec.GCMParameterSpec)18 FileNotFoundException (java.io.FileNotFoundException)16 SecureRandom (java.security.SecureRandom)16