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

private static byte[] doFinal(String key, int opmode, byte[] input)
        throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException,
        IllegalBlockSizeException, BadPaddingException {
    key = checkNull(key) ? DEFAULT_KEY : key;
    if (checkNull(key)) {
        return null;
    }/* ww  w  .j  a va2 s  . com*/
    SecureRandom sr = new SecureRandom();
    DESKeySpec dks = new DESKeySpec(key.getBytes());
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);
    Cipher cipher = Cipher.getInstance(MODE);
    cipher.init(opmode, securekey, sr);
    return cipher.doFinal(input);
}

From source file:Main.java

private static byte[] encryptWithPassphrase(byte[] encryptionSalt, int iterations, byte[] data,
        String passphrase) throws GeneralSecurityException {
    Cipher cipher = getCipherFromPassphrase(passphrase, encryptionSalt, iterations, Cipher.ENCRYPT_MODE);
    return cipher.doFinal(data);
}

From source file:Main.java

/**
 * Decrypt data/*from w  w  w  .j ava 2  s.c  o  m*/
 * @param secretKey -   a secret key used for decryption
 * @param data      -   data to decrypt
 * @return   Decrypted data
 * @throws Exception
 */
public static String decipher(String secretKey, String data) throws Exception {
    // Key has to be of length 8
    if (secretKey == null || secretKey.length() != 8)
        throw new Exception("Invalid key length - 8 bytes key needed!");

    SecretKey key = new SecretKeySpec(secretKey.getBytes(), ALGORITHM);
    Cipher cipher = Cipher.getInstance(ALGORITHM);
    cipher.init(Cipher.DECRYPT_MODE, key);

    return new String(cipher.doFinal(toByte(data)));
}

From source file:com.searchbox.utils.DecryptLicense.java

public static byte[] decrypt(byte[] inpBytes) throws Exception {

    byte[] pkbytes = Base64.decodeBase64(privkey);
    KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(pkbytes);
    PrivateKey pk = keyFactory.generatePrivate(privateKeySpec);

    Cipher cipher = Cipher.getInstance(xform);
    cipher.init(Cipher.DECRYPT_MODE, pk);
    return cipher.doFinal(inpBytes);
}

From source file:Main.java

private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    return cipher.doFinal(clear);
}

From source file:Main.java

private static byte[] decryptWithPassphrase(byte[] encryptionSalt, int iterations, byte[] data,
        String passphrase) throws GeneralSecurityException, IOException {
    Cipher cipher = getCipherFromPassphrase(passphrase, encryptionSalt, iterations, Cipher.DECRYPT_MODE);
    return cipher.doFinal(data);
}

From source file:Main.java

private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

From source file:Main.java

public static byte[] decrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) {
    try {//from www. ja  v a  2 s.c om
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
        return cipher.doFinal(textBytes);
    } catch (Exception e) {
        return errorbyte;
    }
}

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

public static String encrypt(String valueToEnc) throws Exception {
    Key key = generateKey();//from www. j a  v a  2 s  .  c o m
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encValue = c.doFinal(valueToEnc.getBytes());
    String encryptedValue = Base64.encodeBase64String(encValue);
    return encryptedValue;
}

From source file:Main.java

public static byte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) {
    try {/*from  ww w  . j a  v a  2 s.c o m*/
        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
        return cipher.doFinal(textBytes);
    } catch (Exception e) {
        Log.e(TAG, "Error during encryption: " + e.toString());
        return errorbyte;
    }
}