AES encrypt text with secret key - Android java.security

Android examples for java.security:AES

Description

AES encrypt text with secret key

Demo Code


//package com.java2s;
import android.util.Base64;
import android.util.Log;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

public class Main {
    public static String v_encrypt(String text, SecretKeySpec sks) {
        byte[] encodedBytes = null;
        try {/*from   w  ww .ja  va2  s .c  o m*/
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.ENCRYPT_MODE, sks);
            encodedBytes = c.doFinal(text.getBytes());
        } catch (Exception e) {
            Log.e("", "AES encryption error");
            e.printStackTrace();
        }
        return Base64.encodeToString(encodedBytes, Base64.DEFAULT);
    }
}

Related Tutorials