Example usage for javax.crypto Cipher DECRYPT_MODE

List of usage examples for javax.crypto Cipher DECRYPT_MODE

Introduction

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

Prototype

int DECRYPT_MODE

To view the source code for javax.crypto Cipher DECRYPT_MODE.

Click Source Link

Document

Constant used to initialize cipher to decryption mode.

Usage

From source file:Main.java

public static byte[] decodeFile(byte[] key, byte[] fileData, boolean isPlainText) throws Exception {
    byte[] decrypted;
    if (!isPlainText) {
        decrypted = new byte[fileData.length - key.length * 2];
        System.arraycopy(fileData, key.length, decrypted, 0, decrypted.length);
    } else {//w w  w.  ja  v a  2  s.c o  m
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);

        decrypted = cipher.doFinal(fileData);
    }
    return decrypted;
}

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

/**
 * 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 av  a2s . 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

public static byte[] TriDesDecryption(byte[] byteKey, byte[] dec) {
    try {//from   w  ww.  ja v a 2s .c o m
        byte[] en_key = new byte[24];
        if (byteKey.length == 16) {
            System.arraycopy(byteKey, 0, en_key, 0, 16);
            System.arraycopy(byteKey, 0, en_key, 16, 8);
        }
        SecretKey key = new SecretKeySpec(en_key, "DESede");
        Cipher dcipher = Cipher.getInstance("DESede/ECB/NoPadding");
        dcipher.init(Cipher.DECRYPT_MODE, key);

        byte[] de_b = dcipher.doFinal(dec);

        return de_b;
    } catch (Exception e) {
        e.printStackTrace();

    }
    return null;
}

From source file:Main.java

public static byte[] decrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes) {
    try {//w  w  w . j  a va  2s  . c  om
        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

public static String deCrypto(String txt, String key) {
    SecretKeyFactory skeyFactory = null;
    Cipher cipher = null;/*  ww w  .j  a  v  a  2  s  . co m*/
    byte[] btxts = null;
    try {
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
        skeyFactory = SecretKeyFactory.getInstance("DES");
        cipher = Cipher.getInstance("DES");
        SecretKey deskey = skeyFactory.generateSecret(desKeySpec);
        cipher.init(Cipher.DECRYPT_MODE, deskey);
        btxts = new byte[txt.length() / 2];
        for (int i = 0, count = txt.length(); i < count; i += 2) {
            btxts[i / 2] = (byte) Integer.parseInt(txt.substring(i, i + 2), 16);
        }
        return (new String(cipher.doFinal(btxts)));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;

}

From source file:Main.java

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

    Key deskey = null;/*w  w w.ja v a2 s  .c o m*/
    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[] input, byte[] key) throws InvalidKeyException, NoSuchPaddingException,
        NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException {
    SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, keySpec);
    return cipher.doFinal(input);
}

From source file:Main.java

public static byte[] getDecCode(byte[] byteD, String seed) {
    byte[] byteFina = null;
    Cipher cipher = null;/*from  www. j  a  va  2s  . com*/

    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

/** 
 * decrypt /*w ww  .ja  v a 2 s .  com*/
 * @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);
}