Example usage for android.util Base64 decode

List of usage examples for android.util Base64 decode

Introduction

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

Prototype

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

Source Link

Document

Decode the Base64-encoded data in input and return the data in a new byte array.

Usage

From source file:org.bcsphere.bluetooth.tools.Tools.java

public static byte[] decodeBase64(String value) {
    byte[] result = Base64.decode(value, Base64.DEFAULT);
    return result;
}

From source file:de.tum.frm2.nicos_android.nicos.NicosClient.java

private PublicKey extractPublicKey(String source) {
    // Java wants the key formatted without prefix and postfix.
    String prefix = "-----BEGIN RSA PUBLIC KEY-----";
    String postfix = "\n-----END RSA PUBLIC KEY-----";

    // Java's string formatting/slicing is... 'slightly' inferior to python's.
    String keyNoPrefix = source.substring(prefix.length());
    String reversed = new StringBuilder(keyNoPrefix).reverse().toString();
    String reversedNoPostfix = reversed.substring(postfix.length());
    String keyString = new StringBuilder(reversedNoPostfix).reverse().toString();
    keyString = keyString.replace("\n", "");

    ASN1InputStream in = new ASN1InputStream(Base64.decode(keyString, Base64.NO_WRAP));
    ASN1Primitive obj;//from   w  w w  .  j a v  a2 s. c  o  m
    try {
        obj = in.readObject();
    } catch (IOException e) {
        return null;
    }

    RSAPublicKey key = RSAPublicKey.getInstance(obj);
    RSAPublicKeySpec keySpec = null;
    if (key != null) {
        keySpec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent());
    }

    KeyFactory keyFactory = null;
    try {
        keyFactory = KeyFactory.getInstance("RSA");
    } catch (NoSuchAlgorithmException e) {
        // Cannot happen.
    }

    PublicKey pubkey = null;
    try {
        if (keyFactory != null) {
            pubkey = keyFactory.generatePublic(keySpec);
        }
    } catch (InvalidKeySpecException e) {
        // Cannot (SHOULD NOT) happen.
    }
    return pubkey;
}

From source file:com.app.khclub.base.easeim.activity.ContactlistFragment.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    case SCANNIN_GREQUEST_CODE:
        Bundle bundle = data.getExtras();
        String resultString = bundle.getString("result");

        // ?/* w  w  w .  j a  va 2s  . co m*/
        if (resultString.contains(KHConst.KH_GROUP)) {
            String baseUid = resultString.replace(KHConst.KH_GROUP, "");
            // ?
            EMGroupInfo group = new EMGroupInfo(baseUid, "");
            Intent intent = new Intent(getActivity(), GroupSimpleDetailActivity.class).putExtra("groupinfo",
                    group);
            startActivity(intent);
            return;
        }

        // ?
        if (resultString.contains(KHConst.KH)) {
            String baseUid = resultString.substring(2);
            int uid = KHUtils.stringToInt(new String(Base64.decode(baseUid, Base64.DEFAULT)));
            Intent intent = new Intent(getActivity(), OtherPersonalActivity.class);
            intent.putExtra(OtherPersonalActivity.INTENT_KEY, uid);
            startActivity(intent);
            return;
        }

        ToastUtil.show(getActivity(), "'" + resultString + "'");
        break;
    }
}

From source file:mobisocial.bento.ebento.io.EventManager.java

private Bitmap parseEventImage(Obj object, String eventUuid) {
    Bitmap bitmap = null;//  w  w  w.ja  v  a2s .  c om
    if (object != null && object.getJson() != null && object.getJson().has(EVENT_IMAGE)) {
        JSONObject imageObj = object.getJson().optJSONObject(EVENT_IMAGE);
        if (eventUuid.equals(imageObj.optString(EVENT_IMAGE_UUID))) {
            byte[] byteArray = Base64.decode(object.getJson().optString(B64JPGTHUMB), Base64.DEFAULT);
            bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
        }
    }
    return bitmap;
}

From source file:com.ruesga.rview.fragments.EditorFragment.java

private void readFileContent(final OnSavedContentReady cb) {
    final String file = mFile;
    mBinding.editor.readContent(new EditorView.OnReadContentReadyListener() {
        @Override/*from   ww  w. j a va2s.c o m*/
        public void onReadContentReady(byte[] content) {
            if (content.length > 0) {
                String name = getEditCachedFileName(file);
                if (DEBUG) {
                    Log.i(TAG, new String(Base64.decode(content, Base64.NO_WRAP)));
                }
                try {
                    CacheHelper.writeAccountDiffCacheFile(getActivity(), name, content);
                    mContentFile = new File(CacheHelper.getAccountDiffCacheDir(getActivity(), mAccount), name)
                            .getAbsolutePath();
                } catch (IOException ex) {
                    Log.w(TAG, "Failed to store edit for " + file);
                }
            }

            if (cb != null) {
                cb.onContentSaved();
            }
        }

        @Override
        public void onContentUnchanged() {
            if (cb != null) {
                cb.onContentSaved();
            }
        }
    });
}

From source file:de.ub0r.android.websms.WebSMS.java

/**
 * {@inheritDoc}// w w  w.  java  2s .  c  o  m
 */
@SuppressWarnings({ "unchecked", "deprecation" })
@Override
public final void onCreate(final Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    this.setTheme(PreferencesActivity.getTheme(this));
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate(" + savedInstanceState + ")");
    this.threadHandler = new Handler();

    // Restore preferences
    de.ub0r.android.lib.Utils.setLocale(this);

    this.cbmgr = (ClipboardManager) this.getSystemService(CLIPBOARD_SERVICE);

    // save ref to me.
    me = this;
    final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);

    // inflate XML
    this.setContentView(R.layout.main);
    this.getSupportActionBar().setHomeButtonEnabled(true);

    // indeterminate progress bar is spinning by default so stop it,
    // updateProgressBar will start it again if necessary
    this.setSupportProgressBarIndeterminateVisibility(false);

    this.etTo = (MultiAutoCompleteTextView) this.findViewById(R.id.to);
    this.etText = (EditText) this.findViewById(R.id.text);
    this.etTextLabel = (TextView) this.findViewById(R.id.text_);
    this.tvPaste = (TextView) this.findViewById(R.id.text_paste);
    this.tvClear = (TextView) this.findViewById(R.id.text_clear);

    this.vCustomSender = (ToggleButton) this.findViewById(R.id.custom_sender);
    this.vFlashSMS = (ToggleButton) this.findViewById(R.id.flashsms);
    this.vSendLater = (ToggleButton) this.findViewById(R.id.send_later);

    if (isNewVersion()) {
        Log.i(TAG, "detected version update");
        SharedPreferences.Editor editor = p.edit();
        editor.remove(PREFS_CONNECTORS); // remove cache
        editor.apply();
        rules.upgrade();
    }

    // get cached Connectors
    String s = p.getString(PREFS_CONNECTORS, null);
    if (TextUtils.isEmpty(s)) {
        this.updateConnectors();

    } else if (CONNECTORS.size() == 0) {
        // skip static remaining connectors
        try {
            ArrayList<ConnectorSpec> cache;
            cache = (ArrayList<ConnectorSpec>) (new ObjectInputStream(new BufferedInputStream(
                    new ByteArrayInputStream(Base64.decode(s, Base64.DEFAULT)), BUFSIZE))).readObject();
            CONNECTORS.addAll(cache);
            if (p.getBoolean(PREFS_AUTOUPDATE, true)) {
                updateFreecount();
            }
        } catch (Exception e) {
            Log.d(TAG, "error loading connectors", e);
        }
    }
    Log.d(TAG, "loaded connectors: " + CONNECTORS.size());

    if (PSEUDO_CONNECTORS.size() == 0) {
        PSEUDO_CONNECTORS.add(rules.getSpec(this));
    }

    if (savedInstanceState == null) {
        this.revertPrefsToStdConnector();
        // note: do not revert to std connector on orientation change
    }

    this.reloadPrefs();

    if (savedInstanceState != null) {
        this.lastTo = savedInstanceState.getString(EXTRA_TO);
        this.lastMsg = savedInstanceState.getString(EXTRA_TEXT);
    }

    // register Listener
    this.vCustomSender.setOnClickListener(this);
    this.vSendLater.setOnClickListener(this);
    this.findViewById(R.id.select).setOnClickListener(this);
    View v = this.findViewById(R.id.clear);
    v.setOnClickListener(this);
    v.setOnLongClickListener(this);
    this.findViewById(R.id.emo).setOnClickListener(this);
    this.tvPaste.setOnClickListener(this);
    this.tvClear.setOnClickListener(this);
    this.etText.addTextChangedListener(this.twCount);
    this.etText.addTextChangedListener(this.twButtons);
    this.etTo.addTextChangedListener(this.twButtons);
    this.etTo.setAdapter(new MobilePhoneAdapter(this));
    this.etTo.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    this.etTo.requestFocus();

    this.parseIntent(this.getIntent());

    boolean checkPrefix = true;
    boolean showIntro = false;
    if (TextUtils.isEmpty(p.getString(PREFS_SENDER, null))
            && TextUtils.isEmpty(p.getString(PREFS_DEFPREFIX, null)) && CONNECTORS.size() == 0) {
        checkPrefix = false;
        showIntro = true;
    }

    requestPermission(Manifest.permission.READ_CONTACTS, PERMISSIONS_REQUEST_READ_CONTACTS,
            R.string.permissions_read_contacts, null);
    if (TextUtils.isEmpty(p.getString(PREFS_SENDER, null))
            || TextUtils.isEmpty(p.getString(PREFS_DEFPREFIX, null))) {
        fetchSenderAndPrefixFromPhoneNumber();
    }

    // check default prefix
    if (checkPrefix && !p.getString(PREFS_DEFPREFIX, "").startsWith("+")) {
        this.log(R.string.log_wrong_defprefix);
    }

    if (showIntro) {
        // skip help intro for at least 2min
        if (System.currentTimeMillis() > p.getLong(PREFS_LASTHELP, 0L)
                + de.ub0r.android.lib.Utils.MINUTES_IN_MILLIS * 2) {
            p.edit().putLong(PREFS_LASTHELP, System.currentTimeMillis()).apply();
            this.startActivity(new Intent(this, HelpIntroActivity.class));
        }
    }

}

From source file:org.strongswan.android.ui.VpnProfileImportActivity.java

private byte[] decodeBase64(String encoded) {
    if (encoded == null || encoded.isEmpty()) {
        return null;
    }//from  w  w  w .  j a  va2  s.  c o m
    byte[] data = null;
    try {
        data = Base64.decode(encoded, Base64.DEFAULT);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
    return data;
}

From source file:uk.bowdlerize.API.java

@Deprecated
private String SignHeaders(String dataToSign, boolean isUser) throws NoSuchAlgorithmException,
        InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException,
        BadPaddingException, UnsupportedEncodingException, NoSuchProviderException, SignatureException {
    PKCS8EncodedKeySpec spec;//  w  w  w  . ja  va  2s  .c o  m
    if (isUser) {
        spec = new PKCS8EncodedKeySpec(
                Base64.decode(settings.getString(SETTINGS_USER_PRIVATE_KEY, "").getBytes(), 0));
    } else {
        spec = new PKCS8EncodedKeySpec(
                Base64.decode(settings.getString(SETTINGS_PROBE_PRIVATE_KEY, "").getBytes(), 0));
    }

    KeyFactory kf = KeyFactory.getInstance("RSA", "BC");
    PrivateKey pk = kf.generatePrivate(spec);
    byte[] signed = null;

    //Log.e("algorithm", pk.getAlgorithm());

    Signature instance = Signature.getInstance("SHA1withRSA");
    instance.initSign(pk);
    instance.update(dataToSign.getBytes());
    signed = instance.sign();

    Log.e("privateKey", settings.getString(SETTINGS_USER_PRIVATE_KEY, ""));
    Log.e("privateKey", settings.getString(SETTINGS_PROBE_PRIVATE_KEY, ""));
    //Log.e("Signature",Base64.encodeToString(signed, Base64.NO_WRAP));

    return Base64.encodeToString(signed, Base64.NO_WRAP);
}

From source file:com.supremainc.biostar2.user.MyProfileFragment.java

private void setView() {
    if (mUserInfo == null) {
        return;/* w  w  w. j a  v a 2s.  c  o  m*/
    }
    mLayout.setUserID(mUserInfo.user_id);
    mLayout.setUserName(mUserInfo.name);

    if (mUserInfo.pin_exist) {
        mLayout.showPin(true);
    } else {
        mLayout.showPin(false);
    }
    mLayout.setUserID(mUserInfo.user_id);
    mLayout.setUserName(mUserInfo.name);
    mLayout.setEmail(mUserInfo.email);
    mLayout.setTelephone(mUserInfo.phone_number);
    mLayout.setLoginID(mUserInfo.login_id);
    setPermission();

    if (mUserInfo.password_exist || (mPasswordData != null && !mPasswordData.isEmpty())) {
        mLayout.setPassword(getString(R.string.password_display));
    } else {
        mLayout.setPassword("");
    }

    if (mUserInfo.user_group != null) {
        mLayout.setUserGroup(mUserInfo.user_group.name);
        mLayout.setUserGroupID(mUserInfo.user_group.id);
    } else {
        //   mLayout.setUserGroup(getString(R.string.all_users));
        mLayout.setUserGroupID(String.valueOf(1));
    }

    if (mUserInfo.isActive()) {
        mLayout.setStatus(getString(R.string.status) + " " + getString(R.string.active));
    } else {
        mLayout.setStatus(getString(R.string.status) + " " + getString(R.string.inactive));
    }
    mLayout.setStatusSwitch(mUserInfo.isActive());
    mLayout.setDateStart(mUserInfo.getTimeFormmat(mTimeConvertProvider, User.UserTimeType.start_datetime,
            TimeConvertProvider.DATE_TYPE.FORMAT_DATE));
    mLayout.setDateEnd(mUserInfo.getTimeFormmat(mTimeConvertProvider, User.UserTimeType.expiry_datetime,
            TimeConvertProvider.DATE_TYPE.FORMAT_DATE));
    setAccessGroupCount();
    setFingerCount();
    setCardCount();

    if (mUserInfo.pin_exist || (mPinData != null && !mPinData.isEmpty())) {
        mLayout.setPin(getString(R.string.password_display));
    } else {
        mLayout.setPin("");
    }
    mPinSwitch.init(getActivity(), new OnChangeListener() {
        @Override
        public void onChange(boolean on) {
            if (BuildConfig.DEBUG) {
                Log.e(TAG, "pin :" + on);
            }
            if (on) {
                showPinPasswodPopup();
            } else {
                mLayout.setPin("");
                mPinData = null;
            }
        }
    }, (mUserInfo.pin_exist || (mPinData != null && !mPinData.isEmpty())));
    mPinSwitch.setSwitch((mUserInfo.pin_exist || (mPinData != null && !mPinData.isEmpty())));
    mLayout.setUserPhotoDefault();
    if (mRbmp != null) {
        mLayout.setUserPhoto(mRbmp);
    } else if (bmp != null) {
        mRbmp = ImageUtil.getRoundedBitmap(bmp, false);
        mLayout.setUserPhoto(mRbmp);
    } else {
        if (mUserInfo.photo != null && !mUserInfo.photo.isEmpty()) {
            byte[] photoByte = Base64.decode(mUserInfo.photo, 0);
            bmp = ImageUtil.byteArrayToBitmap(photoByte);
            if (bmp != null) {
                mBlurBmp = ImageUtil.fastBlur(bmp, 32);
                mLayout.setBlurBackGroud(mBlurBmp);
                mRbmp = ImageUtil.getRoundedBitmap(bmp, false);
                mLayout.setUserPhoto(mRbmp);
            }
        }
    }
    isExistImageCheck();
}