Android String Decrypt decrypt(String keyStr, String encryptData)

Here you can find the source of decrypt(String keyStr, String encryptData)

Description

decrypt

Declaration

public static String decrypt(String keyStr, String encryptData) 

Method Source Code

//package com.java2s;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

public class Main {
    private static final String AESTYPE = "AES/ECB/PKCS5Padding";

    public static String decrypt(String keyStr, String encryptData) {
        byte[] decrypt = null;
        try {/*from ww w.  j  a  va 2  s.co  m*/
            Key key = generateKey(keyStr);
            Cipher cipher = Cipher.getInstance(AESTYPE);
            cipher.init(Cipher.DECRYPT_MODE, key);
            decrypt = cipher.doFinal(Base64.decodeBase64(encryptData));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(decrypt).trim();
    }

    private static Key generateKey(String key) throws Exception {
        try {
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
            return keySpec;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }
}

Related

  1. decrypt(String seed, String encrypted)
  2. decrypt(String old)