Search in sources :

Example 56 with BadPaddingException

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("明文数据已损坏");
    }
}
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) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Example 57 with BadPaddingException

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;
    }
}
Also used : Cipher(javax.crypto.Cipher) BadPaddingException(javax.crypto.BadPaddingException)

Example 58 with BadPaddingException

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());
}
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 59 with BadPaddingException

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;
}
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 60 with BadPaddingException

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

Aggregations

BadPaddingException (javax.crypto.BadPaddingException)120 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)103 InvalidKeyException (java.security.InvalidKeyException)80 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)70 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)69 Cipher (javax.crypto.Cipher)53 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)45 IOException (java.io.IOException)39 KeyStoreException (java.security.KeyStoreException)25 UnrecoverableKeyException (java.security.UnrecoverableKeyException)25 CertificateException (java.security.cert.CertificateException)25 SecretKey (javax.crypto.SecretKey)25 IvParameterSpec (javax.crypto.spec.IvParameterSpec)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)23 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)17 RemoteException (android.os.RemoteException)15 ShortBufferException (javax.crypto.ShortBufferException)14 KeyGenerator (javax.crypto.KeyGenerator)13 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 FileNotFoundException (java.io.FileNotFoundException)11