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 String decryptData(String ciphertext, String password) throws Exception {
    int iterationCount = 100; //because polaroid
    int keyLength = 256;

    String[] fields = ciphertext.split("]");
    byte[] iv = Base64.decode(fields[0], 0);
    byte[] salt = Base64.decode(fields[1], 0);
    byte[] cipherBytes = Base64.decode(fields[2], 0);

    Log.d(TAG, "ciphertext: " + ciphertext + "\n" + "iv length is " + "\n" + iv.length);

    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
    SecretKey key = new SecretKeySpec(keyBytes, "AES");

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec ivParams = new IvParameterSpec(iv);
    cipher.init(Cipher.DECRYPT_MODE, key, ivParams);
    byte[] plaintext = cipher.doFinal(cipherBytes);
    String plainStr = new String(plaintext, "UTF-8");

    return plainStr;
}

From source file:br.com.zeros.tipsandtricks.cryto.EncryptionStrings.java

public static String decrypt(String encryptedValue) throws Exception {
    Key key = generateKey();/*from  ww  w  .j  a  va 2  s  . c om*/
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = new Base64().decode(encryptedValue);
    byte[] decValue = c.doFinal(decordedValue);//////////LINE 50
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

From source file:Main.java

public static byte[] decipherAes256(byte[] encrypedPwdBytes, String password) throws NullPointerException {

    if (password == null || password.length() == 0) {
        throw new NullPointerException("Please give Password");
    }//from ww  w . ja  va2s.  c o m

    if (encrypedPwdBytes == null || encrypedPwdBytes.length <= 0) {
        throw new NullPointerException("Please give encrypedPwdBytes");
    }

    try {
        SecretKey key = getKey(password);

        // IMPORTANT TO GET SAME RESULTS ON iOS and ANDROID
        final byte[] iv = new byte[16];
        Arrays.fill(iv, (byte) 0x00);
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);

        // cipher is not thread safe
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
        byte[] decryptedValueBytes = (cipher.doFinal(encrypedPwdBytes));

        return decryptedValueBytes;

    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Decrypts a encrypted and encoded message given a key.
 * @param cipherBytes//from w  ww. ja  v  a 2 s .c  o  m
 * @param key
 * @return
 * @throws NoSuchAlgorithmException
 * @throws NoSuchAlgorithmException
 * @throws InvalidAlgorithmParameterException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws InvalidAlgorithmParameterException
 * @throws NoSuchPaddingException
 * @throws InvalidKeyException
 */
public static byte[] decryptMessage(byte[] cipherBytes, SecretKey key) throws NoSuchAlgorithmException,
        NoSuchAlgorithmException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
        BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException {
    Cipher cipher = Cipher.getInstance("AES");
    //ipher cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING");
    byte[] init = new byte[128 / 8];
    SecureRandom secureRandom = new SecureRandom();
    //secureRandom.nextBytes(init);
    for (int i = 0; i < 16; i++)
        init[i] = 0;
    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(init));
    byte[] textBytes = cipher.doFinal(cipherBytes);
    return textBytes;
}

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

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

    if (value == null || value.equals(""))
        return "";

    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
    IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
    cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
    byte[] outText;
    outText = cipher.doFinal(hexToBytes(value));
    return new String(outText);

}

From source file:Main.java

public static String decryp(String key, String str) throws java.io.UnsupportedEncodingException,
        NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    String aesKey;/*from  ww  w.j av a 2s.  c  om*/
    aesKey = key.substring(0, 16);
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, AESkey(key), new IvParameterSpec(aesKey.getBytes("UTF-8")));

    byte[] byteStr = Base64.decode(str.getBytes(), 0);
    String deStr = new String(c.doFinal(byteStr), "UTF-8");

    return deStr;
}

From source file:Logic.security.java

public static String symmetricDecrypt(String text, String secretKey) {
    Cipher cipher;// w  ww  . ja v  a2s  . c  om
    String encryptedString;
    byte[] encryptText = null;
    byte[] raw;
    SecretKeySpec skeySpec;
    try {
        raw = Base64.decodeBase64(secretKey);
        skeySpec = new SecretKeySpec(raw, "AES");
        encryptText = Base64.decodeBase64(text);
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        encryptedString = new String(cipher.doFinal(encryptText));
    } catch (Exception e) {
        e.printStackTrace();
        return "Error";
    }
    return encryptedString;
}

From source file:clases.Seguridad.java

public static String desencriptar(String encrypted) throws Exception {
    Cipher cipher = Cipher.getInstance(CI);
    SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes(), ALG);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(IV.getBytes());
    byte[] enc = decodeBase64(encrypted);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] decrypted = cipher.doFinal(enc);
    return new String(decrypted);
}

From source file:com.fpt.crypto.CipherDemo.java

public static String decrypt(String in, String key) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException, DecoderException {
    Cipher cipher = Cipher.getInstance("AES");
    byte[] inputBytes = Hex.decodeHex(in.toCharArray());
    SecretKey secKey = new SecretKeySpec(key.getBytes(), "AES");
    cipher.init(Cipher.DECRYPT_MODE, secKey);
    byte[] outputBytes = cipher.doFinal(inputBytes);
    return new String(outputBytes);

}

From source file:Main.java

public static void decrypt(String fileIn, String fileOut, byte key[])
        throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream(fileIn);
    FileOutputStream fos = new FileOutputStream(fileOut);

    // Length is 32 bytes
    //byte key[] = "1010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100000011010110101010101010100101111001001001001001001011110111100001111000011".getBytes("UTF-8");
    MessageDigest sha = MessageDigest.getInstance("SHA-256");
    key = sha.digest(key);/*  w  w  w. j av a2  s  .  c  o m*/
    SecretKeySpec sks = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);

    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while ((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }

    fos.flush();
    fos.close();
    cis.close();
}