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

public static void main(String[] args) {
    String hello = "Hello World";

    byte[] encoded = Base64.encodeBase64(hello.getBytes());

    System.out.println(Arrays.toString(encoded));

    String encodedString = new String(encoded);
    System.out.println(hello + " = " + encodedString);
}

From source file:icevaluation.BingAPIAccess.java

public static void main(String[] args) {
    String searchText = "arts site:wikipedia.org";
    searchText = searchText.replaceAll(" ", "%20");
    // String accountKey="jTRIJt9d8DR2QT/Z3BJCAvY1BfoXj0zRYgSZ8deqHHo";
    String accountKey = "JfeJSA3x6CtsyVai0+KEP0A6CYEUBT8VWhZmm9CS738";

    byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes());
    String accountKeyEnc = new String(accountKeyBytes);
    URL url;/*from  www  .  j a  v a2s.co  m*/
    try {
        url = new URL("https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27Web%27&Query=%27"
                + searchText + "%27&$format=JSON");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Authorization", "Basic " + accountKeyEnc);

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        StringBuilder sb = new StringBuilder();
        String output;
        System.out.println("Output from Server .... \n");
        //write json to string sb
        int c = 0;
        if ((output = br.readLine()) != null) {
            System.out.println("Output is: " + output);
            sb.append(output);
            c++;
            //System.out.println("C:"+c);

        }

        conn.disconnect();
        //find webtotal among output      
        int find = sb.indexOf("\"WebTotal\":\"");
        int startindex = find + 12;
        System.out.println("Find: " + find);

        int lastindex = sb.indexOf("\",\"WebOffset\"");

        System.out.println(sb.substring(startindex, lastindex));

    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

From source file:algoritmorsa_md5.MD5.java

/**
 * @param args the command line arguments
 *///from w ww.  jav a2  s .c o  m
public static void main(String[] args) throws NoSuchAlgorithmException {
    // TODO code application logic here

    MessageDigest md = MessageDigest.getInstance(MessageDigestAlgorithms.MD5);
    md.update("yesica".getBytes());
    byte[] digest = md.digest();

    for (byte b : digest) {
        System.out.println(Integer.toHexString(0xFF & b));
    }
    System.out.println();
    byte[] encoded = Base64.encodeBase64(digest);
    System.out.println(new String(encoded));
}

From source file:com.woooha.FooTest.java

public static void main(String[] args) throws UnsupportedEncodingException, JSONException {
    JSONArray result = new JSONArray("[{}]");
    System.out.println(result.get(0).getClass());
    byte[] bytes = "http://192.168.1.5:3477/woooha/upload/music/x13939426.mp3".getBytes("UTF-8");
    System.out.println(new String(Base64.encodeBase64(bytes)));
    bytes = "http://192.168.1.5:3477/woooha/upload/music/x13850595.mp3".getBytes("UTF-8");
    System.out.println(new String(Base64.encodeBase64(bytes)));
    bytes = "http://192.168.1.5:3477/woooha/upload/music/x13918712.mp3".getBytes("UTF-8");
    System.out.println(new String(Base64.encodeBase64(bytes)));
}

From source file:com.manning.blogapps.chapter10.examples.AuthDelete.java

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.out.println("USAGE: authdelete <username> <password> <url>");
        System.exit(-1);//w  ww. j  a  v a 2s  . co m
    }
    String credentials = args[0] + ":" + args[1];
    String url = args[2];

    HttpClient httpClient = new HttpClient();
    DeleteMethod method = new DeleteMethod(url);
    method.setRequestHeader("Authorization",
            "Basic " + new String(Base64.encodeBase64(credentials.getBytes())));

    httpClient.executeMethod(method);
    System.out.println("Server returned status code: ");
}

From source file:com.manning.blogapps.chapter10.examples.AuthGetJava.java

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.out.println("USAGE: authget <username> <password> <url>");
        System.exit(-1);//from   w  w  w  . j  a  va  2s  .c  o  m
    }
    String credentials = args[0] + ":" + args[1];

    URL url = new URL(args[2]);
    URLConnection conn = url.openConnection();

    conn.setRequestProperty("Authorization",
            "Basic " + new String(Base64.encodeBase64(credentials.getBytes())));

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

    String s = null;
    while ((s = in.readLine()) != null) {
        System.out.println(s);
    }
}

From source file:com.manning.blogapps.chapter10.examples.AuthGet.java

public static void main(String[] args) throws Exception {
    if (args.length < 3) {
        System.out.println("USAGE: authget <username> <password> <url>");
        System.exit(-1);/*  www.  j  av a 2 s  .  com*/
    }
    String credentials = args[0] + ":" + args[1];
    String url = args[2];

    HttpClient httpClient = new HttpClient();
    GetMethod method = new GetMethod(url);
    method.setRequestHeader("Authorization",
            "Basic " + new String(Base64.encodeBase64(credentials.getBytes())));

    httpClient.executeMethod(method);
    System.out.println(method.getResponseBodyAsString());
}

From source file:com.cfs.util.AESCriptografia.java

public static void main(String[] args) {
    AESCriptografia aes = new AESCriptografia();
    String msg = "Teste4";
    String key = "8TScvUZRTmS8V6hd/cZt/A==";

    try {//from  w  w w  .j  a  va  2  s  . co m
        System.out.println("                 AES - " + aes.testeCifrar(msg, key));
        String teste = "AES/ECB/PKCS5PADDING";
        Cipher cipher = Cipher.getInstance(teste);
        cipher.init(Cipher.ENCRYPT_MODE, aes.gerarChave(key));
        byte[] original = msg.getBytes();
        byte[] cifrada = cipher.doFinal(original);
        byte[] retorno = Base64.encodeBase64(cifrada);

        System.out.println(teste + " - " + new String(retorno));
    } catch (Exception e) {
        System.out.println(Utilities.getInstance().getLineNumber() + e.getLocalizedMessage());
    }
}

From source file:com.mycompany.mavenpost.HttpTest.java

public static void main(String args[]) throws UnsupportedEncodingException, IOException {

    System.out.println("this is a test program");
    HttpPost httppost = new HttpPost("https://app.monsum.com/api/1.0/api.php");

    // Request parameters and other properties.
    String auth = DEFAULT_USER + ":" + DEFAULT_PASS;
    byte[] encodedAuth = Base64.encodeBase64(auth.getBytes());
    String authHeader = "Basic " + new String(encodedAuth);
    //String authHeader = "Basic " +"YW5kcmVhcy5zZWZpY2hhQG1hcmtldHBsYWNlLWFuYWx5dGljcy5kZTo5MGRkYjg3NjExMWRiNjNmZDQ1YzUyMjdlNTNmZGIyYlhtMUJQQm03OHhDS1FUVm1OR1oxMHY5TVVyZkhWV3Vh";

    httppost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);

    httppost.setHeader(HttpHeaders.CONTENT_TYPE, "Content-Type: application/json");

    Map<String, Object> params = new LinkedHashMap<>();
    params.put("SERVICE", "customer.get");

    JSONObject json = new JSONObject();
    json.put("SERVICE", "customer.get");

    //Map<String, Object> params2 = new LinkedHashMap<>();

    //params2.put("CUSTOMER_NUMBER","5");
    JSONObject array = new JSONObject();
    array.put("CUSTOMER_NUMBER", "2");

    json.put("FILTER", array);

    StringEntity param = new StringEntity(json.toString());
    httppost.setEntity(param);/*from   ww w .  ja va 2  s.co m*/

    HttpClient client = HttpClientBuilder.create().build();
    HttpResponse response = client.execute(httppost);

    int statusCode = response.getStatusLine().getStatusCode();
    System.out.println("The status code is  " + statusCode);

    //Execute and get the response.
    HttpEntity entity = response.getEntity();

    Header[] headers = response.getAllHeaders();
    for (Header header : headers) {
        System.out.println("Key : " + header.getName() + " ,Value : " + header.getValue());
    }
    if (entity != null) {
        String retSrc = EntityUtils.toString(entity); //Discouraged better open a stream and read the data as per Apache manual                     
        // parsing JSON
        //JSONObject result = new JSONObject(retSrc);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonParser jp = new JsonParser();
        JsonElement je = jp.parse(retSrc);
        String prettyJsonString = gson.toJson(je);
        System.out.println(prettyJsonString);
    }
    //if (entity != null) {
    //    InputStream instream = entity.getContent();
    //    try {
    //  final BufferedReader reader = new BufferedReader(
    //                    new InputStreamReader(instream));
    //            String line = null;
    //            while ((line = reader.readLine()) != null) {
    //                System.out.println(line);
    //            }
    //            reader.close();
    //    } finally {
    //        instream.close();
    //    }
    //}
}

From source file:com.manning.blogapps.chapter10.examples.AuthPut.java

public static void main(String[] args) throws Exception {
    if (args.length < 4) {
        System.out.println("USAGE: authput <username> <password> <filepath> <url>");
        System.exit(-1);/*from  w  w  w.j a  v a2s .c  om*/
    }
    String credentials = args[0] + ":" + args[1];
    String filepath = args[2];
    String url = args[3];

    HttpClient httpClient = new HttpClient();
    EntityEnclosingMethod method = new PutMethod(url);
    method.setRequestHeader("Authorization",
            "Basic " + new String(Base64.encodeBase64(credentials.getBytes())));

    File upload = new File(filepath);
    method.setRequestHeader("name", upload.getName());

    String contentType = "application/atom+xml; charset=utf8";
    if (filepath.endsWith(".gif"))
        contentType = "image/gif";
    else if (filepath.endsWith(".jpg"))
        contentType = "image/jpg";
    method.setRequestHeader("Content-type", contentType);

    method.setRequestBody(new FileInputStream(filepath));
    httpClient.executeMethod(method);

    System.out.println(method.getResponseBodyAsString());
}