Example usage for android.util Base64 DEFAULT

List of usage examples for android.util Base64 DEFAULT

Introduction

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

Prototype

int DEFAULT

To view the source code for android.util Base64 DEFAULT.

Click Source Link

Document

Default values for encoder/decoder flags.

Usage

From source file:Main.java

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

    if (certificateString == null) {
        throw new IllegalArgumentException("certificateString cannot be null");
    }/* www  .j a  va2  s. c om*/

    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

@Nullable
public static String decompress(String input) {
    StringBuilder sb = new StringBuilder();
    ByteArrayInputStream is = null;
    GZIPInputStream gis = null;/*  ww w  .j  av a  2s .  c  om*/
    try {
        final byte[] bytes = Base64.decode(input, Base64.DEFAULT);
        is = new ByteArrayInputStream(bytes);
        gis = new GZIPInputStream(is, BUFFER_SIZE);

        int cache;
        final byte[] data = new byte[BUFFER_SIZE];
        while ((cache = gis.read(data)) != -1) {
            sb.append(new String(data, 0, cache));
        }
    } catch (IOException e) {
        return null;
    } finally {
        try {
            if (gis != null)
                gis.close();
            if (is != null)
                is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

From source file:Main.java

public static RSAPublicKeySpec getRecipientsPublicKey(String contactNum, Context context) {
    Log.w(TAG, "retrieving public key for contact " + contactNum);
    SharedPreferences prefs = context.getSharedPreferences(contactNum, Context.MODE_PRIVATE);

    String pubMod = prefs.getString(PREF_PUBLIC_MOD, DEFAULT_PREF);
    String pubExp = prefs.getString(PREF_PUBLIC_EXP, DEFAULT_PREF);
    Log.w(TAG, "the public modulus is " + pubMod + " and exponent is " + pubExp + " for " + contactNum);
    // String recipient = prefs.getString(PREF_RECIPIENT_NUM, DEFAULT_PREF);
    if (!pubMod.isEmpty() && !pubExp.isEmpty()) {
        Log.i(TAG, "great! public key found for " + contactNum + " with modulus " + pubMod + " and exponent "
                + pubExp);/*ww w . j av a2  s. co  m*/
        byte[] pubModBA = Base64.decode(pubMod, Base64.DEFAULT);
        byte[] pubExpBA = Base64.decode(pubExp, Base64.DEFAULT);
        BigInteger pubModBI = new BigInteger(1, pubModBA); // 1 is added as
        // an attempt to
        // deal with
        // com.android.org.bouncycastle.crypto.DataLengthException:
        // input too
        // large for RSA
        // cipher
        BigInteger pubExpBI = new BigInteger(1, pubExpBA);
        Log.i(TAG, "public modulus is " + pubModBI + " and public exponent is " + pubExpBI + " in base 256 "
                + pubModBA + " " + pubExpBA);

        // do I need to catch any exception for the following?
        RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(pubModBI, pubExpBI);
        // X509EncodedKeySpec publicKeySpec = new
        // X509EncodedKeySpec(encodedKey);

        return pubKeySpec;
    }
    Log.w(TAG, "recipient's public key not found");
    return null;

}

From source file:Main.java

/**
 * Encode an image form the given path into a {@link Base64}  string
 *
 * @param imagePath Path of the image to encode (must be absolute)
 * @return a {@link Base64} encoded string.
 *//* w  w w .j a v a  2s.  c o m*/
public static String encodeBitmap(String imagePath) {
    Bitmap image = BitmapFactory.decodeFile(imagePath);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    byte[] b = bos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    return imageEncoded;
}

From source file:Main.java

public static void printKeyHash(Activity pActivity) {
    // Add code to print out the key hash
    try {//from  w ww .j  av  a 2 s.  c o m
        PackageInfo info = pActivity.getPackageManager().getPackageInfo(pActivity.getPackageName(),
                PackageManager.GET_SIGNATURES);
        for (Signature signature : info.signatures) {
            MessageDigest md = MessageDigest.getInstance("SHA");
            md.update(signature.toByteArray());
            Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.d("KeyHash:", e.toString());
    } catch (NoSuchAlgorithmException e) {
        Log.d("KeyHash:", e.toString());
    }
}

From source file:Main.java

/**
 * Converts a PEM formatted cert in a given file to the binary DER format.
 *
 * @param pemPathname the location of the certificate to convert.
 * @return array of bytes that represent the certificate in DER format.
 * @throws IOException if the file cannot be read.
 */// w  w w  . j  a v a2  s .  c  om
public static byte[] pemToDer(String pemPathname) throws IOException {
    BufferedReader reader = new BufferedReader(new FileReader(pemPathname));
    StringBuilder builder = new StringBuilder();

    // Skip past leading junk lines, if any.
    String line = reader.readLine();
    while (line != null && !line.contains(BEGIN_MARKER))
        line = reader.readLine();

    // Then skip the BEGIN_MARKER itself, if present.
    while (line != null && line.contains(BEGIN_MARKER))
        line = reader.readLine();

    // Now gather the data lines into the builder.
    while (line != null && !line.contains(END_MARKER)) {
        builder.append(line.trim());
        line = reader.readLine();
    }

    reader.close();
    return Base64.decode(builder.toString(), Base64.DEFAULT);
}

From source file:Main.java

/**
 * /*from  w ww.j a  v a 2 s  .  c o  m*/
 * Get Private Key or return null.
 * 
 * @param account
 *            the account we're syncing
 * @return Private Key
 */
public static SecretKey getPrivateKey(Account account, AccountManager accountManager) {
    // TODO: Redesign more secure location to safe private key?
    String keyString = accountManager.getUserData(account, PRIVATE_KEY);
    SecretKey key = null;
    if (!TextUtils.isEmpty(keyString)) {
        key = new SecretKeySpec(Base64.decode(keyString, Base64.DEFAULT), "AES");
    }
    return key;
}

From source file:bencoding.securely.Converters.java

public static String serializeObjectToString(Object object) throws Exception {

    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(arrayOutputStream);
    ObjectOutputStream objectOutputStream = new ObjectOutputStream(gzipOutputStream);

    objectOutputStream.writeObject(object);

    objectOutputStream.flush();//from  w  w w .  j av a2s  .co  m
    objectOutputStream.close();
    gzipOutputStream.close();
    arrayOutputStream.close();

    String objectString = new String(Base64.encodeToString(arrayOutputStream.toByteArray(), Base64.DEFAULT));

    return objectString;
}

From source file:Main.java

/**
 * File to Base64/*from w  w w . j  a va 2 s. c o m*/
 * @param filePath
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 */
public static String getBase64StringFromFile(String filePath) throws FileNotFoundException, IOException {
    byte[] buffer = null;
    File file = new File(filePath);
    FileInputStream fis = null;
    ByteArrayOutputStream bos = null;
    byte[] b = new byte[1000];
    try {
        fis = new FileInputStream(file);
        bos = new ByteArrayOutputStream(1000);
        int n;
        while ((n = fis.read(b)) != -1) {
            bos.write(b, 0, n);
        }
        fis.close();
        bos.close();
        buffer = bos.toByteArray();
    } catch (FileNotFoundException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    return Base64.encodeToString(buffer, Base64.DEFAULT);
}

From source file:Main.java

public static byte[] generateRequestToken(byte[] countryCode, byte[] phoneNumber)
        throws NoSuchAlgorithmException {

    String signature = "MIIDMjCCAvCgAwIBAgIETCU2pDALBgcqhkjOOAQDBQAwfDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFDASBgNVBAcTC1NhbnRhIENsYXJhMRYwFAYDVQQKEw1XaGF0c0FwcCBJbmMuMRQwEgYDVQQLEwtFbmdpbmVlcmluZzEUMBIGA1UEAxMLQnJpYW4gQWN0b24wHhcNMTAwNjI1MjMwNzE2WhcNNDQwMjE1MjMwNzE2WjB8MQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEUMBIGA1UEBxMLU2FudGEgQ2xhcmExFjAUBgNVBAoTDVdoYXRzQXBwIEluYy4xFDASBgNVBAsTC0VuZ2luZWVyaW5nMRQwEgYDVQQDEwtCcmlhbiBBY3RvbjCCAbgwggEsBgcqhkjOOAQBMIIBHwKBgQD9f1OBHXUSKVLfSpwu7OTn9hG3UjzvRADDHj+AtlEmaUVdQCJR+1k9jVj6v8X1ujD2y5tVbNeBO4AdNG/yZmC3a5lQpaSfn+gEexAiwk+7qdf+t8Yb+DtX58aophUPBPuD9tPFHsMCNVQTWhaRMvZ1864rYdcq7/IiAxmd0UgBxwIVAJdgUI8VIwvMspK5gqLrhAvwWBz1AoGBAPfhoIXWmz3ey7yrXDa4V7l5lK+7+jrqgvlXTAs9B4JnUVlXjrrUWU/mcQcQgYC0SRZxI+hMKBYTt88JMozIpuE8FnqLVHyNKOCjrh4rs6Z1kW6jfwv6ITVi8ftiegEkO8yk8b6oUZCJqIPf4VrlnwaSi2ZegHtVJWQBTDv+z0kqA4GFAAKBgQDRGYtLgWh7zyRtQainJfCpiaUbzjJuhMgo4fVWZIvXHaSHBU1t5w//S0lDK2hiqkj8KpMWGywVov9eZxZy37V26dEqr/c2m5qZ0E+ynSu7sqUD7kGx/zeIcGT0H+KAVgkGNQCo5Uc0koLRWYHNtYoIvt5R3X6YZylbPftF/8ayWTALBgcqhkjOOAQDBQADLwAwLAIUAKYCp0d6z4QQdyN74JDfQ2WCyi8CFDUM4CaNB+ceVXdKtOrNTQcc0e+t";
    String classesMd5 = "P3b9TfNFCkkzPoVzZnm+BA=="; // 2.11.395 [*]

    byte[] key2 = Base64.decode(
            "/UIGKU1FVQa+ATM2A0za7G2KI9S/CwPYjgAbc67v7ep42eO/WeTLx1lb1cHwxpsEgF4+PmYpLd2YpGUdX/A2JQitsHzDwgcdBpUf7psX1BU=",
            Base64.DEFAULT);

    byte[] data = concat(
            concat(Base64.decode(signature, Base64.DEFAULT), Base64.decode(classesMd5, Base64.DEFAULT)),
            phoneNumber);//  w ww  .j  a  v  a  2 s  .  c om

    byte[] opad = new byte[64];
    byte[] ipad = new byte[64];
    for (int i = 0; i < opad.length; i++) {
        opad[i] = 0x5C;
        ipad[i] = 0x36;
    }

    for (int i = 0; i < 64; i++) {
        opad[i] = (byte) ((opad[i] & 0xFF) ^ (key2[i] & 0xFF));
        ipad[i] = (byte) ((ipad[i] & 0xFF) ^ (key2[i] & 0xFF));
    }

    data = concat(ipad, data);

    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(data);
    byte[] firstHash = md.digest();

    data = concat(opad, firstHash);
    md = MessageDigest.getInstance("SHA-1");
    md.update(data);
    byte[] secondHash = md.digest();

    return Base64.encode(secondHash, Base64.DEFAULT | Base64.NO_WRAP);
}