Example usage for javax.crypto Cipher doFinal

List of usage examples for javax.crypto Cipher doFinal

Introduction

In this page you can find the example usage for javax.crypto Cipher doFinal.

Prototype

public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException 

Source Link

Document

Encrypts or decrypts data in a single-part operation, or finishes a multiple-part operation.

Usage

From source file:com.alkacon.opencms.commons.CmsStringCrypter.java

/**
 * Encrypts the given value.<p>//from w  w  w  .  j a v a 2s . c  o  m
 * 
 * @param value the string which should be encrypted
 * @param password the passsword used for encryption, use the same password for decryption
 * @return the encrypted string of the value or null if something went wrong
 */
public static String encrypt(String value, String password) {

    // check if given value is valid
    if (CmsStringUtil.isEmptyOrWhitespaceOnly(value)) {
        if (LOG.isWarnEnabled()) {
            LOG.warn(Messages.get().getBundle().key(Messages.LOG_WARN_INVALID_ENCRYPT_STRING_1, value));
        }
        return null;
    }

    try {

        // create key
        byte[] k = getKey(password);
        Key key = new SecretKeySpec(k, ENCRYPTION);
        Cipher cipher = Cipher.getInstance(ENCRYPTION);
        cipher.init(Cipher.ENCRYPT_MODE, key);

        // encrypt text
        byte[] cleartext = value.getBytes(FORMAT);
        byte[] ciphertext = cipher.doFinal(cleartext);

        // encode with base64 to be used as a url parameter
        BASE64Encoder base64encoder = new BASE64Encoder();
        return CmsEncoder.encode(base64encoder.encode(ciphertext));
    } catch (Exception ex) {
        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_ENCRYPT_0), ex);
        }
    }

    return null;
}

From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java

/**
 * ????//from  w w w. j av  a  2  s. c  o  m
 * <dl>
 * <dt>?
 * <dd>RSA/ECB/PKCS1Padding???????
 * </dl>
 * @param key ?
 * @param input 
 * @return ?
 */
public static byte[] encrypt(final Key key, final byte[] input) {
    try {
        final Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(input);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | IllegalBlockSizeException | BadPaddingException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java

/**
 * ????//from   w  w w. jav a  2 s  .co  m
 * <dl>
 * <dt>?
 * <dd>RSA/ECB/PKCS1Padding???????
 * </dl>
 * @param key ?
 * @param input 
 * @return ?
 */
public static byte[] decrypt(final Key key, final byte[] input) {
    try {
        final Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(input);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | IllegalBlockSizeException | BadPaddingException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:de.pawlidi.openaletheia.utils.CipherUtils.java

/**
 * //from w  ww.  j a v a2s  . c om
 * @param data
 * @param key
 * @return
 */
public static byte[] encrypt(byte[] data, Key key) {
    if (key != null && ArrayUtils.isNotEmpty(data)) {
        try {
            Cipher rsaCipher = Cipher.getInstance(CIPHER_EXTENDED_ALGORITHM);
            rsaCipher.init(Cipher.ENCRYPT_MODE, key);
            return rsaCipher.doFinal(data);
        } catch (Exception e) {
            throw new RuntimeException("Cannot encrypt, " + CIPHER_ALGORITHM + " error", e);
        }
    }
    return null;
}

From source file:com.frame.Conf.Utilidades.java

public static String encrypt(String key, String iv, String cleartext) throws Exception {
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] encrypted = cipher.doFinal(cleartext.getBytes());
    return new String(encodeBase64(encrypted));
}

From source file:net.mobid.codetraq.utils.PasswordProcessor.java

/**
 * Decrypts a text using the <code>passPhrase</code> above and an algorithm supported
 * by your virtual machine implementation. You can change the default algorithm with
 * another algorithm, but please make sure your virtual machine supports it.
 * @param valueToDecrypt - text to decrypt
 * @return a plain text/*w  w w  .  ja  v a 2s .com*/
 */
public static String decryptString(String valueToDecrypt) {
    String output = null;
    try {
        KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterations);
        SecretKey secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm());
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterations);
        cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
        // begin decrypting...
        byte[] encrypted = new Base64().decode(valueToDecrypt);
        byte[] utf8 = cipher.doFinal(encrypted);
        output = new String(utf8, "UTF8");
    } catch (Exception ex) {
        Logger.getLogger(PasswordProcessor.class.getName()).log(Level.SEVERE, null, ex);
    }
    return output;
}

From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java

/**
 * ???//from   w ww . j a  v  a  2s  . c  o  m
 * <dl>
 * <dt>?
 * <dd>AES/CBC/PKCS5Padding???????
 * </dl>
 * @param key ?
 * @param iv ?
 * @param input 
 * @return ?
 */
public static byte[] encrypt(final Key key, final IvParameterSpec iv, final byte[] input) {
    try {
        final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);
        return cipher.doFinal(input);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:com.github.aynu.yukar.framework.util.SecurityHelper.java

/**
 * ???/*from  w w  w  .  j a va 2s  . c  o m*/
 * <dl>
 * <dt>?
 * <dd>AES/CBC/PKCS5Padding???????
 * </dl>
 * @param key ?
 * @param iv ?
 * @param input 
 * @return ?
 */
public static byte[] decrypt(final Key key, final IvParameterSpec iv, final byte[] input) {
    try {
        final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, iv);
        return cipher.doFinal(input);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
            | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
        throw new StandardRuntimeException(e);
    }
}

From source file:de.pawlidi.openaletheia.utils.CipherUtils.java

/**
 * /*w  w w . j ava  2  s. c  om*/
 * @param data
 * @param key
 * @return
 */
public static byte[] decrypt(byte[] data, Key key) {
    if (key != null && ArrayUtils.isNotEmpty(data)) {
        try {
            Cipher rsaCipher = Cipher.getInstance(CIPHER_EXTENDED_ALGORITHM);
            rsaCipher.init(Cipher.DECRYPT_MODE, key);
            return rsaCipher.doFinal(data);
        } catch (Exception e1) {
            try {
                Cipher rsaCipher = Cipher.getInstance(CIPHER_ALGORITHM);
                rsaCipher.init(2, key);
                return rsaCipher.doFinal(data);
            } catch (Exception e2) {
                throw new RuntimeException("Cannot decrypt, " + CIPHER_ALGORITHM + " error", e2);
            }
        }
    }
    return null;
}

From source file:net.seleucus.wsp.crypto.FwknopSymmetricCrypto.java

public static String encrypt(byte[] key, String message)
        throws NoSuchAlgorithmException, IOException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    SecureRandom sr = new SecureRandom();
    byte[] salt = new byte[8];
    sr.nextBytes(salt);//from  w  w w  .j  a v a  2s.c o m

    byte[][] key_and_iv = deriveKeyAndIV(salt, key);

    SecretKeySpec enc_key;
    enc_key = new SecretKeySpec(key_and_iv[0], "AES");
    Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec iv = new IvParameterSpec(key_and_iv[1]);
    aes.init(Cipher.ENCRYPT_MODE, enc_key, iv);

    byte[] salted = "Salted__".getBytes("UTF-8");
    byte[] cipher = aes.doFinal(message.getBytes("UTF-8"));

    byte[] result = new byte[salted.length + salt.length + cipher.length];

    // now we need to glue: "Salted__" + salt + cipher
    System.arraycopy(salted, 0, result, 0, salted.length);
    System.arraycopy(salt, 0, result, salted.length, salt.length);
    System.arraycopy(cipher, 0, result, salted.length + salt.length, cipher.length);

    // remove = and FWKNOP_ENCRYPTION_HEADER
    return Base64.encodeBase64String(result).replace("=", "").replace(FWKNOP_ENCRYPTION_HEADER, "");
}