Android AES Encrypt encrypt(String data)

Here you can find the source of encrypt(String data)

Description

encrypt

Declaration

public static String encrypt(String data) 

Method Source Code

//package com.java2s;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;

public class Main {
    private static final String seed = "This is a secret";

    public static String encrypt(String data) {
        try {//from   w ww.  j  a  v  a 2 s  . co m
            SecretKeySpec sks = null;
            byte[] encodedBytes = null;

            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
            sr.setSeed(seed.getBytes());
            KeyGenerator kg = KeyGenerator.getInstance("AES");
            kg.init(128, sr);
            sks = new SecretKeySpec((kg.generateKey()).getEncoded(), "AES");

            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.ENCRYPT_MODE, sks);
            encodedBytes = c.doFinal(data.getBytes());

            return Base64.encodeToString(encodedBytes, Base64.DEFAULT);
        } catch (Exception e) {

        }

        return null;
    }
}

Related

  1. encrypt(String cleartext)
  2. encrypt(String content, String password)
  3. encrypt(String content, String password)
  4. encrypt(String content, String password)
  5. encrypt(String content, String password)
  6. encrypt(String dataPassword, String cleartext)
  7. encrypt(String key, String src)
  8. encrypt(String key, String src)
  9. encrypt(String seed, String clearText)