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:Main.java

public static String decrypt(byte[] cipherText) {
    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

    Cipher cipher = null;/*from   ww  w .  j  a v  a 2  s  .  c  om*/
    byte[] clearText = null;

    try {
        // init cipher
        cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        clearText = cipher.doFinal(Base64.decode(new String(cipherText), 10));
    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchPaddingException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {// TODO Auto-generated catch block
        e.printStackTrace();
    }

    return new String(clearText);
}

From source file:Main.java

/**
 * Decodes a string into a Bitmap// w w  w  .  j a v a 2  s  .  c o m
 *
 * @param input String to decode
 * @return decoded Bitmap
 */
private static Bitmap decodeBitmap(String input) {
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

From source file:Main.java

/**
 * Base64 to File//  w ww.  j a  v a  2  s.  c  om
 * @param base64Str
 * @param filePath
 * @param fileName
 * @throws FileNotFoundException
 * @throws IOException
 */
public static void saveBase64StringToFile(String base64Str, String filePath, String fileName)
        throws FileNotFoundException, IOException {
    BufferedOutputStream bos = null;
    FileOutputStream fos = null;
    File file = null;
    try {
        File dir = new File(filePath);
        if (!dir.exists() && dir.isDirectory()) {
            dir.mkdirs();
        }
        file = new File(filePath, fileName);
        fos = new FileOutputStream(file);
        bos = new BufferedOutputStream(fos);
        byte[] bfile = Base64.decode(base64Str, Base64.DEFAULT);
        bos.write(bfile);
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static Bundle fromBase64(String serialized) {
    Bundle bundle = null;//w ww . j a  v a2s. c  om
    if (serialized != null) {
        Parcel parcel = Parcel.obtain();
        try {
            byte[] data = Base64.decode(serialized, 0);
            parcel.unmarshall(data, 0, data.length);
            parcel.setDataPosition(0);
            bundle = parcel.readBundle();
        } finally {
            parcel.recycle();
        }
    }
    return bundle;
}

From source file:Main.java

public static Bitmap decodeBase64(String input) {
    byte[] decodedByte = Base64.decode(input, 0);
    return bitmapResult = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}

From source file:Main.java

/**
 * Gets the bitmap image.//  w w w .j av a2  s .  co m
 *
 * @param context the context
 * @param base64  the base64
 * @return the bitmap image
 */
public static Bitmap getBitmapImage(Context context, String base64) {
    byte[] imageAsBytes = Base64.decode(base64.getBytes(), 5);
    return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);

}

From source file:Main.java

/**
 * Gets the bitmap image./*w w  w.  ja v  a 2 s.  co m*/
 *
 * @param context the context
 * @param base64  the base64
 * @return the bitmap image
 */
public static Bitmap getBitmapImage(Context context, String base64) {

    String imagestring = base64;
    byte[] imageAsBytes = Base64.decode(imagestring.getBytes(), 5);
    return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);

}

From source file:Main.java

static byte[] decodeB64(String s64) {
    return Base64.decode(s64, Base64.URL_SAFE);
}

From source file:Main.java

public static X509Certificate base64StringToCertificate(String certificateString)
        throws CertificateException, IOException {

    if (certificateString == null) {
        throw new IllegalArgumentException("certificateString cannot be null");
    }/*w w w  .  j  a v  a 2s .  c  o m*/

    byte[] encodedCert = Base64.decode(certificateString, Base64.DEFAULT);
    InputStream inStream = new ByteArrayInputStream(encodedCert);
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate cert = (X509Certificate) cf.generateCertificate(inStream);
    inStream.close();

    return cert;
}

From source file:Main.java

/**
 * Method deciphers previously ciphered message
 *
 * @param message ciphered message//from w ww .  j ava  2s .co  m
 * @param salt    salt which was used for ciphering
 * @return deciphered message
 */
@NonNull
public static String fromX(@NonNull String message, @NonNull String salt) throws IllegalArgumentException {
    return x(new String(Base64.decode(message, Base64.URL_SAFE)), salt);
}