Example usage for android.util Base64 encodeToString

List of usage examples for android.util Base64 encodeToString

Introduction

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

Prototype

public static String encodeToString(byte[] input, int flags) 

Source Link

Document

Base64-encode the given data and return a newly allocated String with the result.

Usage

From source file:Main.java

/**
 *  //from  ww w. ja va  2s  .com
 * @param data
 * @param key
 * @return
 * @throws UnsupportedEncodingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 */
public static String sha256(String data, String key)
        throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
    byte[] hmacValue = null;
    Mac mac = getMac(key, KEY_HASH_SHA256);
    hmacValue = mac.doFinal(data.getBytes(KEY_ENCODING));
    return new String(Base64.encodeToString(hmacValue, 0));
}

From source file:Main.java

/**
 * Converts the given salt into a base64 encoded string suitable for
 * storage./*from   w  ww  .  j a v  a2 s .c  o  m*/
 *
 * @return a base 64 encoded salt string suitable to pass into generateKeyFromPassword.
 */
public static String saltString(byte[] salt) {
    return Base64.encodeToString(salt, BASE64_FLAGS);
}

From source file:Main.java

public static String uriToBase64(Uri uri, ContentResolver resolver, boolean thumbnail) {
    String encodedBase64 = "";
    try {/*from ww  w. jav  a  2 s.  c  om*/
        byte[] bytes = readBytes(uri, resolver, thumbnail);
        encodedBase64 = Base64.encodeToString(bytes, 0);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    return encodedBase64;
}

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

/**
 * Return a Base64 encoded String of the hash(input) (SHA 256)
 * @param input a String to encode//w w  w  .  java  2 s  .  c  o m
 * @return a Base64 encoded String of the hash(input) (SHA 256)
 */
public static String sha256(String input) {
    MessageDigest digest = null;
    try {
        digest = MessageDigest.getInstance("SHA-256");
    } catch (NoSuchAlgorithmException e) {
        LOGE(TAG, e.toString());
    }
    byte[] hash = new byte[0];
    if (digest != null) {
        try {
            hash = digest.digest(input.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            LOGE(TAG, e.toString());
        }

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

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();//w  w w  .  j  a v a  2 s.  c o  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 ww  w.  j av a 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

/**
 * Encrypt and encode message using 256-bit AES with key generated from password.
 *
 * @param password used to generated key
 * @param message  the thing you want to encrypt assumed String UTF-8
 * @return Base64 encoded CipherText/*from   w  ww.  j  a  va2 s  .c o  m*/
 * @throws GeneralSecurityException if problems occur during encryption
 */
public static String encrypt(final String password, String message) throws GeneralSecurityException {
    try {
        final SecretKeySpec key = generateKey(password);

        log("message", message);

        byte[] cipherText = encrypt(key, ivBytes, message.getBytes(CHARSET));

        //NO_WRAP is important as was getting \n at the end
        String encoded = Base64.encodeToString(cipherText, Base64.NO_WRAP);
        log("Base64.NO_WRAP", encoded);
        return encoded;
    } catch (UnsupportedEncodingException e) {
        if (DEBUG_LOG_ENABLED)
            Log.e(TAG, "UnsupportedEncodingException ", e);
        throw new GeneralSecurityException(e);
    }
}

From source file:Main.java

/**
 * Encrypt and encode message using 256-bit AES with key generated from password.
 *
 * @param password used to generated key
 * @param message  the thing you want to encrypt assumed String UTF-8
 * @return Base64 encoded CipherText/*from  w w  w.java 2 s . com*/
 * @throws GeneralSecurityException if problems occur during encryption
 */
public static String encrypt(final String password, String message) throws GeneralSecurityException {

    try {
        final SecretKeySpec key = generateKey(password);

        log("message", message);

        byte[] cipherText = encrypt(key, ivBytes, message.getBytes(CHARSET));

        //NO_WRAP is important as was getting \n at the end
        String encoded = Base64.encodeToString(cipherText, Base64.NO_WRAP);
        log("Base64.NO_WRAP", encoded);
        return encoded;
    } catch (UnsupportedEncodingException e) {
        if (DEBUG_LOG_ENABLED)
            Log.e(TAG, "UnsupportedEncodingException ", e);
        throw new GeneralSecurityException(e);
    }
}

From source file:Main.java

public static String encrypt(String key, String str) throws java.io.UnsupportedEncodingException,
        NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
    String aesKey;//from ww w  .j  av a2 s.co  m
    aesKey = key.substring(0, 16);
    Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
    c.init(Cipher.ENCRYPT_MODE, AESkey(key), new IvParameterSpec(aesKey.getBytes()));

    byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
    String enStr = new String(Base64.encodeToString(encrypted, 0));

    return enStr;
}

From source file:Main.java

public static String encryption(String string) throws Exception {
    // TODO Auto-generated method stub

    //generate symmetric key
    KeyGenerator keygt = KeyGenerator.getInstance("AES");
    keygt.init(128);//ww w  .  j  a  v  a2  s .  c  o  m
    SecretKey symkey = keygt.generateKey();

    //get it encoded
    byte[] aes_ba = symkey.getEncoded();

    //create cipher
    SecretKeySpec skeySpec = new SecretKeySpec(aes_ba, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    //encryption
    byte[] EncSymbyteArray = cipher.doFinal(string.getBytes());

    //encrypt symKey with PublicKey
    //        Key pubKey = getPublicKey();

    Key pubKey = publicKey;

    //RSA cipher
    Cipher cipherAsm = Cipher.getInstance("RSA", "BC");
    cipherAsm.init(Cipher.ENCRYPT_MODE, pubKey);

    //RSA encryption
    byte[] asymEncsymKey = cipherAsm.doFinal(aes_ba);

    //           File f3 = new File(BASE_PATH,"enc.txt");
    //           File f3key = new File(BASE_PATH,"enckey.txt");
    //           File f3file = new File(BASE_PATH,"encfile.txt");
    //           writeToFile2(f3,f3key,f3file, asymEncsymKey, EncSymbyteArray);

    //byte != new String
    //return new String(byteMerger(asymEncsymKey, EncSymbyteArray));
    return Base64.encodeToString(byteMerger(asymEncsymKey, EncSymbyteArray), Base64.DEFAULT);

}