Android AES Decrypt decrypt(String seed, String encrypted)

Here you can find the source of decrypt(String seed, String encrypted)

Description

To decrypt a string.

License

Open Source License

Parameter

Parameter Description
seed the Key used to decrypt the data.
encrypted the encrypted text via function encrypt of this class.

Return

the decrypted text.

Declaration

public static String decrypt(String seed, String encrypted) 

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 {
    /**//from   www .  jav  a  2  s  .c  om
     * 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(String encrypted)
  2. decrypt(String key, String encrypted)
  3. decrypt(String key, String src)
  4. decrypt(String seed, String encrypted)
  5. decrypt(String seed, String encrypted)
  6. decrypt(String src)
  7. decrypt(String src)
  8. decrypt(byte[] content, String key)
  9. decrypt(byte[] content, String password)