Search in sources :

Example 76 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project core-ng-project by neowu.

the class RSA method decrypt.

public byte[] decrypt(byte[] encryptedMessage) {
    try {
        Cipher cipher = Cipher.getInstance(ALGORITHM_RSA);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(encryptedMessage);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
        throw new Error("failed to decrypt message, please check private key and message", e);
    } catch (InvalidKeyException e) {
        throw new Error(e);
    }
}
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)

Example 77 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project android-fingerprint-authentication by multidots.

the class FingerPrintAuthHelper method cipherInit.

/**
 * Initialize the cipher.
 *
 * @return true if the initialization is successful.
 */
@TargetApi(23)
private boolean cipherInit() {
    boolean isKeyGenerated = generateKey();
    if (!isKeyGenerated) {
        mCallback.onAuthFailed(AuthErrorCodes.NON_RECOVERABLE_ERROR, ERROR_FAILED_TO_GENERATE_KEY);
        return false;
    }
    try {
        mCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        mCallback.onAuthFailed(AuthErrorCodes.NON_RECOVERABLE_ERROR, ERROR_FAILED_TO_GENERATE_KEY);
        return false;
    }
    try {
        mKeyStore.load(null);
        SecretKey key = (SecretKey) mKeyStore.getKey(KEY_NAME, null);
        mCipher.init(Cipher.ENCRYPT_MODE, key);
        return true;
    } catch (KeyPermanentlyInvalidatedException e) {
        mCallback.onAuthFailed(AuthErrorCodes.NON_RECOVERABLE_ERROR, ERROR_FAILED_TO_INIT_CHIPPER);
        return false;
    } catch (KeyStoreException | CertificateException | UnrecoverableKeyException | IOException | NoSuchAlgorithmException | InvalidKeyException e) {
        mCallback.onAuthFailed(AuthErrorCodes.NON_RECOVERABLE_ERROR, ERROR_FAILED_TO_INIT_CHIPPER);
        return false;
    }
}
Also used : KeyPermanentlyInvalidatedException(android.security.keystore.KeyPermanentlyInvalidatedException) SecretKey(javax.crypto.SecretKey) UnrecoverableKeyException(java.security.UnrecoverableKeyException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) InvalidKeyException(java.security.InvalidKeyException) TargetApi(android.annotation.TargetApi)

Example 78 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project ctSESAM-android by pinae.

the class Crypter method encrypt.

public byte[] encrypt(byte[] data, String padding) {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    try {
        Cipher cipher = Cipher.getInstance("AES/CBC/" + padding);
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(this.iv));
        return cipher.doFinal(data);
    } catch (NoSuchAlgorithmException e) {
        Log.d("Encryption error", "AES/CBC is not implemented.");
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        Log.d("Encryption error", padding + " is not implemented.");
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        Log.d("Encryption error", "Invalid IV.");
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        Log.d("Encryption error", "Invalid key.");
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        Log.d("Encryption error", "Illegal block size of the data.");
        e.printStackTrace();
    } catch (BadPaddingException e) {
        Log.d("Encryption error", "Bad padding of the data.");
        e.printStackTrace();
    }
    return new byte[] {};
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecretKeySpec(javax.crypto.spec.SecretKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 79 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project ballerina by ballerina-lang.

the class AESCipherTool method decrypt.

/**
 * This method is used to decrypt a given encrypted and base64 encoded value.
 *
 * @param value Encrypted value to be decrypted.
 * @return Decrypted value.
 */
public String decrypt(String value) throws AESCipherToolException {
    try {
        byte[] decodedByteArray = decodeBase64(value);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(Arrays.copyOfRange(decodedByteArray, 0, IV_SIZE));
        Cipher decryptionCipher = Cipher.getInstance(ALGORITHM_AES_CBC_PKCS5);
        decryptionCipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
        byte[] encryptedByteArray = Arrays.copyOfRange(decodedByteArray, IV_SIZE, decodedByteArray.length);
        return new String(decryptionCipher.doFinal(encryptedByteArray), StandardCharsets.UTF_8);
    } catch (BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException err) {
        log.error("Failed to decrypt value: " + value, err);
        throw new AESCipherToolException(err.getMessage(), err);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 80 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project ballerina by ballerina-lang.

the class AESCipherTool method encrypt.

/**
 * This method is used to encrypt and base64 encode a plain value.
 *
 * @param value Value to be encrypted.
 * @return Encrypted value.
 */
public String encrypt(String value) throws AESCipherToolException {
    try {
        byte[] ivByteArray = getSecureRandomBytes();
        IvParameterSpec ivParameterSpec = new IvParameterSpec(ivByteArray);
        Cipher encryptionCipher = Cipher.getInstance(ALGORITHM_AES_CBC_PKCS5);
        encryptionCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
        byte[] encryptedBytes = encryptionCipher.doFinal(getBytes(value));
        return encodeBase64(appendByteArrays(ivByteArray, encryptedBytes));
    } catch (BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException err) {
        log.error("Failed to encrypt value: " + value, err);
        throw new AESCipherToolException(err.getMessage(), err);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) 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