Android AES Decrypt decrypt(String src)

Here you can find the source of decrypt(String src)

Description

decrypt

Declaration

public static String decrypt(String src) 

Method Source Code

//package com.java2s;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;

public class Main {
    private static final String ENCRYPTION_KEY = "RasdfVpg";
    private static final String ENCRYPTION_IV = "4e5WasdfasdfMFEX";

    public static String decrypt(String src) {
        String decrypted = "";
        try {/*from  w  ww  .j a v a2  s.  c o  m*/
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, makeKey(), makeIv());
            decrypted = new String(cipher.doFinal(Base64.decode(src,
                    Base64.NO_WRAP)));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return decrypted;
    }

    static Key makeKey() {
        return makeKey(ENCRYPTION_KEY);
    }

    static Key makeKey(String passkey) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] key = md.digest(passkey.getBytes("UTF-8"));
            return new SecretKeySpec(key, "AES");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return null;
    }

    static AlgorithmParameterSpec makeIv() {
        return makeIv(ENCRYPTION_IV);
    }

    static AlgorithmParameterSpec makeIv(String ivString) {
        try {
            return new IvParameterSpec(ivString.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. decrypt(String key, String encrypted)
  2. decrypt(String key, String src)
  3. decrypt(String seed, String encrypted)
  4. decrypt(String seed, String encrypted)
  5. decrypt(String seed, String encrypted)
  6. decrypt(String src)
  7. decrypt(byte[] content, String key)
  8. decrypt(byte[] content, String password)
  9. decrypt(byte[] content, String password)