Android AES Encrypt encryptUrlEncode(String dataPassword, String cleartext)

Here you can find the source of encryptUrlEncode(String dataPassword, String cleartext)

Description

encrypt Url Encode

License

Apache License

Declaration

public static String encryptUrlEncode(String dataPassword,
        String cleartext) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import java.net.URLEncoder;

public class Main {
    private static final byte[] ENCRYPT_IV = { 0x0, 0x0, 0x0, 0x0, 0x0,
            0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
    private static final String CHARSET = "UTF-8";

    public static String encryptUrlEncode(String dataPassword,
            String cleartext) throws Exception {

        return URLEncoder.encode(encrypt(dataPassword, cleartext), CHARSET);
    }/*from   w  ww.  j a v  a  2s .co m*/

    public static String encrypt(String dataPassword, String cleartext)
            throws Exception {
        IvParameterSpec zeroIv = new IvParameterSpec(ENCRYPT_IV);
        SecretKeySpec key = new SecretKeySpec(dataPassword.getBytes(),
                "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
        byte[] encryptedData = cipher.doFinal(cleartext.getBytes(CHARSET));

        return new String(Base64.encodeBase64(encryptedData));
    }
}

Related

  1. encryptHex(String content, String key)
  2. encryptHex(String content, String username, String password)
  3. encryptNumberWithAES(String number)
  4. encryptToBase64Text(String key, String src)
  5. encryptToBytes(String key, String src)
  6. encryptWithKey(String key, String src)
  7. crypt(byte[] text, byte[] key, int mode)
  8. aesEncode(String seed, String cleartext)
  9. encrypt(byte[] raw, byte[] clear)