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:com.vmware.fdmsecprotomgmt.PasswdEncrypter.java

/**
 * Decrypt the encrypted value with provided key
 *//*from   w w  w  .j  a  va  2s  . c om*/
public static String decrypt(String key, String encryptedValue) {
    String decryptedString = null;
    try {
        IvParameterSpec iv = new IvParameterSpec(INIT_VECTOR.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

        decryptedString = new String(cipher.doFinal(Base64.decodeBase64(encryptedValue)));

    } catch (Exception ex) {
        System.out.println("Caught exception while decrypting string");
        ex.printStackTrace();
    }

    return decryptedString;
}

From source file:com.greenline.hrs.admin.util.encrypt.DESUtil.java

/**
 * Description ?/*from ww  w  .  j a v a2  s . c  o  m*/
 *
 * @param data
 * @param key  byte
 * @return
 * @throws Exception
 */
private static byte[] encrypt(byte[] data, byte[] key) throws GeneralSecurityException {
    // ????
    SecureRandom sr = new SecureRandom();

    // ?DESKeySpec
    DESKeySpec dks = new DESKeySpec(key);

    // ?DESKeySpec??SecretKey
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
    SecretKey securekey = keyFactory.generateSecret(dks);

    // Cipher??
    Cipher cipher = Cipher.getInstance(DES);

    // ?Cipher
    cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

    return cipher.doFinal(data);

}

From source file:net.fender.crypto.CryptoUtil.java

/**
 * @param keyName/*from ww w  .  j a v  a 2 s .  com*/
 * @param plaintext
 * @return
 * @throws GeneralSecurityException
 */
public static String encryptUsingSystemPropertyKey(String keyName, String plaintext)
        throws GeneralSecurityException {
    Key key = getSystemPropertyKey(keyName);
    Cipher cipher = Cipher.getInstance(key.getAlgorithm());
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte[] encryptedByes = cipher.doFinal(plaintext.getBytes());
    String encrypted = new String(Base64.encodeBase64(encryptedByes));
    return encrypted;
}

From source file:fr.ortolang.diffusion.security.authentication.TicketHelper.java

public static Ticket decodeTicket(String ticket) {
    byte[] encryptedBytes = Base64.decodeBase64(ticket.getBytes());
    try {/*from   w w  w. jav a 2  s  . c  om*/
        Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        // Extract digest from decryptedBytes (MD5 length is 16 bytes)
        byte[] digest = Arrays.copyOfRange(decryptedBytes, decryptedBytes.length - 16, decryptedBytes.length);
        byte[] serializedMap = Arrays.copyOfRange(decryptedBytes, 0, decryptedBytes.length - 16);
        MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM);
        if (!Arrays.equals(digest, md.digest(serializedMap))) {
            throw new DigestException();
        }
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serializedMap));
        return (Ticket) ois.readObject();
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException
            | IllegalBlockSizeException | IOException | ClassNotFoundException | DigestException e) {
        LOGGER.log(Level.SEVERE, "Error when decoding ticket: " + e.getMessage());
    }
    return null;
}

From source file:com.netcrest.pado.internal.security.AESCipher.java

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

From source file:net.fender.crypto.CryptoUtil.java

/**
 * @param keyName//from   w  w  w . j a  va  2 s . c om
 * @param base64Encoded
 * @return
 * @throws GeneralSecurityException
 */
public static String decryptUsingSystemPropertyKey(String keyName, String base64Encoded)
        throws GeneralSecurityException {
    byte[] encryptedBytes = Base64.decodeBase64(base64Encoded.getBytes());
    Key key = getSystemPropertyKey(keyName);
    Cipher cipher = Cipher.getInstance(key.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
    String decrypted = new String(decryptedBytes);
    return decrypted;
}

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

public static String decrypt1(String encryptedData) throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance(ALGO);
    keyGen.init(128);//from w  w  w  .jav  a 2s. c  om
    SecretKey key = keyGen.generateKey();
    Cipher c = Cipher.getInstance(ALGO);

    c.init(Cipher.DECRYPT_MODE, key);
    //        byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes());
    //        byte[] decValue = c.doFinal(decordedValue);
    byte[] decValue = c.doFinal(encryptedData.getBytes("UTF8"));
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

From source file:com.servoy.j2db.util.SecuritySupport.java

@SuppressWarnings("nls")
public static String decrypt(Settings settings, String value) throws Exception {
    if (value == null)
        return value;
    Cipher cipher = Cipher.getInstance("DESede");
    cipher.init(Cipher.DECRYPT_MODE, SecuritySupport.getCryptKey(settings));
    return new String(cipher.doFinal(Utils.decodeBASE64(value)));
}

From source file:com.sshutils.utils.CryptHelper.java

public static String encrypt(String strToEncrypt) {
    try {//w  ww  .java2s.  c o m

        Cipher cipher = Cipher.getInstance(ENCRYPT_TYPE, "BC");
        final SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        final String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes()));
        return encryptedString;
    } catch (Exception e) {
        log.error("Error while encrypting", e);
    }
    return null;

}

From source file:de.hybris.platform.b2b.punchout.services.impl.SymmetricManager.java

public static String decrypt(final String encrypted, final String key) throws PunchOutCipherException {
    String decrypted = null;//  ww  w.  j a va 2 s.  c  o m

    try {
        final Key skeySpec = new SecretKeySpec(new Base64().decode(key), ALGORITHM);
        final Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        final byte[] decodedValue = new Base64().decode(encrypted.getBytes());
        final byte[] decryptedValue = cipher.doFinal(decodedValue);
        decrypted = new String(decryptedValue);
    } catch (final NoSuchAlgorithmException | NoSuchPaddingException e) {
        // should never happen
        LOG.error("System was unable instantiate Cipher.", e);
    } catch (InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
        final String msg = "Error occured during decryption." + e.getMessage();
        LOG.info(msg);
        throw new PunchOutCipherException(msg, e);
    }
    return decrypted;
}