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[] decrypt(byte[] byteArray, PublicKey publicKey) {
    Cipher cipher = null;/* w  ww  .  ja  v a 2s. c  o  m*/

    try {
        cipher = Cipher.getInstance("RSA/ECB/NoPadding");
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {

        cipher.init(Cipher.DECRYPT_MODE, publicKey);

    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
    ByteArrayOutputStream output = new ByteArrayOutputStream();

    try {

        while (input.available() != 0) {
            byte[] t0 = new byte[128];
            input.read(t0);
            output.write(cipher.doFinal(t0));
        }

    } catch (IllegalBlockSizeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadPaddingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return output.toByteArray();
}

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

From source file:Main.java

private static String decrypt(byte[] encryptionBytes)
        throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] recoveredBytes = cipher.doFinal(encryptionBytes);
    String recovered = new String(recoveredBytes);
    return recovered;
}

From source file:Main.java

static byte[] decryptRsaB64(String s64, Key privRsaKey) {
    try {/*from  w  ww  .j av  a2 s .co  m*/
        byte[] sBytes = decodeB64(s64);
        if (sBytes != null) {
            Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "SC");
            cipher.init(Cipher.DECRYPT_MODE, privRsaKey);
            return cipher.doFinal(sBytes);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] TDesDec(byte[] key, byte[] in) throws Exception {

    Key deskey = null;/*from w  ww . j  av  a2 s  .  c om*/
    byte[] tdesKey = new byte[24];
    if (key.length % 8 != 0)
        return null;

    if (key.length == 8) {
        System.arraycopy(key, 0, tdesKey, 0, 8);
        System.arraycopy(key, 0, tdesKey, 8, 8);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    if (key.length == 16) {
        System.arraycopy(key, 0, tdesKey, 0, 16);
        System.arraycopy(key, 0, tdesKey, 16, 8);
    }

    DESedeKeySpec spec = new DESedeKeySpec(tdesKey);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");
    deskey = keyfactory.generateSecret(spec);

    Cipher cipher = Cipher.getInstance("desede" + "/ECB/NoPadding");

    cipher.init(Cipher.DECRYPT_MODE, deskey);
    byte[] bOut = cipher.doFinal(in);

    return bOut;
}

From source file:com.sds.acube.ndisc.xnapi.XNApiDesCipher.java

/**
 * /*  w w  w  .ja v a  2 s  . c  om*/
 * 
 * @param data  ?
 * @return  ?
 */
public static String decrypt(String data) {
    try {
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, getKey());
        byte d[] = cipher.doFinal(Base64.decodeBase64(data.getBytes()));
        return new String(d);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:MainClass.java

private static String decrypt(char[] password, String text) throws Exception {
    String salt = text.substring(0, 12);
    String ciphertext = text.substring(12, text.length());
    BASE64Decoder decoder = new BASE64Decoder();
    byte[] saltArray = decoder.decodeBuffer(salt);
    byte[] ciphertextArray = decoder.decodeBuffer(ciphertext);

    PBEKeySpec keySpec = new PBEKeySpec(password);

    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");

    SecretKey key = keyFactory.generateSecret(keySpec);

    PBEParameterSpec paramSpec = new PBEParameterSpec(saltArray, 1000);

    Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
    cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);

    return new String(cipher.doFinal(ciphertextArray));
}

From source file:Main.java

public static byte[] aesIGEdecrypt(byte[] tmpAESiv, byte[] tmpAesKey, byte[] data) {
    try {/*from w w w . j  av  a  2s .  c  o m*/

        ByteBuffer out = ByteBuffer.allocate(data.length);

        byte[] iv2p = Arrays.copyOfRange(tmpAESiv, 0, tmpAESiv.length / 2);
        byte[] ivp = Arrays.copyOfRange(tmpAESiv, tmpAESiv.length / 2, tmpAESiv.length);

        int len = data.length / AES_BLOCK_SIZE;

        byte[] xorInput = null;
        byte[] xorOutput = null;

        SecretKeySpec keySpec = null;
        keySpec = new SecretKeySpec(tmpAesKey, "AES");
        Cipher cipher = null;
        cipher = Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        byte[] input = null;
        byte[] output = null;

        for (int i = 0; i < len; i++) {
            input = Arrays.copyOfRange(data, i * AES_BLOCK_SIZE, (i + 1) * AES_BLOCK_SIZE);
            xorInput = xor(input, ivp);
            output = cipher.doFinal(xorInput);
            xorOutput = xor(output, iv2p);
            out.put(xorOutput);

            ivp = xorOutput;
            iv2p = input;
        }
        return out.array();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static byte[] DESTemplet(byte[] data, byte[] key, String algorithm, String transformation,
        boolean isEncrypt) {
    try {//www .ja v  a  2s  .c om
        SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
        Cipher cipher = Cipher.getInstance(transformation);
        SecureRandom random = new SecureRandom();
        cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random);
        return cipher.doFinal(data);
    } catch (Throwable e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static byte[] desTemplate(byte[] data, byte[] key, String algorithm, String transformation,
        boolean isEncrypt) {
    if (data == null || data.length == 0 || key == null || key.length == 0)
        return null;
    try {/*  w  ww  .ja  va 2 s. c  om*/
        SecretKeySpec keySpec = new SecretKeySpec(key, algorithm);
        Cipher cipher = Cipher.getInstance(transformation);
        SecureRandom random = new SecureRandom();
        cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random);
        return cipher.doFinal(data);
    } catch (Throwable e) {
        e.printStackTrace();
        return null;
    }
}