encrypt String Data using AES/ECB/PKCS5Padding - Android java.security

Android examples for java.security:AES

Description

encrypt String Data using AES/ECB/PKCS5Padding

Demo Code


import android.annotation.SuppressLint;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Main{
    /*from   w w w. j  a  v  a  2  s  .  co  m*/
    public synchronized static String encryptData(String content) {

        int index = EncryptUtil.createRandomkeySort();
        String data = "";
        data = EncryptUtil.encrypt(content, index);

        return index + data;
    }
    
    public static int createRandomkeySort() {
        int randomNum = (int) (Math.random() * 10);
        return randomNum;
    }
    
    @SuppressLint("TrulyRandom")
    private static String encrypt(String input, int index) {
        byte[] crypted = null;
        try {
            SecretKeySpec skey = new SecretKeySpec(
                    DynamicEncryptUtil.findkeyBySortNum(index), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            crypted = cipher.doFinal(input.getBytes());
        } catch (Exception e) {
        }
        return new String(Base64.encode(crypted));
    }
}

Related Tutorials