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

private static Cipher getCipherFromPassphrase(String passphrase, byte[] salt, int iterations, int opMode)
        throws GeneralSecurityException {
    SecretKey key = getKeyFromPassphrase(passphrase, salt, iterations);
    Cipher cipher = Cipher.getInstance(key.getAlgorithm());
    cipher.init(opMode, key, new PBEParameterSpec(salt, iterations));

    return cipher;
}

From source file:com.ad.mediasharing.tvmclient.AESEncryption.java

public static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) throws Exception {
    Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
    AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
    params.init(new IvParameterSpec(iv));
    cipher.init(Cipher.DECRYPT_MODE, getKey(key), params);
    return cipher.doFinal(cipherBytes);
}

From source file:com.drisoftie.cwdroid.util.CredentialUtils.java

private static Cipher getDefaultCipher() {
    Cipher cipher = null;//from   w  w w  .j ava2 s .  com
    try {
        cipher = Cipher.getInstance(AES);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
        e.printStackTrace();
    }
    return cipher;
}

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;
}

From source file:com.amazonaws.cognito.sync.demo.client.server.AESEncryption.java

private static byte[] decrypt(byte[] cipherBytes, String key, byte[] iv) throws Exception {
    Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
    AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
    params.init(new IvParameterSpec(iv));
    cipher.init(Cipher.DECRYPT_MODE, getKey(key), params);
    return cipher.doFinal(cipherBytes);
}

From source file:com.kylinolap.rest.security.PasswordPlaceholderConfigurer.java

public static String encrypt(String strToEncrypt) {
    try {/*from w  ww  .j  ava  2 s  . co  m*/
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        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) {
    }
    return null;

}

From source file:edu.wright.cs.sp16.ceg3120.util.PasswordEncryptionUtility.java

/**
 * Encrypts a given string using AES.//w w w.  j a v  a 2  s .c om
 * 
 * @param value
 *            // String to encrypt.
 * @return // Returns encrypted string.
 */
public static String encrypt(String value) {
    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.ENCRYPT_MODE, skeySpec, iv);

        byte[] encrypted = cipher.doFinal(value.getBytes("UTF-8"));
        System.out.println("encrypted string: " + Base64.encodeBase64String(encrypted));

        return Base64.encodeBase64String(encrypted);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:Logi.GSeries.Libraries.Encryption.java

public static String encrypt(String decryptedString, String password) {
    try {/*w w  w  .  j  av  a2s . c o  m*/
        // build the initialization vector (randomly).
        SecureRandom random = new SecureRandom();
        byte initialVector[] = new byte[16];
        //generate random 16 byte IV AES is always 16bytes
        random.nextBytes(initialVector);
        IvParameterSpec ivspec = new IvParameterSpec(initialVector);
        SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);
        byte[] encrypted = cipher.doFinal(decryptedString.getBytes());
        byte[] encryptedWithIV = new byte[encrypted.length + initialVector.length];
        System.arraycopy(encrypted, 0, encryptedWithIV, 0, encrypted.length);
        System.arraycopy(initialVector, 0, encryptedWithIV, encrypted.length, initialVector.length);
        return Base64.encodeBase64String(encryptedWithIV);
    } catch (Exception ex) {
        Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        return "Error";
    }
}

From source file:com.liferay.sync.engine.lan.util.LanTokenUtil.java

public static String createEncryptedToken(String lanTokenKey) throws Exception {

    String lanToken = RandomStringUtils.random(32, 0, 0, true, true, null, _secureRandom);

    byte[] bytes = DigestUtils.sha1(lanTokenKey);

    bytes = Arrays.copyOf(bytes, 16);

    Cipher cipher = Cipher.getInstance("AES");

    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(bytes, "AES"));

    String encryptedToken = Base64
            .encodeBase64String(cipher.doFinal(lanToken.getBytes(Charset.forName("UTF-8"))));

    _lanTokens.add(lanToken);//from  w ww.  j a v a 2  s  . c om

    return encryptedToken;
}

From source file:DesEncrypter.java

DesEncrypter(SecretKey key) throws Exception {
    byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 0x07, 0x72, 0x6F, 0x5A };
    AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
    ecipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    dcipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

    ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
    dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
}