Android AES Decrypt decryptBytes(String key, byte[] src)

Here you can find the source of decryptBytes(String key, byte[] src)

Description

decrypt Bytes

Declaration

public static String decryptBytes(String key, byte[] src)
            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.SecretKeySpec;

public class Main {
    public static String decryptBytes(String key, byte[] src)
            throws Exception {
        byte[] rawKey = getRawKey(key);
        return new String(decrypt(rawKey, src));
    }/*from   ww  w  .  jav a 2s .  c o m*/

    private static byte[] getRawKey(String key) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        sr.setSeed(key.getBytes());
        kgen.init(256, sr);
        SecretKey skey = kgen.generateKey();
        return skey.getEncoded();
    }

    private static byte[] decrypt(byte[] key, byte[] src) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        return cipher.doFinal(src);
    }
}

Related

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