Android AES Encrypt encrypt(String text, String key)

Here you can find the source of encrypt(String text, String key)

Description

encrypt

Declaration

public static byte[] encrypt(String text, String key)
            throws java.io.UnsupportedEncodingException,
            NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, InvalidAlgorithmParameterException,
            IllegalBlockSizeException, BadPaddingException 

Method Source Code

//package com.java2s;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidAlgorithmParameterException;
import java.security.spec.AlgorithmParameterSpec;

public class Main {
    private static byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

    public static byte[] encrypt(String text, String key)
            throws java.io.UnsupportedEncodingException,
            NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, InvalidAlgorithmParameterException,
            IllegalBlockSizeException, BadPaddingException {

        byte[] keyBytes = key.getBytes("UTF-8");
        byte[] textBytes = text.getBytes("UTF-8");

        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = null;//from   ww w.  ja  v  a2 s .c  o  m
        cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
        return cipher.doFinal(textBytes);
    }
}

Related

  1. encrypt(String seed, String clearText)
  2. encrypt(String seed, String clearText)
  3. encrypt(String seed, String cleartext)
  4. encrypt(String src)
  5. encrypt(String src)
  6. encrypt(byte[] content, String key)
  7. encrypt(byte[] content, String password)
  8. encrypt(byte[] content, String password)
  9. encrypt(byte[] data, Key key)