AES decrypt text with secret key - Android java.security

Android examples for java.security:AES

Description

AES decrypt 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_decrypt(String text, SecretKeySpec sks) {
        byte[] decodedBytes = null;
        byte[] encodedBytes = null;
        encodedBytes = Base64.decode(text, Base64.DEFAULT);
        try {//from  w ww.j  a  v a  2 s .  c  o  m
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.DECRYPT_MODE, sks);
            decodedBytes = c.doFinal(encodedBytes);
        } catch (Exception e) {
            Log.e("", "AES decryption error");
            e.printStackTrace();
        }
        return new String(decodedBytes);
    }
}

Related Tutorials