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.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    private static final String CipherMode = "AES/ECB/PKCS5Padding";

    public static byte[] encrypt(byte[] content, String password) {
        try {/*from w ww.  j  a  v a2s .c  o  m*/
            SecretKeySpec key = createKey(password);
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String encrypt(String content, String password) {
        byte[] data = null;
        try {
            data = content.getBytes("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        data = encrypt(data, password);
        String result = byte2hex(data);
        return result;
    }

    private static SecretKeySpec createKey(String password) {
        byte[] data = null;
        if (password == null) {
            password = "";
        }
        StringBuffer sb = new StringBuffer(32);
        sb.append(password);
        while (sb.length() < 32) {
            sb.append("0");
        }
        if (sb.length() > 32) {
            sb.setLength(32);
        }

        try {
            data = sb.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new SecretKeySpec(data, "AES");
    }

    public static String byte2hex(byte[] b) {
        StringBuffer sb = new StringBuffer(b.length * 2);
        String tmp = "";
        for (int n = 0; n < b.length; n++) {
            tmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (tmp.length() == 1) {
                sb.append("0");
            }
            sb.append(tmp);
        }
        return sb.toString().toUpperCase(); 
    }
}

Related

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