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:ar.gob.ambiente.servicios.gestionpersonas.entidades.util.CriptPass.java

/**
 * Mtodo para encriptar las contraseas//from w w w  .j ava 2  s .c  o m
 * @param texto
 * @return 
 */
public static String encriptar(String texto) {

    String secretKey = "zorbazorbas"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("utf-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        base64EncryptedString = new String(base64Bytes);

    } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        System.out.println(ex.getMessage());
    }
    return base64EncryptedString;
}

From source file:com.example.license.DESUtil.java

/**
 * ?/*from  w  ww  .  java2 s.  c  o  m*/
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String decrypt(String data, String key) throws Exception {
    Key deskey = keyGenerator(key);
    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    // ?Cipher?
    cipher.init(Cipher.DECRYPT_MODE, deskey);
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data)));
}

From source file:de.uzk.hki.da.utils.PasswordUtils.java

public static String decryptPassword(String password) {

    byte key[] = "394z57f4".getBytes();
    byte decryptedPassword[];

    try {//  w w  w .  j  a  va2s  .co  m
        SecretKey secretKey = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key));
        Cipher decrypt = Cipher.getInstance("DES/CBC/PKCS5Padding");
        decrypt.init(Cipher.DECRYPT_MODE, secretKey,
                new IvParameterSpec(new byte[] { 0x01, 0x02, 0x04, 0x10, 0x01, 0x02, 0x04, 0x10 }));
        decryptedPassword = decrypt.doFinal(Base64.decodeBase64(password.getBytes()));

    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Couldn't decrypt password " + password + e);
    }

    return new String(decryptedPassword);
}

From source file:eml.studio.shared.util.Aes.java

/** 
 * Aes Decryption//from  ww  w. j a v  a 2  s.  com
 * 
 * @param encryptBytes byte[] to be decrypted
 * @param decryptKey decryption key 
 * @return 
 * @throws Exception 
 */
public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
    KeyGenerator kgen = KeyGenerator.getInstance("AES");
    kgen.init(128);

    Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
    byte[] decryptBytes = cipher.doFinal(encryptBytes);
    return new String(decryptBytes);
}

From source file:com.hybris.mobile.lib.commerce.helper.SecurityHelper.java

/**
 * Decrypt secure String associated to the key
 *
 * @param value The value to decrypt/* w w w  . j  a v  a 2s  .  c  o  m*/
 * @return decrypted string
 */
public static String decrypt(String value) {
    String decryptedText = "";
    try {
        if (StringUtils.isNotBlank(value)) {
            Cipher cipher = Cipher.getInstance(CIPHER);
            cipher.init(Cipher.DECRYPT_MODE, mSecretKeySpec, mIvParameterSpec);
            decryptedText = new String(cipher.doFinal(Base64.decode(value, Base64.NO_CLOSE)), ENCODING);
            return decryptedText;
        }
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Algorithm not found.");
    } catch (NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
        Log.e(TAG, "Exception during decrypt");
    } catch (InvalidKeyException e) {
        Log.e(TAG, "No valid key provided.");
    } catch (InvalidAlgorithmParameterException e) {
        Log.e(TAG, "Algorithm parameter specification is invalid");
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "Character to convert is unavailable");
    }

    return decryptedText;
}

From source file:com.vmware.fdmsecprotomgmt.PasswdEncrypter.java

/**
 * Encrypt the value with key provided/*from  ww w  . j  a v  a  2  s  .  co m*/
 */
private static String encrypt(String key, String value) {
    String encryptedString = null;
    try {
        IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        byte[] encrypted = cipher.doFinal(value.getBytes());
        encryptedString = Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        System.out.println("Caught exception while encrypting string : " + value);
        ex.printStackTrace();
    }

    return encryptedString;
}

From source file:com.credomatic.gprod.db2query2csv.Security.java

/**
 * Descifra una cadena de caracteres apartir de la llave de cifrado y retorna le valor original.
 * @param key llave generada durante el cifrado de la cadena original
 * @param value cadena cifrda//from   w  ww  .j  a va  2s  . c  o  m
 * @return cadena descifrada
 */
public static String decrypt(String key, String value) {
    try {
        Key k = new SecretKeySpec(new Base64().decode(key), "AES");
        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.DECRYPT_MODE, k);
        byte[] decodedValue = new Base64().decode(value);
        byte[] decValue = c.doFinal(decodedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:com.hybris.mobile.lib.commerce.helper.SecurityHelper.java

/**
 * Encrypt String associated with a key//from w w  w.  j a  v a2s  . co  m
 *
 * @param value The value to encrypt
 * @return encrypted string
 */
public static String encrypt(String value) {
    if (StringUtils.isBlank(value)) {
        throw new IllegalArgumentException();
    }

    String encryptedText = "";

    try {
        Cipher cipher = Cipher.getInstance(CIPHER);
        cipher.init(Cipher.ENCRYPT_MODE, mSecretKeySpec, mIvParameterSpec);
        encryptedText = Base64.encodeToString(cipher.doFinal(value.getBytes()), Base64.NO_CLOSE);
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "Algorithm not found.");
    } catch (NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
        Log.e(TAG, "Exception during encrypt");
    } catch (InvalidKeyException e) {
        Log.e(TAG, "No valid key provided.");
    } catch (InvalidAlgorithmParameterException e) {
        Log.e(TAG, "Algorithm parameter specification is invalid");
    }

    return encryptedText;
}

From source file:Main.java

/**
 * More flexible AES encrypt that doesn't encode
 * @param key AES key typically 128, 192 or 256 bit
 * @param iv Initiation Vector//from  w w  w  .  ja  v a2  s  .  c  o  m
 * @param message in bytes (assumed it's already been decoded)
 * @return Encrypted cipher text (not encoded)
 * @throws GeneralSecurityException if something goes wrong during encryption
 */
public static byte[] encrypt(final SecretKeySpec key, final byte[] iv, final byte[] message)
        throws GeneralSecurityException {
    final Cipher cipher = Cipher.getInstance(AES_MODE);
    //IvParameterSpec ivSpec = new IvParameterSpec(iv);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] cipherText = cipher.doFinal(message);

    log("cipherText", cipherText);

    return cipherText;
}

From source file:clases.Seguridad.java

public static String encriptar(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));
}