Example usage for javax.crypto IllegalBlockSizeException getMessage

List of usage examples for javax.crypto IllegalBlockSizeException getMessage

Introduction

In this page you can find the example usage for javax.crypto IllegalBlockSizeException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:org.mycontroller.standalone.auth.McCrypt.java

public static String decrypt(String value) {
    try {//w ww. j a v a  2  s  .  c  o  m
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, generateKey());
        byte[] decryptedValue64 = Base64.decodeBase64(value);
        byte[] decryptedByteValue = cipher.doFinal(decryptedValue64);
        String decryptedValue = new String(decryptedByteValue, "UTF-8");
        return decryptedValue;
    } catch (IllegalBlockSizeException ex) {
        _logger.warn("Exception: '{}'", ex.getMessage());
        _logger.debug("Exception, ", ex);
    } catch (Exception ex) {
        _logger.error("Exception, ", ex);
    }
    return null;
}

From source file:com.mb.framework.util.SecurityUtil.java

/**
 * /*from  w w  w.  jav  a2 s  . com*/
 * This method is used for decrypt by using Algorithm - AES/CBC/PKCS5Padding
 * 
 * @param String
 * @return String
 * @throws Exception
 */
@SuppressWarnings("static-access")
public static String decryptAESPBKDF2(String encryptedText) throws Exception {

    byte[] saltBytes = salt.getBytes("UTF-8");
    byte[] encryptedTextBytes = new Base64().decodeBase64(encryptedText);

    // Derive the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), saltBytes, pswdIterations, keySize);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

    // Decrypt the message
    Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING_ALGO);
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes));

    byte[] decryptedTextBytes = null;
    try {
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);
    } catch (IllegalBlockSizeException e) {

        LOGGER.error("error " + e.getMessage());
    } catch (BadPaddingException e) {
        LOGGER.error("error " + e.getMessage());
    }

    return new String(decryptedTextBytes);
}

From source file:com.clustercontrol.util.KeyCheck.java

private static byte[] encrypt(byte[] source, PrivateKey privateKey) throws HinemosUnknown {
    try {/*from w ww. j  av  a  2 s.c o m*/
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return cipher.doFinal(source);
    } catch (IllegalBlockSizeException ex) {
        m_log.warn(ex.getMessage(), ex);
    } catch (BadPaddingException ex) {
        m_log.warn(ex.getMessage(), ex);
    } catch (InvalidKeyException ex) {
        m_log.warn(ex.getMessage(), ex);
    } catch (NoSuchAlgorithmException ex) {
        m_log.warn(ex.getMessage(), ex);
    } catch (NoSuchPaddingException ex) {
        m_log.warn(ex.getMessage(), ex);
    }
    throw new HinemosUnknown("encrypt error");
}

From source file:com.clustercontrol.util.KeyCheck.java

private static byte[] decrypt(byte[] source, PublicKey publicKey) throws HinemosUnknown {
    m_log.trace("decrypt=" + source.length);
    try {//from  w  w  w  . j  a  v  a 2  s. co  m
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        return cipher.doFinal(source);
    } catch (IllegalBlockSizeException ex) {
        m_log.warn(ex.getMessage(), ex);
    } catch (BadPaddingException ex) {
        m_log.warn(ex.getMessage(), ex);
    } catch (InvalidKeyException ex) {
        m_log.warn(ex.getMessage(), ex);
    } catch (NoSuchAlgorithmException ex) {
        m_log.warn(ex.getMessage(), ex);
    } catch (NoSuchPaddingException ex) {
        m_log.warn(ex.getMessage(), ex);
    }
    throw new HinemosUnknown("decrypt error");
}

From source file:com.cws.esolutions.security.utils.PasswordUtils.java

/**
 * Provides two-way (reversible) encryption of a provided string. Can be used where reversibility
 * is required but encryption (obfuscation, technically) is required.
 *
 * @param value - The plain text data to encrypt
 * @param salt - The salt value to utilize for the request
 * @param secretInstance - The cryptographic instance to use for the SecretKeyFactory
 * @param iterations - The number of times to loop through the keyspec
 * @param keyBits - The size of the key, in bits
 * @param algorithm - The algorithm to encrypt the data with
 * @param cipherInstance - The cipher instance to utilize
 * @param encoding - The text encoding/*w  w w .  j  ava2  s.  co m*/
 * @return The encrypted string in a reversible format
 * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing
 */
public static final String decryptText(final String value, final String salt, final String secretInstance,
        final int iterations, final int keyBits, final String algorithm, final String cipherInstance,
        final String encoding) throws SecurityException {
    final String methodName = PasswordUtils.CNAME
            + "#encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", secretInstance);
        DEBUGGER.debug("Value: {}", iterations);
        DEBUGGER.debug("Value: {}", keyBits);
        DEBUGGER.debug("Value: {}", algorithm);
        DEBUGGER.debug("Value: {}", cipherInstance);
        DEBUGGER.debug("Value: {}", encoding);
    }

    String decPass = null;

    try {
        String decoded = new String(Base64.getDecoder().decode(value));
        String iv = decoded.split(":")[0];
        String property = decoded.split(":")[1];

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretInstance);
        PBEKeySpec keySpec = new PBEKeySpec(salt.toCharArray(), salt.getBytes(), iterations, keyBits);
        SecretKey keyTmp = keyFactory.generateSecret(keySpec);
        SecretKeySpec sks = new SecretKeySpec(keyTmp.getEncoded(), algorithm);

        Cipher pbeCipher = Cipher.getInstance(cipherInstance);
        pbeCipher.init(Cipher.DECRYPT_MODE, sks, new IvParameterSpec(Base64.getDecoder().decode(iv)));
        decPass = new String(pbeCipher.doFinal(Base64.getDecoder().decode(property)), encoding);
    } catch (InvalidKeyException ikx) {
        throw new SecurityException(ikx.getMessage(), ikx);
    } catch (NoSuchAlgorithmException nsx) {
        throw new SecurityException(nsx.getMessage(), nsx);
    } catch (NoSuchPaddingException npx) {
        throw new SecurityException(npx.getMessage(), npx);
    } catch (IllegalBlockSizeException ibx) {
        throw new SecurityException(ibx.getMessage(), ibx);
    } catch (BadPaddingException bpx) {
        throw new SecurityException(bpx.getMessage(), bpx);
    } catch (UnsupportedEncodingException uex) {
        throw new SecurityException(uex.getMessage(), uex);
    } catch (InvalidAlgorithmParameterException iapx) {
        throw new SecurityException(iapx.getMessage(), iapx);
    } catch (InvalidKeySpecException iksx) {
        throw new SecurityException(iksx.getMessage(), iksx);
    }

    return decPass;
}

From source file:com.cws.esolutions.security.utils.PasswordUtils.java

/**
 * Provides two-way (reversible) encryption of a provided string. Can be used where reversibility
 * is required but encryption (obfuscation, technically) is required.
 *
 * @param value - The plain text data to encrypt
 * @param salt - The salt value to utilize for the request
 * @param secretInstance - The cryptographic instance to use for the SecretKeyFactory
 * @param iterations - The number of times to loop through the keyspec
 * @param keyBits - The size of the key, in bits
 * @param algorithm - The algorithm to encrypt the data with
 * @param cipherInstance - The cipher instance to utilize
 * @param encoding - The text encoding/*  ww w . j  a v a 2 s  .  co  m*/
 * @return The encrypted string in a reversible format
 * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing
 */
public static final String encryptText(final String value, final String salt, final String secretInstance,
        final int iterations, final int keyBits, final String algorithm, final String cipherInstance,
        final String encoding) throws SecurityException {
    final String methodName = PasswordUtils.CNAME
            + "#encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", secretInstance);
        DEBUGGER.debug("Value: {}", iterations);
        DEBUGGER.debug("Value: {}", keyBits);
        DEBUGGER.debug("Value: {}", algorithm);
        DEBUGGER.debug("Value: {}", cipherInstance);
        DEBUGGER.debug("Value: {}", encoding);
    }

    String encPass = null;

    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretInstance);
        PBEKeySpec keySpec = new PBEKeySpec(salt.toCharArray(), salt.getBytes(), iterations, keyBits);
        SecretKey keyTmp = keyFactory.generateSecret(keySpec);
        SecretKeySpec sks = new SecretKeySpec(keyTmp.getEncoded(), algorithm);

        Cipher pbeCipher = Cipher.getInstance(cipherInstance);
        pbeCipher.init(Cipher.ENCRYPT_MODE, sks);

        AlgorithmParameters parameters = pbeCipher.getParameters();
        IvParameterSpec ivParameterSpec = parameters.getParameterSpec(IvParameterSpec.class);

        byte[] cryptoText = pbeCipher.doFinal(value.getBytes(encoding));
        byte[] iv = ivParameterSpec.getIV();

        String combined = Base64.getEncoder().encodeToString(iv) + ":"
                + Base64.getEncoder().encodeToString(cryptoText);

        encPass = Base64.getEncoder().encodeToString(combined.getBytes());
    } catch (InvalidKeyException ikx) {
        throw new SecurityException(ikx.getMessage(), ikx);
    } catch (NoSuchAlgorithmException nsx) {
        throw new SecurityException(nsx.getMessage(), nsx);
    } catch (NoSuchPaddingException npx) {
        throw new SecurityException(npx.getMessage(), npx);
    } catch (IllegalBlockSizeException ibx) {
        throw new SecurityException(ibx.getMessage(), ibx);
    } catch (BadPaddingException bpx) {
        throw new SecurityException(bpx.getMessage(), bpx);
    } catch (UnsupportedEncodingException uex) {
        throw new SecurityException(uex.getMessage(), uex);
    } catch (InvalidKeySpecException iksx) {
        throw new SecurityException(iksx.getMessage(), iksx);
    } catch (InvalidParameterSpecException ipsx) {
        throw new SecurityException(ipsx.getMessage(), ipsx);
    }

    return encPass;
}

From source file:org.openntf.xpt.core.dss.encryption.EncryptionFactory.java

public static String decrypt(String strHash, SecretKeySpec key) throws DSSException {
    String strRC = "";
    try {/*from   ww w .  jav  a2s.  c om*/
        Cipher aes = Cipher.getInstance("AES");
        byte[] ciphertext = Base64.decodeBase64(strHash);
        aes.init(Cipher.DECRYPT_MODE, key);
        strRC = new String(aes.doFinal(ciphertext));
    } catch (IllegalBlockSizeException e) {
        //System.out.println("- BlockSize : Not encrypted Values in Store - ");
        throw new DSSException("- BlockSize : Not encrypted Values in Store -");
    } catch (BadPaddingException e) {
        //System.out.println("- BadPadding : try clean build -");
        throw new DSSException("- BadPadding : try clean build -");
    } catch (Exception e) {
        //e.printStackTrace();
        throw new DSSException(e.getMessage());
    }
    return strRC;

}

From source file:org.kitodo.data.encryption.DesEncrypter.java

/**
 * Decrypt a encrypted string.//  w ww  . java  2 s.c o  m
 *
 * @param messageToDecrypt
 *            String to decrypt
 * @return decrypted string or null on error
 */
public String decrypt(String messageToDecrypt) {

    try {
        byte[] dec = Base64.decodeBase64(messageToDecrypt.getBytes(StandardCharsets.UTF_8));
        byte[] utfEight = decryptionCipher.doFinal(dec);
        return new String(utfEight, StandardCharsets.UTF_8);
    } catch (IllegalBlockSizeException e) {
        logger.warn("Catched IllegalBlockSizeException with message: " + e.getMessage());
    } catch (BadPaddingException e) {
        logger.warn("Catched BadPaddingException with message: " + e.getMessage());
    }

    return null;
}

From source file:org.kitodo.production.security.password.SecurityPasswordEncoder.java

/**
 * Decrypt a encrypted string.//from   ww w.  j av a  2 s  .  c  o m
 *
 * @param messageToDecrypt
 *            String to decrypt
 * @return decrypted string or null on error
 */
public String decrypt(String messageToDecrypt) {
    try {
        byte[] dec = Base64.decodeBase64(messageToDecrypt.getBytes(StandardCharsets.UTF_8));
        byte[] utfEight = decryptionCipher.doFinal(dec);
        return new String(utfEight, StandardCharsets.UTF_8);
    } catch (IllegalBlockSizeException e) {
        logger.warn("Catched IllegalBlockSizeException with message: " + e.getMessage());
    } catch (BadPaddingException e) {
        logger.warn("Catched BadPaddingException with message: " + e.getMessage());
    }
    return null;
}

From source file:tds.itemrenderer.security.Encryption.java

private synchronized String encrypt(final String stringToEncrypt) {
    try {/*from   w  w  w. j  a  v a  2s  .  co m*/
        byte[] plainBytes = stringToEncrypt.getBytes(UTF_8);
        byte[] encryptedBytes = encryptCipher.doFinal(plainBytes);
        byte[] encryptIv = encryptCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
        byte[] cipherText = new byte[encryptedBytes.length + encryptIv.length];
        System.arraycopy(encryptIv, 0, cipherText, 0, encryptIv.length);
        System.arraycopy(encryptedBytes, 0, cipherText, encryptIv.length, encryptedBytes.length);
        return DatatypeConverter.printBase64Binary(cipherText);
    } catch (IllegalBlockSizeException e) {
        log.error("Encyption.encrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Block Size is not valid");
    } catch (BadPaddingException e) {
        log.error("Encyption.encrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Padding is not valid");
    } catch (InvalidParameterSpecException e) {
        log.error("Encyption.encrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Parameter Sepcification is not valid");
    }
}