Android AES Encrypt encrypt(byte[] content, String password)

Here you can find the source of encrypt(byte[] content, String password)

Description

encrypt

Declaration

public static byte[] encrypt(byte[] content, String password) 

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 byte[] encrypt(byte[] content, String password) {
        byte[] result = null;
        try {/*w  w  w .  jav  a  2s  . c  o  m*/
            SecretKeySpec key = new SecretKeySpec(
                    getRawKey(password.getBytes()), "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            result = cipher.doFinal(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    private static byte[] getRawKey(byte[] seed) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
        sr.setSeed(seed);
        kgen.init(128, sr);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return raw;
    }
}

Related

  1. encrypt(String src)
  2. encrypt(String src)
  3. encrypt(String text, String key)
  4. encrypt(byte[] content, String key)
  5. encrypt(byte[] content, String password)
  6. encrypt(byte[] data, Key key)
  7. encrypt(byte[] data, Key key, String cipherAlgorithm)
  8. encrypt(byte[] data, byte[] key)
  9. encrypt(byte[] data, byte[] key, String cipherAlgorithm)