Search in sources :

Example 56 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project metrics by dropwizard.

the class PacketWriter method encrypt.

private static EncryptionResult encrypt(byte[] password, ByteBuffer input) {
    final Cipher cipher;
    try {
        cipher = Cipher.getInstance(AES_CYPHER);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sha256(password), AES));
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
        throw new RuntimeException(e);
    }
    final byte[] iv;
    try {
        iv = cipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
    } catch (InvalidParameterSpecException e) {
        throw new RuntimeException(e);
    }
    if (iv.length != IV_LENGTH) {
        throw new IllegalStateException("Bad initialization vector");
    }
    final ByteBuffer output = ByteBuffer.allocate(input.remaining() * 2);
    try {
        cipher.doFinal(input, output);
    } catch (ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        throw new RuntimeException(e);
    }
    return new EncryptionResult(iv, (ByteBuffer) output.flip());
}
Also used : NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) ByteBuffer(java.nio.ByteBuffer) SecretKeySpec(javax.crypto.spec.SecretKeySpec) ShortBufferException(javax.crypto.ShortBufferException) Cipher(javax.crypto.Cipher) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException)

Example 57 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project cxf by apache.

the class XmlEncOutInterceptor method encryptSymmetricKey.

// Apache Security XMLCipher does not support
// Certificates for encrypting the keys
protected byte[] encryptSymmetricKey(byte[] keyBytes, X509Certificate remoteCert, String keyEncAlgo, String digestAlgo) throws WSSecurityException {
    Cipher cipher = EncryptionUtils.initCipherWithCert(keyEncAlgo, digestAlgo, Cipher.ENCRYPT_MODE, remoteCert);
    int blockSize = cipher.getBlockSize();
    if (blockSize > 0 && blockSize < keyBytes.length) {
        String message = "Public key algorithm too weak to encrypt symmetric key";
        LOG.severe(message);
        throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, "unsupportedKeyTransp", new Object[] { message });
    }
    byte[] encryptedEphemeralKey = null;
    try {
        encryptedEphemeralKey = cipher.doFinal(keyBytes);
    } catch (IllegalStateException | IllegalBlockSizeException | BadPaddingException ex) {
        throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_ENCRYPTION, ex);
    }
    return encryptedEphemeralKey;
}
Also used : IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) XMLCipher(org.apache.xml.security.encryption.XMLCipher) Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException)

Example 58 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project knime-core by knime.

the class DBDialogPane method saveSettingsTo.

/**
 * Save settings.
 * @param settings to save into
 * @param credProvider a credentials provider, must not be <code>null</code>
 * @throws InvalidSettingsException if the connection could not be validated
 */
public void saveSettingsTo(final NodeSettingsWO settings, final CredentialsProvider credProvider) throws InvalidSettingsException {
    DatabaseConnectionSettings s = new DatabaseConnectionSettings();
    String driverName = m_driver.getSelectedItem().toString();
    s.setDriver(driverName);
    String url = m_db.getEditor().getItem().toString();
    s.setJDBCUrl(url);
    boolean useCredential = m_credCheckBox.isSelected();
    if (useCredential) {
        s.setCredentialName((String) m_credBox.getSelectedItem());
    } else {
        s.setUserName(m_user.getText().trim());
        if (m_passwordChanged) {
            try {
                s.setPassword(KnimeEncryption.encrypt(m_pass.getPassword()));
            } catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException | UnsupportedEncodingException ex) {
                LOGGER.error("Could not encrypt password, reason: " + ex.getMessage(), ex);
            }
        } else {
            s.setPassword(new String(m_pass.getPassword()));
        }
    }
    if (m_noCorrectionTZ.isSelected()) {
        s.setTimezone("none");
    } else if (m_currentTZ.isSelected()) {
        s.setTimezone("current");
    } else {
        final String timezone = (String) m_timezone.getSelectedItem();
        s.setTimezone(timezone);
    }
    s.setAllowSpacesInColumnNames(m_allowSpacesInColumnNames.isSelected());
    s.setValidateConnection(m_validateConnection.isSelected());
    s.setRetrieveMetadataInConfigure(m_retrieveMetadataInConfigure.isSelected());
    if (s.getValidateConnection()) {
        try {
            s.execute(credProvider, conn -> {
                return conn != null;
            });
        } catch (SQLException ex) {
            Throwable cause = ExceptionUtils.getRootCause(ex);
            if (cause == null) {
                cause = ex;
            }
            throw new InvalidSettingsException("Database connection could not be validated: " + cause.getMessage(), ex);
        }
    }
    s.saveConnection(settings);
}
Also used : DatabaseConnectionSettings(org.knime.core.node.port.database.DatabaseConnectionSettings) SQLException(java.sql.SQLException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 59 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project cassandra by apache.

the class EncryptionUtils method decrypt.

/**
     * Decrypt the input data, as well as manage sizing of the {@code outputBuffer}; if the buffer is not big enough,
     * deallocate current, and allocate a large enough buffer.
     *
     * @return the byte buffer that was actaully written to; it may be the {@code outputBuffer} if it had enough capacity,
     * or it may be a new, larger instance. Callers should capture the return buffer (if calling multiple times).
     */
public static ByteBuffer decrypt(ReadableByteChannel channel, ByteBuffer outputBuffer, boolean allowBufferResize, Cipher cipher) throws IOException {
    ByteBuffer metadataBuffer = reusableBuffers.get();
    if (metadataBuffer.capacity() < ENCRYPTED_BLOCK_HEADER_SIZE) {
        metadataBuffer = ByteBufferUtil.ensureCapacity(metadataBuffer, ENCRYPTED_BLOCK_HEADER_SIZE, true);
        reusableBuffers.set(metadataBuffer);
    }
    metadataBuffer.position(0).limit(ENCRYPTED_BLOCK_HEADER_SIZE);
    channel.read(metadataBuffer);
    if (metadataBuffer.remaining() < ENCRYPTED_BLOCK_HEADER_SIZE)
        throw new IllegalStateException("could not read encrypted blocked metadata header");
    int encryptedLength = metadataBuffer.getInt();
    // this is the length of the compressed data
    int plainTextLength = metadataBuffer.getInt();
    outputBuffer = ByteBufferUtil.ensureCapacity(outputBuffer, Math.max(plainTextLength, encryptedLength), allowBufferResize);
    outputBuffer.position(0).limit(encryptedLength);
    channel.read(outputBuffer);
    ByteBuffer dupe = outputBuffer.duplicate();
    dupe.clear();
    try {
        cipher.doFinal(outputBuffer, dupe);
    } catch (ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        throw new IOException("failed to decrypt commit log block", e);
    }
    dupe.position(0).limit(plainTextLength);
    return dupe;
}
Also used : ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 60 with IllegalBlockSizeException

use of javax.crypto.IllegalBlockSizeException in project otter by alibaba.

the class AESUtils method decrypt.

/**
     * 解密byte数据
     * 
     * @param encrypted
     * @return
     * @throws AESException
     */
public byte[] decrypt(byte[] encrypted) throws AESException {
    try {
        SecretKeySpec skeySpec = new SecretKeySpec(secretKey, ENCRYPTION_ALGORITHM);
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        return cipher.doFinal(encrypted);
    } catch (NoSuchAlgorithmException e) {
        throw new AESException(e);
    } catch (NoSuchPaddingException e) {
        throw new AESException(e);
    } catch (InvalidKeyException e) {
        throw new AESException(e);
    } catch (IllegalBlockSizeException e) {
        throw new AESException(e);
    } catch (BadPaddingException e) {
        throw new AESException(e);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) 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

IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)118 BadPaddingException (javax.crypto.BadPaddingException)103 InvalidKeyException (java.security.InvalidKeyException)83 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)70 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)69 Cipher (javax.crypto.Cipher)59 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)46 IOException (java.io.IOException)40 IvParameterSpec (javax.crypto.spec.IvParameterSpec)27 SecretKey (javax.crypto.SecretKey)26 UnrecoverableKeyException (java.security.UnrecoverableKeyException)25 CertificateException (java.security.cert.CertificateException)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)25 KeyStoreException (java.security.KeyStoreException)24 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)17 RemoteException (android.os.RemoteException)15 ShortBufferException (javax.crypto.ShortBufferException)14 AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)13 KeyGenerator (javax.crypto.KeyGenerator)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12