Android AES Encrypt encryptedData(String userkey, String userData)

Here you can find the source of encryptedData(String userkey, String userData)

Description

encrypted Data

Declaration

public static String encryptedData(String userkey, String userData) 

Method Source Code

//package com.java2s;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class Main {
    private static final String APP_KEY = "yourName";
    private static final String ENCODING = "ISO-8859-1";

    public static String encryptedData(String userkey, String userData) {
        String encryptedUserData = "";
        try {//from   www . j a v  a 2s.  c o m

            String key = getAppPassCode(userkey);
            // Create key and cipher
            Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES");

            // encrypt the text
            cipher.init(Cipher.ENCRYPT_MODE, aesKey);
            byte[] encrypted = cipher.doFinal(userData.getBytes());
            encryptedUserData = new String(encrypted, ENCODING);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedUserData;
    }

    private static String getAppPassCode(String userPassCode) {
        String appPassCode = userPassCode;
        if (userPassCode.length() < 16) {
            appPassCode = appPassCode
                    + APP_KEY.substring(0,
                            (APP_KEY.length() - userPassCode.length()));
        }
        return appPassCode;
    }
}

Related

  1. crypt(byte[] text, byte[] key, int mode)
  2. aesEncode(String seed, String cleartext)
  3. encrypt(byte[] raw, byte[] clear)
  4. encrypt(Context context, String text)
  5. encrypt(Context context, String text)
  6. encryptedPassword(String key, String userData)
  7. generateEncryptionSecret()
  8. encode(String seed, String cleartext)