Example usage for android.util Base64 encode

List of usage examples for android.util Base64 encode

Introduction

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

Prototype

public static byte[] encode(byte[] input, int flags) 

Source Link

Document

Base64-encode the given data and return a newly allocated byte[] with the result.

Usage

From source file:Main.java

private static byte[] base64Encode(byte[] input) {
    return Base64.encode(input, Base64.NO_WRAP);
}

From source file:Main.java

public static String objectToString(Serializable serializableObject) throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
    objectOutputStream.writeObject(serializableObject);
    objectOutputStream.close();//from  w w  w  .  j  av  a 2  s.  com
    return new String(Base64.encode(byteArrayOutputStream.toByteArray(), 0));
}

From source file:Main.java

public static void writeSObject(Context context, Object content, String path) {

    try {//from w w w.j  a v  a2 s  .co  m
        ByteArrayOutputStream e = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(e);
        oos.writeObject(content);
        String stringBase64 = new String(Base64.encode(e.toByteArray(), 0));
        writeString(context, stringBase64, path);
    } catch (IOException var6) {
        var6.printStackTrace();
    }

}

From source file:Main.java

public static byte[] encodeBase64(byte[] input) {
    return Base64.encode(input, Base64.DEFAULT);
}

From source file:Main.java

public static String sign(String paramString1, String paramString2, String paramString3) {
    if (paramString3 == null)
        paramString3 = "";
    try {/*from  ww  w .j ava2s . c o  m*/
        SecretKeySpec localSecretKeySpec = new SecretKeySpec(
                (urlencode(paramString2, "UTF8") + '&' + urlencode(paramString3, "UTF8")).getBytes("UTF8"),
                "HmacSHA1");
        Mac localMac = Mac.getInstance("HmacSHA1");
        localMac.init(localSecretKeySpec);
        String str = urlencode(
                new String(Base64.encode(localMac.doFinal(paramString1.getBytes("UTF8")), 0), "UTF8"), "UTF8");
        return str;
    } catch (InvalidKeyException localInvalidKeyException) {
        return "";
    } catch (NoSuchAlgorithmException localNoSuchAlgorithmException) {
        return "";
    } catch (UnsupportedEncodingException localUnsupportedEncodingException) {
    }
    return "";
}

From source file:Main.java

public static String uploadFile(String path) {
    String sourceFileUri = path;// w  w w .j av  a  2 s. c o  m
    File file = new File(sourceFileUri);
    ByteArrayOutputStream objByteArrayOS = null;
    FileInputStream objFileIS = null;
    boolean isSuccess = false;
    String strAttachmentCoded = null;

    if (!file.isFile()) {
        Log.w(TAG, "Source File not exist :" + sourceFileUri);
    } else {
        try {
            objFileIS = new FileInputStream(file);
            objByteArrayOS = new ByteArrayOutputStream();
            byte[] byteBufferString = new byte[(int) file.length()];
            objFileIS.read(byteBufferString);

            byte[] byteBinaryData = Base64.encode(byteBufferString, Base64.DEFAULT);
            strAttachmentCoded = new String(byteBinaryData);

            isSuccess = true;
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Upload file to server", "error: " + e.getMessage(), e);
        } finally {
            try {
                objByteArrayOS.close();
                objFileIS.close();
            } catch (IOException e) {
                Log.e(TAG, "Error : " + e.getMessage());
            }
        }
    }

    if (isSuccess) {
        return strAttachmentCoded;
    } else {
        return "No Picture";
    }
}

From source file:Main.java

public static String getKeyFromParams(List<NameValuePair> nameValuePairs, String publishId) {
    Collections.sort(nameValuePairs, new Comparator<NameValuePair>() {
        @Override//w  w  w.j  a va  2 s  . c  om
        public int compare(NameValuePair p1, NameValuePair p2) {
            return p1.getName().compareTo(p2.getName());
        }
    });

    StringBuilder keyBuilder = new StringBuilder();
    boolean isFirst = true;
    for (NameValuePair nvp : nameValuePairs) {
        if (!isFirst) {
            keyBuilder.append("&");
        }
        keyBuilder.append(nvp.getName()).append("=").append(nvp.getValue());
        isFirst = false;
    }

    keyBuilder.append("&").append(publishId);

    String key = keyBuilder.toString();
    byte[] keyBytes = getBytes(key);
    return getMd5Digest(new String(Base64.encode(keyBytes, Base64.NO_WRAP)));
}

From source file:Main.java

public static byte[] encrypt(String clearText) {
    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
    Cipher cipher = null;//from  w ww  .j  a  v  a  2 s  .  c  om
    byte[] cipherText = null;

    try {
        // init cipher
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        cipherText = cipher.doFinal(clearText.getBytes());
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    }
    return Base64.encode(cipherText, 10);
}

From source file:Main.java

public static String encryptData(String password, String plaintextData) throws Exception {
    // Thank you Mr. Nelenkov
    String maybeThisHelps = "http://nelenkov.blogspot.com/2012/04/using-password-based-encryption-on.html";
    Log.v(TAG, maybeThisHelps);/*from w w  w  . j a va  2s.  c  o  m*/
    int iterationCount = 100; //because Polaroid
    int keyLength = 256;
    int saltLength = keyLength;

    SecureRandom random = new SecureRandom();
    byte[] salt = new byte[saltLength];
    random.nextBytes(salt);
    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
    SecretKey key = new SecretKeySpec(keyBytes, "AES");

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    byte[] iv = new byte[cipher.getBlockSize()];
    random.nextBytes(iv);

    IvParameterSpec ivParams = new IvParameterSpec(iv);
    cipher.init(Cipher.ENCRYPT_MODE, key, ivParams);
    byte[] ciphertext = cipher.doFinal(plaintextData.getBytes("UTF-8"));

    String ivToString = new String(Base64.encode(iv, 0));
    String saltToString = new String(Base64.encode(salt, 0));
    String ciphertextToString = new String(Base64.encode(ciphertext, 0));

    Log.d(TAG, ivToString + "]" + saltToString + "]" + ciphertextToString);
    return (ivToString + "]" + saltToString + "]" + ciphertextToString).replace("\n", "");
}

From source file:org.starfishrespect.myconsumption.android.util.CryptoUtils.java

/**
 * Create header for basic authentication with username and password
 * @return HttpHeaders with basic authentication
 *//*w w  w .j a v  a  2  s .co  m*/
public static HttpHeaders createHeaders(final String username, final String password) {
    HttpHeaders headers = new HttpHeaders() {
        {
            String auth = username + ":" + password;
            byte[] encodedAuth = Base64.encode(auth.getBytes(Charset.forName("US-ASCII")), Base64.NO_WRAP);
            String authHeader = "Basic " + new String(encodedAuth);
            //String authHeader = "Basic " + auth;
            set("Authorization", authHeader);
        }
    };
    headers.add("Content-Type", "application/json");
    headers.add("Accept", "*/*");

    return headers;
}