Decrypts the text using AES - Android java.security

Android examples for java.security:AES

Description

Decrypts the text using AES

Demo Code


//package com.java2s;
import android.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    /**// w  w w  . ja va2 s  . c  o  m
     * Decrypts the text
     *
     * @param seed          The secret/seed key
     * @param encryptedText The text you want to encrypt
     * @return Decrypted data if successful, or null if unsucessful
     */
    public static String decrypt(String seed, String encryptedText) {
        byte[] clearText;
        try {
            byte[] keyData = seed.getBytes();
            SecretKey ks = new SecretKeySpec(keyData, "AES");
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.DECRYPT_MODE, ks);
            clearText = c.doFinal(Base64.decode(encryptedText,
                    Base64.DEFAULT));
            return new String(clearText, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials