Android String Encrypt encrypt(String keyStr, String plainText)

Here you can find the source of encrypt(String keyStr, String plainText)

Description

encrypt

Declaration

public static String encrypt(String keyStr, String plainText) 

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 encrypt(String keyStr, String plainText) {
        byte[] encrypt = null;
        try {/*from  ww w .j a  v  a  2  s .c o m*/
            Key key = generateKey(keyStr);
            Cipher cipher = Cipher.getInstance(AESTYPE);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            encrypt = cipher.doFinal(plainText.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(Base64.encodeBase64(encrypt));
    }

    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. encrypt(String seed, String cleartext)
  2. encrypt(String str)