Example usage for javax.xml.bind DatatypeConverter printBase64Binary

List of usage examples for javax.xml.bind DatatypeConverter printBase64Binary

Introduction

In this page you can find the example usage for javax.xml.bind DatatypeConverter printBase64Binary.

Prototype

public static String printBase64Binary(byte[] val) 

Source Link

Document

Converts an array of bytes into a string.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    int size = 200;
    BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = image.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setColor(Color.BLUE);//from w  w w  .jav  a2s  .  c  om
    for (int i = 0; i < size; i += 5) {
        g.drawOval(i, i, size - i, size - i);
    }
    g.dispose();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "png", baos);

    String data = DatatypeConverter.printBase64Binary(baos.toByteArray());
    String imageString = "data:image/png;base64," + data;
    String html = "<html><body><img src='" + imageString + "'></body></html>";
    System.out.println(html);
}

From source file:com.openshift.restclient.utils.Base64Coder.java

/**
 * Encodes the given byte array to a base64 encoded String
 * //from w w w.j a  v a2  s .  c  o m
 * @param unencoded
 *            the array of unencoded bytes that shall get encoded
 * @return the encoded string
 */
public static String encode(byte[] unencoded) {
    if (unencoded == null) {
        return null;
    } else if (unencoded.length == 0) {
        return new String();
    }
    return DatatypeConverter.printBase64Binary(unencoded);
}

From source file:com._8x8.data.repository.EncryptorRepository.java

@Override
public String encrypt(String dataToBeEncrypted) throws Exception {
    String encoded = DatatypeConverter.printBase64Binary(dataToBeEncrypted.getBytes());
    return encoded;
}

From source file:com._8x8.logic.service.EncryptorService.java

@Override
public String encryptCode(String plainText) throws Exception {
    //String cipherText = _encryptorRepository.encrypt(plainText);
    // ** add more secure encrypt logic
    //return cipherText;

    String encoded = DatatypeConverter.printBase64Binary(plainText.getBytes());
    return encoded;

}

From source file:com.vexsoftware.votifier.crypto.RSAIO.java

/**
 * Saves the key pair to the disk.//w w w  .  j  a va 2s  .co m
 * 
 * @param directory
 *            The directory to save to
 * @param keyPair
 *            The key pair to save
 * @throws Exception
 *             If an error occurs
 */
public static void save(File directory, KeyPair keyPair) throws Exception {
    PrivateKey privateKey = keyPair.getPrivate();
    PublicKey publicKey = keyPair.getPublic();

    // Store the public key.
    X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicKey.getEncoded());
    FileOutputStream out = new FileOutputStream(directory + "/public.key");
    out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded()).getBytes());
    out.close();

    // Store the private key.
    PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
    out = new FileOutputStream(directory + "/private.key");
    out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded()).getBytes());
    out.close();
}

From source file:com.redhat.lightblue.crud.ldap.ITCaseLdapCRUDController_DataType_Test.java

@Parameters(name = "{index}: {0}")
public static Collection<Object[]> data() {
    return Arrays.asList(new Object[][] { { "date", "testdate", DateType.getDateFormat().format(new Date()) },
            { "binary", "testbinary", DatatypeConverter.printBase64Binary("test binary data".getBytes()) } });
}

From source file:com.vexsoftware.votifier.util.rsa.RSAIO.java

/**
 * Saves the key pair to the disk.//from   w  ww.  ja  v  a 2  s  .  c o  m
 * 
 * @param directory
 *            The directory to save to
 * @param keyPair
 *            The key pair to save
 * @throws Exception
 *            If an error occurs
 */
public static void save(File directory, KeyPair keyPair) throws Exception {
    PrivateKey privateKey = keyPair.getPrivate();
    PublicKey publicKey = keyPair.getPublic();

    // Store the public key.
    X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicKey.getEncoded());
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(directory + "/public.key");
        out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded()).getBytes());
    } finally {
        try {
            out.close();
        } catch (Exception exception) {
            // ignore
        }
    }

    // Store the private key.
    PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
    try {
        out = new FileOutputStream(directory + "/private.key");
        out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded()).getBytes());
    } finally {
        try {
            out.close();
        } catch (Exception exception) {
            // ignore
        }
    }
}

From source file:it.geosolutions.geofence.core.model.Base64EncodersTest.java

@Test
public void testEq() {

    String msg1 = "this is the message to encode";

    String output_codec = new String(Base64.encodeBase64(msg1.getBytes()));
    String output_dconv = DatatypeConverter.printBase64Binary(msg1.getBytes());

    System.out.println("apache commons:    " + output_codec);
    System.out.println("DatatypeConverter: " + output_dconv);
    assertEquals(output_codec, output_dconv);

    byte[] back_codec = Base64.decodeBase64(output_dconv);
    byte[] back_dconv = DatatypeConverter.parseBase64Binary(output_dconv);

    Assert.assertTrue(Arrays.equals(msg1.getBytes(), back_codec));
    Assert.assertTrue(Arrays.equals(msg1.getBytes(), back_dconv));

}

From source file:org.kie.remote.tests.base.RestUtil.java

private static String basicAuthenticationHeader(String user, String password) {
    String token = user + ":" + password;
    try {// w  w w  .  j  a v a2 s .  c  o  m
        return "BASIC " + DatatypeConverter.printBase64Binary(token.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException ex) {
        throw new IllegalStateException("Cannot encode with UTF-8", ex);
    }
}

From source file:com.orthancserver.OrthancConnection.java

private static String ComputeAuthentication(String username, String password) {
    String auth = (new StringBuffer(username).append(":").append(password)).toString();
    // http://stackoverflow.com/a/14413290/881731
    return DatatypeConverter.printBase64Binary(auth.getBytes());
}