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:license.rsa.WakeRSA.java

/**
 * rsa/* w  ww .  j  a v  a2 s .c  om*/
 * @param key
 * @return
 * @throws Exception
 */
private static byte[] rsa(byte[] key) throws Exception {
    PublicKey pubKey = readPublicKeyFromFile();
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, pubKey);
    return cipher.doFinal(key);
}

From source file:com.juicioenlinea.application.secutiry.Security.java

public static String desencriptar(String textoEncriptado) {

    final String secretKey = "hunter"; //llave para encriptar datos
    String encryptedString = null;

    try {/* ww w .  j a  v a 2s.  co  m*/
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("UTF-8"));
        MessageDigest md = MessageDigest.getInstance("SHA");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("UTF-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");

        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        encryptedString = new String(plainText, "UTF-8");

    } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException
            | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }

    return encryptedString;
}

From source file:com.liferay.sync.engine.lan.util.LanTokenUtil.java

public static String decryptLanToken(String lanTokenKey, String encryptedToken) throws Exception {

    byte[] bytes = DigestUtils.sha1(lanTokenKey);

    bytes = Arrays.copyOf(bytes, 16);

    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(bytes, "AES"));

    return new String(cipher.doFinal(Base64.decodeBase64(encryptedToken)), Charset.forName("UTF-8"));
}

From source file:Main.java

public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception {

    Key deskey = null;//from  w  w  w  .  jav a  2  s. c  o  m
    DESedeKeySpec spec = new DESedeKeySpec(key);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
    IvParameterSpec ips = new IvParameterSpec(keyiv);
    cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
    byte[] bOut = cipher.doFinal(data);

    return bOut;
}

From source file:com.formatAdmin.utils.UtilsSecurity.java

public static String decifrarMD5(String textoEncriptado) throws Exception {
    String base64EncryptedString = "";

    try {//from   ww  w  . j  av  a2s.  co m
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(SECRET_MD5_KEY.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, ENCRYPT_ALGORITHM);

        Cipher decipher = Cipher.getInstance(ENCRYPT_ALGORITHM);
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        base64EncryptedString = new String(plainText, "UTF-8");

    } catch (UnsupportedEncodingException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException
            | IllegalBlockSizeException | NoSuchPaddingException ex) {
        throw ex;
    }
    return base64EncryptedString;
}

From source file:Main.java

public static String encrypt(String key, String str) throws java.io.UnsupportedEncodingException,
        NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    String aesKey;// w  w  w.j av  a2 s.c o m
    aesKey = key.substring(0, 16);
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, AESkey(key), new IvParameterSpec(aesKey.getBytes()));

    byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
    String enStr = new String(Base64.encodeToString(encrypted, 0));

    return enStr;
}

From source file:ch.helmchen.camlapse.user.control.Encryption.java

public static String decrypt(String aBase64String) throws GeneralSecurityException, IOException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return new String(pbeCipher.doFinal(base64Decode(aBase64String)));
}

From source file:com._64bitlabs.util.Encryptors.java

/**
 * Encrypts string value with the specified AES algorithm
 * @param Data string to be encrypted//from  w w  w  .ja v  a 2  s  .com
 * @return encrypted string
 */
public static String encrypt(String Data) {
    try {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encVal = c.doFinal(Data.getBytes());
        Base64 encoder = new Base64();
        String encryptedValue = new String(encoder.encode(encVal)).trim();
        return encryptedValue;
    } catch (Exception e) {
        return null;
    }
}

From source file:com.doculibre.constellio.utils.aes.SimpleProtector.java

public static String decrypt(String encryptedValue) throws Exception {
    Key key = generateKey();/*  ww  w.j a v a  2s  .c o m*/
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = Base64.decodeBase64(encryptedValue);
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

From source file:de.taimos.dvalin.interconnect.model.MessageCryptoUtil.java

/**
 *
 * @param data the BASE 64 data/*from   w  ww  . ja  v a  2 s.c o m*/
 * @return the decrypted data
 * @throws CryptoException on decryption error
 */
public static String decrypt(final String data) throws CryptoException {
    if (data == null) {
        return null;
    }

    try {
        final Cipher cipher = MessageCryptoUtil.getCipher(Cipher.DECRYPT_MODE);
        return new String(cipher.doFinal(Base64.decodeBase64(data)), Charset.forName("UTF-8"));
    } catch (final Exception e) {
        throw new CryptoException("Decryption of data failed!", e);
    }
}