Example usage for android.util Base64 encodeToString

List of usage examples for android.util Base64 encodeToString

Introduction

In this page you can find the example usage for android.util Base64 encodeToString.

Prototype

public static String encodeToString(byte[] input, int flags) 

Source Link

Document

Base64-encode the given data and return a newly allocated String with the result.

Usage

From source file:Main.java

/**
 * Encrypt and encode message using 256-bit AES with key generated from password.
 *
 *
 * @param password used to generated key
 * @param message the thing you want to encrypt assumed String UTF-8
 * @return Base64 encoded CipherText//from w w w . j  a  va 2 s .  co  m
 * @throws GeneralSecurityException if problems occur during encryption
 */
public static String encrypt(final String password, String message) {

    log("message", message);

    byte[] cipherText;
    try {
        final SecretKeySpec key = generateKey(password);
        cipherText = encrypt(key, ivBytes, nullPadString(message).getBytes(CHARSET));
        //NO_WRAP is important as was getting \n at the end
        String encoded = Base64.encodeToString(cipherText, Base64.NO_WRAP);
        log("Base64.NO_WRAP", encoded);
        return encoded;
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (GeneralSecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;

}

From source file:com.appdynamics.demo.gasp.twitter.TwitterAuthentication.java

public static String getEncodedBase64Credentials() {
    String authEncodedBase64 = "";
    try {/*www  . j a  va  2  s  . co  m*/
        String urlEncoded = URLEncoder.encode(consumerKey, charSet) + ":"
                + URLEncoder.encode(consumerSecret, charSet);
        byte[] urlEncodedBytes = urlEncoded.getBytes(charSet);
        authEncodedBase64 = "Basic " + Base64.encodeToString(urlEncodedBytes, Base64.NO_WRAP);
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "Check URL encoding", e);
    }
    return authEncodedBase64;
}

From source file:illab.nabal.util.SecurityHelper.java

/**
  * AES-encrypt a plain message. Return null if message is empty.
  * //from w w w.  j  a  va 2 s  .  c  o  m
  * @param message
  * @return AES encrypted hex
  * @throws Exception
  */
public static String cipher(String message) throws Exception {
    if (StringHelper.isEmpty(message) == false) {
        Cipher cipher = Cipher.getInstance(AES);
        cipher.init(Cipher.ENCRYPT_MODE, SYSTEM_SECRET_KEY_SPEC);
        return Base64.encodeToString(cipher.doFinal(message.getBytes()), Base64.DEFAULT);
    } else {
        return null;
    }
}

From source file:com.ternup.caddisfly.util.WebClient.java

private static void addCredentials(AsyncHttpClient client) {
    String credentials = Globals.CONNECT;
    String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);

    client.addHeader("Authorization", "Basic " + base64EncodedCredentials);
}

From source file:Main.java

/**
 * Returns the key hashes of the signatures of the app with the specified package name
 * which may be needed when integrating with 3rd party services such as Facebook. Note that
 * Android apps usually only have one signature.
 *
 * @param context/* w ww . ja va  2  s.c  om*/
 * @param packageName The package name of the app
 * @return The key hashes
 */
public static List<String> getKeyHashes(Context context, String packageName) {
    try {
        List<String> hashes = new ArrayList<>();
        PackageInfo info = context.getPackageManager().getPackageInfo(packageName,
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            hashes.add(Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
        return hashes;
    } catch (PackageManager.NameNotFoundException e) {
    } catch (NoSuchAlgorithmException e) {
    }
    return null;
}

From source file:com.phonegap.plugins.xapkreader.XAPKReader.java

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext)
        throws JSONException {
    if (action.equals("get")) {
        final String filename = args.getString(0);
        try {/*from  w w w.  j  a v a 2 s . c om*/
            Context ctx = cordova.getActivity().getApplicationContext();
            // Read file as array buffer
            byte[] data = XAPKReader.readFile(ctx, filename);
            if (null != data) {
                // Encode to Base64 string
                String encoded = Base64.encodeToString(data, Base64.DEFAULT);
                // Return file data as base64 string
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, encoded));
            } else {
                callbackContext.error("File not found.");
            }
        } catch (Exception e) {
            e.printStackTrace();
            callbackContext.error(e.getMessage());
        }
        return true;
    }
    return false;
}

From source file:eu.liveGov.libraries.livegovtoolkit.helper.DownloadHelper.java

private String[] getBasicAuthHeader(byte[] b64) {
    String s64 = Base64.encodeToString(b64, Base64.NO_WRAP);
    String[] s = { "Authorization", "Basic " + s64 };
    return s;//from  w  w  w. j  a v  a 2  s .c o  m
}

From source file:es.wolfi.passman.API.Core.java

public static void requestAPIGET(Context c, String endpoint, final FutureCallback<String> callback) {
    String auth = "Basic "
            .concat(Base64.encodeToString(username.concat(":").concat(password).getBytes(), Base64.NO_WRAP));

    Ion.with(c).load(host.concat(endpoint)).setHeader("Authorization", auth) // set the header
            .asString().setCallback(new FutureCallback<String>() {
                @Override//from   w ww .  j a va  2 s  . co  m
                public void onCompleted(Exception e, String result) {
                    if (e == null && JSONUtils.isJSONObject(result)) {
                        try {
                            JSONObject o = new JSONObject(result);
                            if (o.getString("message").equals("Current user is not logged in")) {
                                callback.onCompleted(new Exception("401"), null);
                                return;
                            }
                        } catch (JSONException e1) {

                        }
                    }

                    callback.onCompleted(e, result);
                }
            });
}

From source file:com.drisoftie.cwdroid.util.CredentialUtils.java

public static String obfuscateToBase64(byte[] key, String pure) {
    String result = null;//from w w w. ja v a 2s  . c o  m
    try {
        byte[] encrypted = encrypt(key, pure.getBytes());
        result = Base64.encodeToString(encrypted, Base64.DEFAULT);
    } catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException
            | IllegalBlockSizeException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.github.javiersantos.piracychecker.LibraryUtils.java

@SuppressLint("PackageManagerGetSignatures")
static String getCurrentSignature(Context context) {
    String res = "";
    try {/*w w w.j a  v a  2s .c  o  m*/
        PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature signature : packageInfo.signatures) {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA");
            messageDigest.update(signature.toByteArray());
            res = Base64.encodeToString(messageDigest.digest(), Base64.DEFAULT);
        }
    } catch (PackageManager.NameNotFoundException | NoSuchAlgorithmException ignored) {
    }
    return res.trim();
}