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.liferay.util.Encryptor.java

public static String decrypt(Key key, String encryptedString) throws EncryptorException {

    try {// w ww .  jav a 2s  .co  m
        Security.addProvider(getProvider());

        Cipher cipher = Cipher.getInstance(key.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, key);

        byte[] encryptedBytes = Base64.decode(encryptedString);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);

        String decryptedString = new String(decryptedBytes, ENCODING);

        return decryptedString;
    } catch (Exception e) {
        throw new EncryptorException(e);
    }
}

From source file:com.amazonaws.cognito.devauthsample.AESEncryption.java

private static byte[] encrypt(String clearText, String key, byte[] iv) {
    try {// w ww  .  j  av a 2s .co  m
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        cipher.init(Cipher.ENCRYPT_MODE, getKey(key), params);
        return cipher.doFinal(clearText.getBytes());
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed to encrypt.", e);
    }
}

From source file:com.amazonaws.cognito.devauthsample.AESEncryption.java

private static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) {
    try {//from  w  w w .ja  va 2s . c om
        Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        cipher.init(Cipher.DECRYPT_MODE, getKey(key), params);
        return cipher.doFinal(cipherBytes);
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed to decrypt.", e);
    }
}

From source file:com.github.aynu.mosir.core.standard.util.SecurityHelper.java

/**
 * ????/*from ww  w.  jav  a 2 s  .  co 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(ALGO_CIPHER_SMALL);
        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.mosir.core.standard.util.SecurityHelper.java

/**
 * ????/*from   w  ww  .  j a va2 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(ALGO_CIPHER_SMALL);
        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:com.github.aynu.mosir.core.standard.util.SecurityHelper.java

/**
 * ???//w  ww  .j ava  2 s . com
 * <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(ALGO_CIPHER_BIG);
        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.mosir.core.standard.util.SecurityHelper.java

/**
 * ???/* w  w  w  .j  av a  2 s.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(ALGO_CIPHER_BIG);
        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:Controller.StringEncrypt.java

public final static String encrypt(String cleartext) {
    byte[] encrypted = null;
    try {//  ww  w  .j  a  v a 2 s . c  o m
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        SecretKeySpec skeySpec = new SecretKeySpec("92AE31A79FEEB2A3".getBytes(), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec("0123456789ABCDEF".getBytes());
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
        encrypted = cipher.doFinal(cleartext.getBytes());
    } catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException
            | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e) {
        JOptionPane.showMessageDialog(null, "Error encriptando contrasea");
    }
    return new String(encodeBase64(encrypted));
}

From source file:cloudeventbus.pki.CertificateUtils.java

public static void validateSignature(PublicKey key, byte[] challenge, byte[] salt, byte[] signature) {
    try {// www.ja v a 2 s.  co  m
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, key);
        final byte[] decryptedSignature = cipher.doFinal(signature);
        if (decryptedSignature.length != challenge.length + salt.length) {
            throw new InvalidSignatureException("Signature doesn't match challenge");
        }
        for (int i = 0; i < challenge.length; i++) {
            if (decryptedSignature[i] != challenge[i]) {
                throw new InvalidSignatureException("Signature doesn't match challenge");
            }
        }
        for (int i = 0; i < salt.length; i++) {
            if (decryptedSignature[challenge.length + i] != salt[i]) {
                throw new InvalidSignatureException("Signature doesn't match challenge");
            }
        }
    } catch (GeneralSecurityException e) {
        throw new CertificateSecurityException(e);
    }
}

From source file:com.app.utils.StringEncrypt.java

/**
 * Funcin de tipo String que recibe una llave (key), un vector de inicializacin (iv)
 * y el texto que se desea descifrar/*from   w ww . ja v a2s  .  c  o  m*/
 * @param key la llave en tipo String a utilizar
 * @param iv el vector de inicializacin a utilizar
 * @param encrypted el texto cifrado en modo String
 * @return el texto desencriptado en modo String
 * @throws Exception puede devolver excepciones de los siguientes tipos: NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException
 */
public static String decrypt(String key, String iv, String encrypted) throws Exception {
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    byte[] enc = decodeBase64(encrypted);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] decrypted = cipher.doFinal(enc);
    return new String(decrypted);
}