Example usage for javax.crypto Cipher getInstance

List of usage examples for javax.crypto Cipher getInstance

Introduction

In this page you can find the example usage for javax.crypto Cipher getInstance.

Prototype

public static final Cipher getInstance(String transformation)
        throws NoSuchAlgorithmException, NoSuchPaddingException 

Source Link

Document

Returns a Cipher object that implements the specified transformation.

Usage

From source file:com.paypal.utilities.AESDecrypt.java

public AESDecrypt(String encryptedString) throws Exception {
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
    SecretKey secretKeyTemp = secretKeyFactory.generateSecret(keySpec);
    SecretKey secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), "AES");

    encrypt = Base64.decodeBase64(encryptedString.getBytes());

    eCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    eCipher.init(Cipher.ENCRYPT_MODE, secretKey);

    byte[] iv = extractIV();
    dCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    dCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
}

From source file:com.bconomy.autobit.Encryption.java

public static byte[] encrypt(byte[] cleartext, byte[] key) {
    if (keySpec == null)
        keySpec = new SecretKeySpec(key, "AES");
    Cipher aes;/*from  w w  w. j  a v a  2  s.  c o  m*/
    try {
        aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
        aes.init(Cipher.ENCRYPT_MODE, keySpec);
        return aes.doFinal(cleartext);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException
            | BadPaddingException ex) {
        ex.printStackTrace();
    }
    return new byte[0];
}

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

public static String decrypt(String encryptedData) throws Exception {
    Key key = generateKey();/*from w w w.j a  va2 s. 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:edu.wright.cs.sp16.ceg3120.util.PasswordEncryptionUtility.java

/**
 * Decrypts a given string using AES./* www  .  ja v a  2  s  .  c o m*/
 * 
 * @param encrypted
 *            // Encrypted string.
 * @return // Returns decrypted string.
 */
public static String decrypt(String encrypted) {
    try {
        IvParameterSpec iv = new IvParameterSpec(initVector.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);

        byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
        System.out.println("Decrypted Password: " + new String(original, "UTF-8"));
        return new String(original, "UTF-8");
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:com.qubole.quark.catalog.db.encryption.AESEncrypt.java

public String convertToDatabaseColumn(String phrase) throws SQLException {
    try {/*from www. j  a  va  2  s. c o m*/
        Cipher encryptCipher = Cipher.getInstance("AES");
        encryptCipher.init(Cipher.ENCRYPT_MODE, generateMySQLAESKey(this.key, "UTF-8"));

        return new String(Hex.encodeHex(encryptCipher.doFinal(phrase.getBytes("UTF-8"))));
    } catch (Exception e) {
        throw new SQLException(e);
    }
}

From source file:com.salesmanager.core.util.EncryptionUtil.java

public static String encrypt(String key, String value) throws Exception {

    // value = StringUtils.rightPad(value, 16,"*");
    // Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    // NEED TO UNDERSTAND WHY PKCS5Padding DOES NOT WORK
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
    IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
    byte[] inpbytes = value.getBytes();
    byte[] encrypted = cipher.doFinal(inpbytes);
    return new String(bytesToHex(encrypted));

}

From source file:de.adorsys.morphiaencryption.AES256CryptoProvider.java

@Override
public byte[] encrypt(byte[] data) {
    try {// w ww.j a v a  2s. c om
        Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, key, getIV());
        byte[] encrypted = cipher.doFinal(data);
        return encrypted;
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException
            | InvalidKeyException | InvalidAlgorithmParameterException e) {
        throw new CryptException(e);
    }
}

From source file:model.Encryption.java

/**
 *
 * @param encryptedText/*from www.  ja  va  2s  .  c  o m*/
 * @return
 * @throws Exception
 */
@SuppressWarnings("static-access")
public String decrypt(String encryptedText) throws Exception {
    if (secret == null)
        generateSecretKey();

    byte[] encryptedTextBytes = new Base64().decode(encryptedText);

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, secret);
    byte[] decryptedTextBytes = cipher.doFinal(encryptedTextBytes);

    return new String(decryptedTextBytes, "UTF-8");
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.util.StringProtector.java

public StringProtector(final String passwd) throws GeneralSecurityException {
    final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    final SecretKey key = keyFactory.generateSecret(new PBEKeySpec(passwd.toCharArray()));
    encryptor = Cipher.getInstance("PBEWithMD5AndDES");
    encryptor.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));

    decryptor = Cipher.getInstance("PBEWithMD5AndDES");
    decryptor.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
}

From source file:com.arvato.thoroughly.util.security.impl.AES128AttributeEncryptionStrategy.java

public String encrypt(final String s) throws Exception {
    if (StringUtils.isNotEmpty(s)) {
        final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
        final SecretKeySpec keySpec = getKeySpec();
        cipher.init(1, keySpec);//from  w  ww.j  a  v a2s .c om

        final byte[] encrypted = cipher.doFinal(s.getBytes());
        final byte[] encoded = new Hex().encode(encrypted);
        return new String(encoded);
    }
    return s;
}