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:Main.java

public static String decryptData(String ciphertext, String password) throws Exception {
    int iterationCount = 100; //because polaroid
    int keyLength = 256;

    String[] fields = ciphertext.split("]");
    byte[] iv = Base64.decode(fields[0], 0);
    byte[] salt = Base64.decode(fields[1], 0);
    byte[] cipherBytes = Base64.decode(fields[2], 0);

    Log.d(TAG, "ciphertext: " + ciphertext + "\n" + "iv length is " + "\n" + iv.length);

    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
    SecretKey key = new SecretKeySpec(keyBytes, "AES");

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivParams = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key, ivParams);
    byte[] plaintext = cipher.doFinal(cipherBytes);
    String plainStr = new String(plaintext, "UTF-8");

    return plainStr;
}

From source file:com.lecaddyfute.utils.security.AESCrypto.java

public static String decrypt(String encryptedData) throws Exception {
    Key key = generateKey();//from   www. j  av  a2s .c o m
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes());
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

From source file:com.dragoniade.encrypt.EncryptionHelper.java

public static void decrypt(Properties p, String seedKey, String key) {
    String value = p.getProperty(key);
    String seed = p.getProperty(seedKey, seedKey);

    if (value == null || value.length() == 0) {
        return;/* w w w. j a  v a2 s. co m*/
    }

    int index = value.indexOf('{');
    switch (index) {
    case -1:
        return;
    case 0: {
        String toDecode = value.substring(1, value.length() - 1);
        String decryptStr;
        try {
            decryptStr = new String(Base64.decode(toDecode), "UTF-16");
        } catch (UnsupportedEncodingException e1) {
            decryptStr = new String(Base64.decode(toDecode));
        }

        p.setProperty(key, decryptStr);
        return;
    }
    default:
        String toDecode = value.substring(index + 1, value.length() - 1);
        String algorithm = value.substring(0, index);

        byte[] decryptStr = Base64.decode(toDecode);

        Cipher cipher = getDecrypter(seed, algorithm);

        String decoded;
        try {
            byte[] result = cipher.doFinal(decryptStr);
            try {
                decoded = new String(result, "UTF-16");
            } catch (UnsupportedEncodingException e1) {
                decoded = new String(result);
            }
        } catch (Exception e) {
            decoded = "";
        }

        p.setProperty(key, decoded);
    }
}

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

/**
 * ?//from   w ww. ja va2  s .  c o  m
 * 
 * @param data
 *            ?
 * @param key
 *            
 * @return ??
 */
public static String decryptBase64(String data, String key) throws Exception {
    DESKeySpec desKey = new DESKeySpec(key.getBytes());
    // ?DESKeySpec??
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    SecretKey securekey = keyFactory.generateSecret(desKey);

    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, securekey);
    // ?
    return new String(cipher.doFinal(Base64.decodeBase64(data.getBytes())));
}

From source file:it.latraccia.pkcs11.reader.util.AESUtil.java

public static String decryptString(String encryptedText, String password)
        throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    String decrypted = null;// w  w  w .  java2 s  . c o  m
    byte[] key = password.getBytes();
    if (key.length != 16) {
        throw new IllegalArgumentException("Invalid key size.");
    }
    byte[] value = Base64.decodeBase64(encryptedText);

    // Decrypt with AES/CBC/PKCS5Padding
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
    byte[] original = cipher.doFinal(value);
    decrypted = new String(original);

    return decrypted;
}

From source file:illab.nabal.util.SecurityHelper.java

/**
  * AES-encrypt a plain message. Return null if message is empty.
  * // w  w w . j av  a 2 s.  co m
  * @param message
  * @return AES encrypted hex
  * @throws Exception
  */
public static String cipher(String message) throws Exception {
    if (StringHelper.isEmpty(message) == false) {
        Cipher cipher = Cipher.getInstance(AES);
        cipher.init(Cipher.ENCRYPT_MODE, SYSTEM_SECRET_KEY_SPEC);
        return Base64.encodeToString(cipher.doFinal(message.getBytes()), Base64.DEFAULT);
    } else {
        return null;
    }
}

From source file:com.iterzp.momo.utils.RSAUtils.java

/**
 * /*from   w  w w. j  a  v  a2s. c  o m*/
 * 
 * @param publicKey
 *            
 * @param data
 *            ?
 * @return ??
 */
public static byte[] encrypt(PublicKey publicKey, byte[] data) {
    Assert.notNull(publicKey);
    Assert.notNull(data);
    try {
        Cipher cipher = Cipher.getInstance("RSA", PROVIDER);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:lib.clases_cripto.java

public static String Encriptar(String texto) {

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

    try {// w  w w.j a  va2  s.  c o m

        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(texto.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        Md5Crypt.md5Crypt(keyBytes);
        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 (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:com.esri.geoevent.datastore.Crypto.java

static public String doDecrypt(String stringToDecrypt) throws GeneralSecurityException {
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, encryptionKey);
    byte[] decodedValue = Base64.decodeBase64(stringToDecrypt.getBytes());
    byte[] decryptedValue = c.doFinal(decodedValue);
    String decryptedString = new String(decryptedValue);
    return decryptedString;
}

From source file:com.sds.acube.ndisc.mts.xserver.util.XNDiscCipher.java

/**
 * /*from   w ww .j a v  a 2  s .c o m*/
 * 
 * @param data ? ?
 * @return ? ?
 */
public static String encode(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte e[] = cipher.doFinal(data.getBytes("UTF-8"));
        return replaceChar(Base64.encodeBase64String(e));
    } catch (NoSuchAlgorithmException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (NoSuchPaddingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (InvalidKeyException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (IllegalBlockSizeException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (BadPaddingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    } catch (UnsupportedEncodingException e) {
        System.err.print(XNDiscUtils.printStackTrace(e));
    }
    return null;
}