Example usage for org.apache.commons.codec.binary Base64 encodeBase64

List of usage examples for org.apache.commons.codec.binary Base64 encodeBase64

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base64 encodeBase64.

Prototype

public static byte[] encodeBase64(final byte[] binaryData) 

Source Link

Document

Encodes binary data using the base64 algorithm but does not chunk the output.

Usage

From source file:com.juicioenlinea.application.secutiry.Security.java

public static String encriptar(String texto) {

    final String secretKey = "hunter"; //llave para encriptar datos
    String encryptedString = null;

    try {/*from  www  .  ja va 2  s.  c om*/

        MessageDigest md = MessageDigest.getInstance("SHA");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("UTF-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);

        SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE, key);

        byte[] plainTextBytes = texto.getBytes("UTF-8");
        byte[] buf = cipher.doFinal(plainTextBytes);
        byte[] base64Bytes = Base64.encodeBase64(buf);
        encryptedString = new String(base64Bytes);

    } catch (NoSuchAlgorithmException | UnsupportedEncodingException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        Logger.getLogger(Security.class.getName()).log(Level.SEVERE, null, ex);
    }
    return encryptedString;
}

From source file:com.ebay.erl.mobius.util.SerializableUtil.java

public final static String serializeToBase64(Serializable obj) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {/*www .  j  a va2s  .c  o  m*/
        oos = new ObjectOutputStream(bos);
        oos.writeObject(obj);
        oos.flush();
        oos.close();

        String result = new String(Base64.encodeBase64(bos.toByteArray()), "UTF-8");
        return result;
    } catch (NotSerializableException e) {
        throw new IllegalArgumentException("Cannot serialize " + obj.getClass().getCanonicalName(), e);
    } finally {
        try {
            bos.close();
        } catch (Throwable e) {
            e = null;
        }

        try {
            if (oos != null)
                oos.close();
        } catch (Throwable e) {
            e = null;
        }
    }
}

From source file:com.envision.envservice.common.util.Base64Utils.java

/**
 * Base64 encode/*  w ww.  j av  a 2s .  c o  m*/
 */
public static String encode(byte[] bytes) {
    if (bytes == null) {
        throw new IllegalArgumentException("Null.");
    }

    return new String(Base64.encodeBase64(bytes));
}

From source file:de.topobyte.livecg.core.painting.backend.ipe.IpeImageEncoder.java

public static IpeImage encode(Image image) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DeflaterOutputStream dos = new DeflaterOutputStream(baos);
    for (int j = 0; j < image.getHeight(); j++) {
        for (int i = 0; i < image.getWidth(); i++) {
            int rgb = image.getRGB(i, j);
            dos.write(rgb >> 16);/*  ww w  . ja  v a  2 s .c  o m*/
            dos.write(rgb >> 8);
            dos.write(rgb);
        }
    }
    dos.close();
    byte[] buffer = baos.toByteArray();
    int length = buffer.length;

    byte[] bbase64 = Base64.encodeBase64(buffer);
    String base64 = new String(bbase64);

    return new IpeImage(base64, length);
}

From source file:filters.SaltingPasswordFilter.java

@Override
public String executePreProcessing(String request) {
    final Random r = new SecureRandom();
    byte[] salt = new byte[32];
    r.nextBytes(salt);/* w  w  w.  j  a va2 s  .c  o m*/
    byte[] encodedSalt = Base64.encodeBase64(salt);
    String saltString = new String(encodedSalt);
    request = request.concat(saltString);
    return request;
}

From source file:com.ec2box.manage.util.EncryptionUtil.java

/**
 * generate salt for hash/* w w w. ja v a 2  s  .c o m*/
 *
 * @return salt
 */
public static String generateSalt() {
    byte[] salt = new byte[32];
    SecureRandom secureRandom = new SecureRandom();
    secureRandom.nextBytes(salt);
    return new String(Base64.encodeBase64(salt));
}

From source file:com.appdynamics.common.RESTClient.java

public static void sendGet(String urlString, String apiKey) {

    try {//from  ww  w .j  a  v  a 2 s .  c  o m

        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Authorization",
                "Basic " + new String(Base64.encodeBase64((apiKey).getBytes())));

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

        String output;
        logger.info("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            logger.info(output);
        }

        conn.disconnect();

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

From source file:helper.Digester.java

/**
 * @param str the input string//from ww  w.  j av a2s .  c o  m
 * @return the digested string
 * @throws NoSuchAlgorithmException encryption algorithm not installed 
 */
public static String digest(String str) throws NoSuchAlgorithmException {
    Configuration config = Play.application().configuration();

    String salt = config.getString("application.secret");

    String saltedStr = str + salt;

    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(saltedStr.getBytes());

    byte byteData[] = md.digest();

    byte[] base64 = Base64.encodeBase64(byteData);

    String result = new String(base64, Charset.defaultCharset());

    return result;
}

From source file:com.messagemedia.restapi.client.v1.internal.http.interceptors.HMACUtils.java

/**
 * This method encrypts data with secret using HmacSHA1. The strings are converted to bytes using the UTF-8 encoding.
 *
 * @param secret - the secret which is being used
 * @param data   - the data which is encrypted
 * @return the base64 encoded result// w w w  .ja  v a  2  s .c o m
 */
static String sha1(String secret, String data) {
    try {
        SecretKeySpec signingKey = new SecretKeySpec(secret.getBytes("UTF-8"), HMAC_SHA1);
        Mac mac = Mac.getInstance(HMAC_SHA1);
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(data.getBytes("UTF-8"));
        return new String(Base64.encodeBase64(rawHmac), "UTF-8");
    } catch (GeneralSecurityException e) {
        throw new RuntimeException("Failed to encrypt data", e);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Can not find the UTF-8 charset", e);
    }
}

From source file:backtype.storm.messaging.netty.SaslUtils.java

/**
 * Encode a password as a base64-encoded char[] array.
 * //from www  .j  a  v a2  s  .  c  o  m
 * @param password
 *            as a byte array.
 * @return password as a char array.
 */
static char[] encodePassword(byte[] password) {
    return new String(Base64.encodeBase64(password), Charsets.UTF_8).toCharArray();
}