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

public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception {

    Key deskey = null;//from www  .ja  v a2s . com
    DESedeKeySpec spec = new DESedeKeySpec(key);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
    IvParameterSpec ips = new IvParameterSpec(keyiv);
    cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);
    byte[] bOut = cipher.doFinal(data);

    return bOut;
}

From source file:Main.java

public static String decode(String key, String ciphertext) throws Exception {
    byte[] bs = parseHexStr2Byte(ciphertext);
    IvParameterSpec ivSpec = new IvParameterSpec(HEX.getBytes());
    SecretKeySpec secretKeySpec = createKey(key);
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);
    return new String(c.doFinal(bs), "UTF-8");
}

From source file:Main.java

public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception {

    Key deskey = null;/*www . j  a  v a2  s .c  om*/
    DESedeKeySpec spec = new DESedeKeySpec(key);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding");
    IvParameterSpec ips = new IvParameterSpec(keyiv);

    cipher.init(Cipher.DECRYPT_MODE, deskey, ips);

    byte[] bOut = cipher.doFinal(data);

    return bOut;

}

From source file:Main.java

public static byte[] decrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) {
    try {//from   ww  w .ja  v  a  2s  .  co  m
        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: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 ww . ja v a2 s  .co m
 */
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

/** 
 * decrypt /*from  w  ww . ja  v  a  2 s.  c  om*/
 * @param data prepare to be decrypted 
 * @param key byteArray[] key
 * @return byte[] original data 
 * */
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {

    Key k = byteArrayKeyToSecurityKey(key);

    Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
    //init, decrypt mode
    cipher.init(Cipher.DECRYPT_MODE, k);
    //exec 
    return cipher.doFinal(data);
}

From source file:Main.java

public static byte[] getDecCode(byte[] byteD, String seed) {
    byte[] byteFina = null;
    Cipher cipher = null;//from   w w  w  . j a  v  a  2  s . co m

    try {
        SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(seed.getBytes()), "AES");
        cipher = Cipher.getInstance("AES/CFB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        byteFina = cipher.doFinal(byteD);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }

    return byteFina;
}

From source file:Main.java

public static byte[] getEncCode(byte[] byteE, String seed) {
    byte[] byteFina = null;
    Cipher cipher = null;/*from  ww  w . j a va 2 s . c  o  m*/

    try {
        SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(seed.getBytes()), "AES");

        cipher = Cipher.getInstance("AES/CFB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        //cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
        byteFina = cipher.doFinal(byteE);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        cipher = null;
    }

    return byteFina;
}

From source file:Main.java

public static byte[] encrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) {
    try {/*from   w  ww . ja  v  a 2s.co  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;
    }
}

From source file:Encrypt.java

private static String decrypt(String message) throws Exception {
    byte[] bytesrc = convertHexString(message);
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
    IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));

    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
    byte[] retByte = cipher.doFinal(bytesrc);
    return new String(retByte);
}