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 byte[] decrypt(byte[] key, byte[] input) throws Exception {
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"));
    byte[] decrypted = cipher.doFinal(input);
    return decrypted;
}

From source file:Main.java

public static byte[] decryptData(byte[] encryptedData, PrivateKey privateKey) {
    try {//  w  w w .  j  a  va2s.c  o  m
        Cipher cipher = Cipher.getInstance(RSA_CHPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(encryptedData);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static byte[] encrypt(byte[] key, byte[] input) throws Exception {
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"));
    byte[] encrypted = cipher.doFinal(input);
    return encrypted;
}

From source file:Main.java

/**
 * function decrypt the string and return the result
 * @param stringToDecrypt the string against which the decryption to be performed
 * @return the decrypted String//from   w w  w.  j a v a2  s.c om
 */
public static final String decrypt(String stringToDecrypt) {
    try {
        Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, aesKey);
        return new String(cipher.doFinal(stringToDecrypt.getBytes()));
    } catch (Exception e) {

    }
    return null;
}

From source file:Main.java

/**
 * RSA encryption/*  w  ww  .j  av a  2s .  co m*/
 * @param pubKey
 * @param message
 * @return
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 */
public static byte[] encryptRSA(RSAPublicKey pubKey, byte[] message) throws IllegalBlockSizeException,
        BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
    Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, pubKey);

    return cipher.doFinal(message);
}

From source file:Main.java

/**
 * RSA decryption/*from  w  w w  . ja va  2s . c  o  m*/
 * @param priKey
 * @param message
 * @return
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 */
public static byte[] decryptRSA(RSAPrivateKey priKey, byte[] message) throws IllegalBlockSizeException,
        BadPaddingException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException {
    Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
    cipher.init(Cipher.DECRYPT_MODE, priKey);

    return cipher.doFinal(message);
}

From source file:br.com.zeros.tipsandtricks.cryto.EncryptionStrings.java

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

From source file:Main.java

/**
 * Encrypt data/*ww w  .  j  a  va2 s .  com*/
 * @param secretKey   -   a secret key used for encryption
 * @param data      -   data to encrypt
 * @return   Encrypted data
 * @throws Exception
 */
public static String cipher(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.ENCRYPT_MODE, key);

    return toHex(cipher.doFinal(data.getBytes()));
}

From source file:Main.java

public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, "DESede");

    Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] plainBytes = cipher.doFinal(data);
    return plainBytes;
}

From source file:MainClass.java

private static String encrypt(char[] password, String plaintext) throws Exception {

    byte[] salt = new byte[8];
    Random random = new Random();
    random.nextBytes(salt);/*from   w  w w  .  j  a  v a  2 s . c o  m*/

    PBEKeySpec keySpec = new PBEKeySpec(password);

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");

    SecretKey key = keyFactory.generateSecret(keySpec);

    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 1000);

    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);

    byte[] ciphertext = cipher.doFinal(plaintext.getBytes());

    BASE64Encoder encoder = new BASE64Encoder();

    String saltString = encoder.encode(salt);
    String ciphertextString = encoder.encode(ciphertext);

    return saltString + ciphertextString;
}