Search in sources :

Example 66 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project triplea by triplea-game.

the class RsaAuthenticator method decryptPasswordForAction.

/**
 * Decrypts the password contained in the specified response and provides it to the specified action for further
 * processing.
 *
 * @param response The response map containing the encrypted password.
 * @param action A {@link Function} which is executed if the password is successfully decrypted.
 *
 * @return The result of {@code action} if the password is decrypted successfully; otherwise a message describing the
 *         error that occurred during decryption.
 *
 * @throws IllegalStateException If the encryption cipher is not available.
 */
String decryptPasswordForAction(final Map<String, String> response, final Function<String, String> action) {
    final String encryptedPassword = response.get(ENCRYPTED_PASSWORD_KEY);
    try {
        final Cipher cipher = Cipher.getInstance(RSA_ECB_OAEPP);
        cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
        return action.apply(new String(cipher.doFinal(Base64.getDecoder().decode(encryptedPassword)), StandardCharsets.UTF_8));
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
        throw new IllegalStateException(e);
    } catch (final InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
        return e.getMessage();
    }
}
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 67 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project incubator-pulsar by apache.

the class MessageCrypto method addPublicKeyCipher.

private void addPublicKeyCipher(String keyName, CryptoKeyReader keyReader) throws CryptoException {
    if (keyName == null || keyReader == null) {
        throw new PulsarClientException.CryptoException("Keyname or KeyReader is null");
    }
    // Read the public key and its info using callback
    EncryptionKeyInfo keyInfo = keyReader.getPublicKey(keyName, null);
    PublicKey pubKey;
    try {
        pubKey = loadPublicKey(keyInfo.getKey());
    } catch (Exception e) {
        String msg = logCtx + "Failed to load public key " + keyName + ". " + e.getMessage();
        log.error(msg);
        throw new PulsarClientException.CryptoException(msg);
    }
    Cipher dataKeyCipher = null;
    byte[] encryptedKey;
    try {
        // Encrypt data key using public key
        if (RSA.equals(pubKey.getAlgorithm())) {
            dataKeyCipher = Cipher.getInstance(RSA_TRANS, BouncyCastleProvider.PROVIDER_NAME);
        } else if (ECDSA.equals(pubKey.getAlgorithm())) {
            dataKeyCipher = Cipher.getInstance(ECIES, BouncyCastleProvider.PROVIDER_NAME);
        } else {
            String msg = logCtx + "Unsupported key type " + pubKey.getAlgorithm() + " for key " + keyName;
            log.error(msg);
            throw new PulsarClientException.CryptoException(msg);
        }
        dataKeyCipher.init(Cipher.ENCRYPT_MODE, pubKey);
        encryptedKey = dataKeyCipher.doFinal(dataKey.getEncoded());
    } catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | InvalidKeyException e) {
        log.error("{} Failed to encrypt data key {}. {}", logCtx, keyName, e.getMessage());
        throw new PulsarClientException.CryptoException(e.getMessage());
    }
    EncryptionKeyInfo eki = new EncryptionKeyInfo(encryptedKey, keyInfo.getMetadata());
    encryptedDataKeyMap.put(keyName, eki);
}
Also used : BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) PublicKey(java.security.PublicKey) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) EncryptionKeyInfo(org.apache.pulsar.client.api.EncryptionKeyInfo) ByteString(com.google.protobuf.ByteString) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) CryptoException(org.apache.pulsar.client.api.PulsarClientException.CryptoException) PEMException(org.bouncycastle.openssl.PEMException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchProviderException(java.security.NoSuchProviderException) CryptoException(org.apache.pulsar.client.api.PulsarClientException.CryptoException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) Cipher(javax.crypto.Cipher) CryptoException(org.apache.pulsar.client.api.PulsarClientException.CryptoException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 68 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project api-framework by vinscom.

the class SecurityTools method encrypt.

public String encrypt(String value) {
    try {
        byte[] riv = new byte[16];
        mRandom.nextBytes(riv);
        IvParameterSpec iv = new IvParameterSpec(riv);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, getKeySpec(), iv);
        byte[] encrypted = cipher.doFinal(value.getBytes());
        return Base64.getEncoder().encodeToString(concatenateByteArrays(encrypted, riv));
    } catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InterruptedException | ExecutionException ex) {
        getLog().error(ex);
    }
    return null;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) 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) ExecutionException(java.util.concurrent.ExecutionException)

Example 69 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project api-framework by vinscom.

the class SecurityTools method decrypt.

public String decrypt(String encrypted) {
    try {
        byte[] data = Base64.getDecoder().decode(encrypted);
        IvParameterSpec iv = new IvParameterSpec(data, data.length - 16, 16);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, getKeySpec(), iv);
        byte[] original = cipher.doFinal(data, 0, data.length - 16);
        return new String(original);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | InterruptedException | ExecutionException ex) {
        getLog().error(ex);
    }
    return null;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) ExecutionException(java.util.concurrent.ExecutionException)

Example 70 with NoSuchPaddingException

use of javax.crypto.NoSuchPaddingException in project cordova-plugin-android-fingerprint-auth by mjwheatley.

the class FingerprintAuth method initialize.

/**
 * Sets the context of the Command. This can then be used to do things like
 * get file paths associated with the Activity.
 *
 * @param cordova The context of the main Activity.
 * @param webView The CordovaWebView Cordova is running in.
 */
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
    super.initialize(cordova, webView);
    Log.v(TAG, "Init FingerprintAuth");
    packageName = cordova.getActivity().getApplicationContext().getPackageName();
    mPluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
    mActivity = cordova.getActivity();
    mContext = cordova.getActivity().getApplicationContext();
    if (android.os.Build.VERSION.SDK_INT < 23) {
        return;
    }
    mKeyguardManager = cordova.getActivity().getSystemService(KeyguardManager.class);
    mFingerPrintManager = cordova.getActivity().getApplicationContext().getSystemService(FingerprintManager.class);
    try {
        mKeyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, ANDROID_KEY_STORE);
        mKeyStore = KeyStore.getInstance(ANDROID_KEY_STORE);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Failed to get an instance of KeyGenerator", e);
    } catch (NoSuchProviderException e) {
        throw new RuntimeException("Failed to get an instance of KeyGenerator", e);
    } catch (KeyStoreException e) {
        throw new RuntimeException("Failed to get an instance of KeyStore", e);
    }
    try {
        mCipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/" + KeyProperties.BLOCK_MODE_CBC + "/" + KeyProperties.ENCRYPTION_PADDING_PKCS7);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("Failed to get an instance of Cipher", e);
    } catch (NoSuchPaddingException e) {
        throw new RuntimeException("Failed to get an instance of Cipher", e);
    }
}
Also used : PluginResult(org.apache.cordova.PluginResult) FingerprintManager(android.hardware.fingerprint.FingerprintManager) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) NoSuchProviderException(java.security.NoSuchProviderException) KeyguardManager(android.app.KeyguardManager)

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