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:JavaTron.JTP.java

/**
 * Decrypts an encrypted string/*from  w w w.  j  av a2s . c o  m*/
 * @param property
 * @return A plaintext string
 */
public static String decrypt(String property) {
    String p = new String();
    try {
        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));
        p = new String(pbeCipher.doFinal(base64Decode(property)));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return p;
}

From source file:JavaTron.JTP.java

/**
 * Encrypts a string/*from  ww w. j  av  a2  s.  c o m*/
 * @param property
 * @return An encrypted string
 */
public static String encrypt(String property) {
    String p = new String();
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
        Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
        pbeCipher.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
        p = base64Encode(pbeCipher.doFinal(property.getBytes()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return p;
}

From source file:Main.java

private static CharSequence decryptText(String ciphertextString, Cipher cipher) {
    byte[] ciphertext = hexStringToBytes(ciphertextString);
    byte[] plaintext = {};

    //can't decrypt an empty string...exceptions gallore
    if (ciphertextString.length() == 0)
        return (CharSequence) ciphertextString;

    try {// w  w w.  j a va 2 s  .  com
        plaintext = cipher.doFinal(ciphertext);
    } catch (Exception e) {
        Log.d(LOG_TAG, "decryptText", e);
    }

    //TODO: dont' convert to string as interim...insecure
    return (CharSequence) (new String(plaintext));
}

From source file:clases.Seguridad.java

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

From source file:de.extra.client.plugins.outputplugin.crypto.ExtraCryptoUtil.java

/** Decrypts the specified encrypted string, using the specified secret key. */
private static String decrypt(String sName, String secretKey) {
    if (secretKey == null) {
        secretKey = SYM_KEY_STR;//from  w  w  w.  j  a  v a2s . c o m
    }

    if (sName == null || sName.equals("")) {
        return "";
    }

    String sText = "";
    try {
        SecretKeySpec skeySpec = decodeKey(secretKey);
        Cipher decryptCipher = Cipher.getInstance(TRANSFORMATION);
        decryptCipher.init(Cipher.DECRYPT_MODE, skeySpec);

        byte[] encpArr = new Base64().decode(sName.trim());

        byte[] plainText = decryptCipher.doFinal(encpArr);

        sText = new String(plainText, CHARSET);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }

    return sText;
}

From source file:com.avbravo.avbravoutils.crypto.Encriptador.java

public static String decrypt(String key, 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);
}

From source file:gsn.http.ac.Protector.java

public static String encrypt(String value) throws Exception {
    logger.debug("Encrypt key");
    Key key = generateKey();/*from ww w.j a v a  2s  . com*/
    String salt = getSalt();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key);

    String valueToEnc = null;
    String eValue = value;
    for (int i = 0; i < ITERATIONS; i++) {
        valueToEnc = salt + eValue;
        byte[] encValue = c.doFinal(valueToEnc.getBytes());
        eValue = new sun.misc.BASE64Encoder().encode(encValue);
        //eValue = Base64.encodeBase64String(encValue);
    }
    return eValue;
}

From source file:Main.java

public static byte[] getDecCode(byte[] byteD, String seed) {
    byte[] byteFina = null;
    Cipher cipher = null;

    try {/*from   www. j  a v a 2 s.  c o  m*/
        SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(seed.getBytes()), "AES");
        cipher = Cipher.getInstance("AES/CFB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        byteFina = cipher.doFinal(byteD);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }

    return byteFina;
}

From source file:com.cherong.mock.common.base.util.EncryptionUtil.java

/**
 * des//from  ww  w  .j av  a 2s.c om
 * 
 * @param content
 * @param key
 * @return
 * @throws Exception
 */
public static String decryptByDES(byte[] content, String key) {
    try {
        DESKeySpec desKS = new DESKeySpec(key.getBytes());
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        SecretKey sk = skf.generateSecret(desKS);
        Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM);
        cip.init(Cipher.DECRYPT_MODE, sk);
        byte[] result = cip.doFinal(content);
        return new String(result);
    } catch (Exception e) {
        LOGGER.error("{}", e);
        return null;
    }
}

From source file:com.liferay.util.Encryptor.java

public static String encrypt(Key key, String plainText) throws EncryptorException {

    try {/*from w  w  w  .  jav a  2  s.c om*/
        Security.addProvider(getProvider());

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

        byte[] decryptedBytes = plainText.getBytes(ENCODING);
        byte[] encryptedBytes = cipher.doFinal(decryptedBytes);

        String encryptedString = Base64.encode(encryptedBytes);

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