Android AES Encrypt crypt(byte[] text, byte[] key, int mode)

Here you can find the source of crypt(byte[] text, byte[] key, int mode)

Description

crypt

Declaration

@SuppressLint("TrulyRandom")
    public static byte[] crypt(byte[] text, byte[] key, int mode)
            throws NoSuchAlgorithmException, NoSuchProviderException,
            NoSuchPaddingException, InvalidKeyException,
            IllegalBlockSizeException, BadPaddingException 

Method Source Code

//package com.java2s;
import android.annotation.SuppressLint;
import java.security.InvalidKeyException;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    @SuppressLint("TrulyRandom")
    public static byte[] crypt(byte[] text, byte[] key, int mode)
            throws NoSuchAlgorithmException, NoSuchProviderException,
            NoSuchPaddingException, InvalidKeyException,
            IllegalBlockSizeException, BadPaddingException {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
        sr.setSeed(key);//  w ww .  j  ava 2s  .c  o  m
        kgen.init(256, sr);
        SecretKey sk = kgen.generateKey();
        SecretKeySpec k = new SecretKeySpec(sk.getEncoded(), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(mode, k);
        return cipher.doFinal(text);
    }
}

Related

  1. encryptNumberWithAES(String number)
  2. encryptToBase64Text(String key, String src)
  3. encryptToBytes(String key, String src)
  4. encryptUrlEncode(String dataPassword, String cleartext)
  5. encryptWithKey(String key, String src)
  6. aesEncode(String seed, String cleartext)
  7. encrypt(byte[] raw, byte[] clear)
  8. encrypt(Context context, String text)
  9. encrypt(Context context, String text)