use of javax.crypto.BadPaddingException in project tech by ffyyhh995511.
the class RASUtil method encrypt.
/**
* 公钥加密过程
*
* @param publicKey
* 公钥
* @param plainTextData
* 明文数据
* @return
* @throws Exception
* 加密过程中的异常信息
*/
public static byte[] encrypt(RSAPublicKey publicKey, byte[] plainTextData) throws Exception {
if (publicKey == null) {
throw new Exception("加密公钥为空, 请设置");
}
Cipher cipher = null;
try {
// 使用默认RSA
cipher = Cipher.getInstance("RSA");
// cipher= Cipher.getInstance("RSA", new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] output = cipher.doFinal(plainTextData);
return output;
} catch (NoSuchAlgorithmException e) {
throw new Exception("无此加密算法");
} catch (NoSuchPaddingException e) {
e.printStackTrace();
return null;
} catch (InvalidKeyException e) {
throw new Exception("加密公钥非法,请检查");
} catch (IllegalBlockSizeException e) {
throw new Exception("明文长度非法");
} catch (BadPaddingException e) {
throw new Exception("明文数据已损坏");
}
}
use of javax.crypto.BadPaddingException in project Protocol-Adapter-OSLP by OSGP.
the class OslpUtils method validateEncryptedHash.
private static boolean validateEncryptedHash(final byte[] message, final byte[] securityKey, final PublicKey publicKey) throws GeneralSecurityException {
// Calculate hash of message
final byte[] verifyHash = createHash(message);
try {
// Decrypt security key hash
final Cipher cipher = Cipher.getInstance(FALLBACK_CIPHER);
cipher.init(Cipher.DECRYPT_MODE, publicKey);
final byte[] messageHash = cipher.doFinal(securityKey);
// Verify calculated and received hash
return Arrays.equals(messageHash, verifyHash);
} catch (final BadPaddingException e) {
LOGGER.error("unexpected exception", e);
return false;
}
}
use of javax.crypto.BadPaddingException 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.BadPaddingException 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.BadPaddingException 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);
}
Aggregations