Android AES Decrypt decrypt(Context context, String encrypted)

Here you can find the source of decrypt(Context context, String encrypted)

Description

decrypt

Declaration

public static String decrypt(Context context, String encrypted)
            throws Exception 

Method Source Code

//package com.java2s;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import android.content.Context;
import android.content.pm.PackageManager;

import android.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

public class Main {
    public static String decrypt(Context context, String encrypted)
            throws Exception {
        SecretKey skeySpec = generateKey(getSeed(context),
                "edu.uoc.skeleton".getBytes());
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] decordedValue = Base64.decode(encrypted.getBytes(),
                Base64.DEFAULT);//www. ja  va 2s  .  c o  m
        byte[] decValue = cipher.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        String decoded = new String(Base64.decode(decryptedValue,
                Base64.DEFAULT));
        return decoded;
    }

    public static SecretKey generateKey(char[] passphraseOrPin, byte[] salt)
            throws NoSuchAlgorithmException, InvalidKeySpecException {

        final int iterations = 1000;

        final int outputKeyLength = 128;

        SecretKeyFactory secretKeyFactory = SecretKeyFactory
                .getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec(passphraseOrPin, salt, iterations,
                outputKeyLength);
        SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
        return secretKey;
    }

    private static char[] getSeed(Context context) {
        long installed = 0;
        try {
            installed = context.getPackageManager().getPackageInfo(
                    "edu.uoc.skeleton", 0).firstInstallTime;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return Long.toString(installed).toCharArray();
    }
}

Related

  1. decryptNumberWithAES(String encrypted)
  2. decryptUrlDecode(String dataPassword, String encrypted)
  3. aesDecode(String seed, String encrypted)
  4. decrypt(byte[] raw, byte[] encrypted)
  5. decrypt(Context context, String encrypted)
  6. decode(String seed, String encrypted)
  7. decrypt(byte[] raw, byte[] encrypted)
  8. decryptedData(String userkey, String encryptedData)
  9. decryptedPassword(String key, String encryptedData)