Java Code Examples for android.util.Base64#encode()

The following examples show how to use android.util.Base64#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenWithActivity.java    From dropbox-sdk-java with MIT License 6 votes vote down vote up
protected String encodeOpenWithIntent(Intent owpIntent) {
    /*
     * Encode OpenWith intent
     * Real 3rd party apps should never run this function as DropboxApp does this entirely
     */

    Bundle extras = owpIntent.getExtras();
    extras.putString("_action", owpIntent.getAction());
    extras.putParcelable("_uri", owpIntent.getData());
    extras.putString("_type", owpIntent.getType());

    // marshall it!
    final Parcel parcel = Parcel.obtain();
    parcel.writeBundle(extras);
    byte[] b = parcel.marshall();
    parcel.recycle();
    return new String(Base64.encode(b, 0));
}
 
Example 2
Source File: PasswordNetworkManager.java    From Shaarlier with GNU General Public License v3.0 6 votes vote down vote up
PasswordNetworkManager(@NonNull ShaarliAccount account) {
    this.mShaarliUrl = account.getUrlShaarli();
    this.mUsername = account.getUsername();
    this.mPassword = account.getPassword();
    this.mValidateCert = account.isValidateCert();

    if (!"".equals(account.getBasicAuthUsername()) && !"".equals(account.getBasicAuthPassword())) {
        String login = account.getBasicAuthUsername() + ":" + account.getBasicAuthPassword();
        this.mBasicAuth = new String(Base64.encode(login.getBytes(), Base64.NO_WRAP));
    } else {
        this.mBasicAuth = "";
    }
}
 
Example 3
Source File: GeolocationHeader.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an X-Geo HTTP header string if:
 *  1. The current mode is not incognito.
 *  2. The url is a google search URL (e.g. www.google.co.uk/search?q=cars), and
 *  3. The user has not disabled sharing location with this url, and
 *  4. There is a valid and recent location available.
 *
 * Returns null otherwise.
 *
 * @param context The Context used to get the device location.
 * @param url The URL of the request with which this header will be sent.
 * @param isIncognito Whether the request will happen in an incognito tab.
 * @return The X-Geo header string or null.
 */
public static String getGeoHeader(Context context, String url, boolean isIncognito) {
    if (!isGeoHeaderEnabledForUrl(context, url, isIncognito, true)) {
        return null;
    }

    // Only send X-Geo header if there's a fresh location available.
    Location location = GeolocationTracker.getLastKnownLocation(context);
    if (location == null) {
        recordHistogram(UMA_LOCATION_NOT_AVAILABLE);
        return null;
    }
    if (GeolocationTracker.getLocationAge(location) > MAX_LOCATION_AGE) {
        recordHistogram(UMA_LOCATION_STALE);
        return null;
    }

    recordHistogram(UMA_HEADER_SENT);

    // Timestamp in microseconds since the UNIX epoch.
    long timestamp = location.getTime() * 1000;
    // Latitude times 1e7.
    int latitude = (int) (location.getLatitude() * 10000000);
    // Longitude times 1e7.
    int longitude = (int) (location.getLongitude() * 10000000);
    // Radius of 68% accuracy in mm.
    int radius = (int) (location.getAccuracy() * 1000);

    // Encode location using ascii protobuf format followed by base64 encoding.
    // https://goto.google.com/partner_location_proto
    String locationAscii = String.format(Locale.US,
            "role:1 producer:12 timestamp:%d latlng{latitude_e7:%d longitude_e7:%d} radius:%d",
            timestamp, latitude, longitude, radius);
    String locationBase64 = new String(Base64.encode(locationAscii.getBytes(), Base64.NO_WRAP));

    return "X-Geo: a " + locationBase64;
}
 
Example 4
Source File: CryptDES.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Decrypt a base64 encoded, DES encrypted string and return
 * the unencrypted string.
 * @param encryptedString The base64 encoded string to decrypt.
 * @return String The decrypted string.
 * @throws Exception If an error occurs.
 */
public String decryptBase64 (String encryptedString) throws Exception {
    // Encode bytes to base64 to get a string
    byte [] decodedBytes = Base64.encode(encryptedString.getBytes(), Base64.DEFAULT);
    // Decrypt
    byte[] unencryptedByteArray = decryptCipher.doFinal(decodedBytes);
    // Decode using utf-8
    return new String(unencryptedByteArray, "UTF8");
}
 
Example 5
Source File: CameraLauncher.java    From cordova-android-chromeview with Apache License 2.0 6 votes vote down vote up
/**
 * Compress bitmap using jpeg, convert to Base64 encoded string, and return to JavaScript.
 *
 * @param bitmap
 */
public void processPicture(Bitmap bitmap) {
    ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
    try {
        if (bitmap.compress(CompressFormat.JPEG, mQuality, jpeg_data)) {
            byte[] code = jpeg_data.toByteArray();
            byte[] output = Base64.encode(code, Base64.DEFAULT);
            String js_out = new String(output);
            this.callbackContext.success(js_out);
            js_out = null;
            output = null;
            code = null;
        }
    } catch (Exception e) {
        this.failPicture("Error compressing image.");
    }
    jpeg_data = null;
}
 
Example 6
Source File: BServiceUtil.java    From unity-bluetooth with MIT License 6 votes vote down vote up
public static String encodeBase64(byte[] data) {
    if (data == null) {
        return null;
    }
    byte[] encode = Base64.encode(data, Base64.DEFAULT);
    String str = null;
    try {
        str = new String(encode, "UTF-8");
    } catch (UnsupportedEncodingException e) {
    }
    return str;
}
 
Example 7
Source File: LoginViewModel.java    From OpenYOLO-Android with Apache License 2.0 5 votes vote down vote up
private String base64Sha256Hash(byte[] data) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        byte[] sha256Bytes = digest.digest(data);
        return new String(
                Base64.encode(sha256Bytes, Base64.NO_WRAP | Base64.URL_SAFE),
                Charset.forName("UTF-8"));
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalStateException("SHA-256 not supported on this platform!");
    }
}
 
Example 8
Source File: Base64Util.java    From fooXposed with Apache License 2.0 5 votes vote down vote up
public static String encrypt(String text) {
    if (TextUtils.isEmpty(text)) {
        return text;
    } else {
        return new String(Base64.encode(text.getBytes(UTF8), Base64.NO_WRAP), UTF8);
    }
}
 
Example 9
Source File: TripleDES.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public static byte[] Base64encodingByte(byte[] context,int type) {
    byte[] result;
    if (judgeVerionOfSdk() > 17) {
        result= Base64.encode(context, type);
    } else {
        result= com.marshalchen.common.commonUtils.urlUtils.Base64.encodeBytesToBytes(context);
    }
    return result;
}
 
Example 10
Source File: AESEncrypting.java    From OpenHub with GNU General Public License v3.0 5 votes vote down vote up
/**
 * AES加密
 * @param input 加密字符串
 * @param key 密钥,密钥必须是16位的
 * @return
 */
public static String encrypt(@NonNull String input, @NonNull String key){
  byte[] crypted = null;
  try{
	  SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
      cipher.init(Cipher.ENCRYPT_MODE, skey);
      crypted = cipher.doFinal(input.getBytes());
    }catch(Exception e){
    	System.out.println(e.toString());
    }
    return new String(Base64.encode(crypted, Base64.DEFAULT));
}
 
Example 11
Source File: EncryptTool.java    From FastAndroid with Apache License 2.0 4 votes vote down vote up
private static byte[] base64Encode(final byte[] input) {
    return Base64.encode(input, Base64.NO_WRAP);
}
 
Example 12
Source File: UserCenterAPIActivity.java    From styT with Apache License 2.0 4 votes vote down vote up
private String encodeData(String data) throws UnsupportedEncodingException {
	//进行BASE64编码,URL_SAFE/NO_WRAP/NO_PADDING
	return new String(Base64.encode(data.getBytes("utf-8"), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING), "utf-8");
}
 
Example 13
Source File: EncodeUtils.java    From Android-utils with Apache License 2.0 4 votes vote down vote up
public static byte[] base64Encode(final byte[] input) {
    if (input == null || input.length == 0) return new byte[0];
    return Base64.encode(input, Base64.NO_WRAP);
}
 
Example 14
Source File: StringXor.java    From text_converter with GNU General Public License v3.0 4 votes vote down vote up
@NonNull
public static String encode(String s, String key) {
    byte[] xor = xor(s.getBytes(), key.getBytes());
    byte[] encoded = Base64.encode(xor, Base64.DEFAULT);
    return new String(encoded);
}
 
Example 15
Source File: BaseApi.java    From Hook with Apache License 2.0 4 votes vote down vote up
public static void send(final Context context, String url, final Object param, final Class clazz, final int TAG) {


        if (!url.startsWith("http")) {
            url = "http://sanlicun.cn/" + url;
        }
        Log.d("tag", url);


        final RequestQueue requestQueue = Volley.newRequestQueue(context);


        final String finalUrl = url;
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i("tag", "onResponse:" + response);
                        if (response.equals("success")) {
                            index=0;
                            return;
                        }

                        if(index<3){
                            PayHelperUtils.sendmsg(context,"发送结果失败");
                            index++;
                            send(context, finalUrl, param, clazz, TAG);
                        }



                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                if(index<3){
                    PayHelperUtils.sendmsg(context,"发送结果失败");
                    index++;
                    send(context, finalUrl, param, clazz, TAG);
                }


            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                //在这里设置需要post的参数
                Map<String, String> map = new HashMap<String, String>();

                try {
                    String p = new String(Base64.encode(JSONObject.toJSONString(param).getBytes(), Base64.DEFAULT), "utf-8");
                    Log.d("tag", p);

                    map.put("param", p);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }


                return map;
            }
        };

        requestQueue.add(stringRequest);

    }
 
Example 16
Source File: JWTTest.java    From JWTDecode.Android with MIT License 4 votes vote down vote up
private String encodeString(String source) {
    byte[] bytes = Base64.encode(source.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING);
    return new String(bytes, Charset.defaultCharset());
}
 
Example 17
Source File: EncodeUtils.java    From AndroidUtilCode with Apache License 2.0 2 votes vote down vote up
/**
 * Return Base64-encode bytes.
 *
 * @param input The input.
 * @return Base64-encode bytes
 */
public static byte[] base64Encode(final byte[] input) {
    if (input == null || input.length == 0) return new byte[0];
    return Base64.encode(input, Base64.NO_WRAP);
}
 
Example 18
Source File: EncryptUtils.java    From Android-UtilCode with Apache License 2.0 2 votes vote down vote up
/**
 * Base64编码
 *
 * @param input 要编码的字节数组
 * @return Base64编码后的字符串
 */
private static byte[] base64Encode(byte[] input) {
    return Base64.encode(input, Base64.NO_WRAP);
}
 
Example 19
Source File: EncodeUtils.java    From ZhiHu-TopAnswer with Apache License 2.0 2 votes vote down vote up
/**
 * Base64编码
 *
 * @param input 要编码的字节数组
 * @return Base64编码后的字符串
 */
public static byte[] base64Encode(byte[] input) {
    return Base64.encode(input, Base64.NO_WRAP);
}
 
Example 20
Source File: RxEncodeTool.java    From RxTools-master with Apache License 2.0 2 votes vote down vote up
/**
 * Base64URL安全编码
 * <p>将Base64中的URL非法字符�?,/=转为其他字符, 见RFC3548</p>
 *
 * @param input 要Base64URL安全编码的字符串
 * @return Base64URL安全编码后的字符串
 */
public static byte[] base64UrlSafeEncode(String input) {
    return Base64.encode(input.getBytes(), Base64.URL_SAFE);
}