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());
}
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;
}
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);
}
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;
}
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);
}
}
Aggregations