Android AES Encrypt encryptNumberWithAES(String number)

Here you can find the source of encryptNumberWithAES(String number)

Description

encrypt Number With AES

License

Open Source License

Declaration

public static String encryptNumberWithAES(String number)
            throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException,
            BadPaddingException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;

public class Main {
    public static String encryptNumberWithAES(String number)
            throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException,
            BadPaddingException {
        // This method will return a base64 encoded value of the AES-encrypted string

        byte[] numberToEncryptBytes = number.getBytes();

        String keyString = "intrepidlearner1"; // The key is exactly 16 bytes long
        byte[] key = keyString.getBytes();

        Cipher c = Cipher.getInstance("AES");
        SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
        c.init(Cipher.ENCRYPT_MODE, keySpec);

        byte[] encryptedData = c.doFinal(numberToEncryptBytes);

        return android.util.Base64.encodeToString(encryptedData,
                Base64.DEFAULT);/*from   w w  w.  j a va2  s . c o m*/

    }
}

Related

  1. encryptBase64(byte[] source)
  2. encryptBytes(String seed, byte[] cleartext)
  3. encryptBytes(byte[] data, byte[] key)
  4. encryptHex(String content, String key)
  5. encryptHex(String content, String username, String password)
  6. encryptToBase64Text(String key, String src)
  7. encryptToBytes(String key, String src)
  8. encryptUrlEncode(String dataPassword, String cleartext)
  9. encryptWithKey(String key, String src)