Android AES Decrypt decrypt(byte[] raw, byte[] encrypted)

Here you can find the source of decrypt(byte[] raw, byte[] encrypted)

Description

Actual decrypt data function.

License

Open Source License

Parameter

Parameter Description
raw the Raw byte data,
clear the Encrypted text byte data.

Return

the decrypted data.

Declaration

private static byte[] decrypt(byte[] raw, byte[] encrypted)
        throws Exception 

Method Source Code

//package com.java2s;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    /**/*  ww  w  .  j a v a2s.c o m*/
     * To decrypt a string.
     * @param seed the Key used to decrypt the data.
     * @param encrypted the encrypted text via function encrypt of this class.
     * @return the decrypted text.
     */
    public static String decrypt(String seed, String encrypted) {
        byte[] rawKey;
        try {
            rawKey = getRawKey(seed.getBytes());
            byte[] enc = toByte(encrypted);
            byte[] result = decrypt(rawKey, enc);
            String coentn = new String(result);
            return coentn;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Actual decrypt data function.
     * @param raw the Raw byte data,
     * @param clear the Encrypted text byte data.
     * @return the decrypted data.
     */
    private static byte[] decrypt(byte[] raw, byte[] encrypted)
            throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(
                new byte[cipher.getBlockSize()]));
        byte[] decrypted = cipher.doFinal(encrypted);
        return decrypted;
    }

    /**
     * To get a raw key.
     * @param seed the byte data of a seed.
     * @return return the seed's byte data.
     */
    private static byte[] getRawKey(byte[] seed) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        //SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
        sr.setSeed(seed);
        kgen.init(128, sr);
        SecretKey sKey = kgen.generateKey();
        byte[] raw = sKey.getEncoded();
        return raw;
    }

    /**
     * Retrieve the bytes data
     * @param hexString the source string
     * @return the source string's byte data.
     */
    public static byte[] toByte(String hexString) {
        int len = hexString.length() / 2;
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++)
            result[i] = Integer.valueOf(
                    hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
        return result;
    }
}

Related

  1. decrypt(byte[] key, byte[] encrypted)
  2. decrypt(byte[] key, byte[] src)
  3. decrypt(byte[] raw, byte[] encrypted)
  4. decrypt(byte[] raw, byte[] encrypted)
  5. decrypt(byte[] raw, byte[] encrypted)
  6. decrypt(byte[] textBytes, String key)
  7. decryptBase64(String content, String key)
  8. decryptBase64(String encryptBase64)
  9. decryptBase64Text(String key, String src)