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:tds.itemrenderer.security.Encryption.java

private synchronized String encrypt(final String stringToEncrypt) {
    try {/*from w  w w  .  j a  v  a  2 s  .c  o m*/
        byte[] plainBytes = stringToEncrypt.getBytes(UTF_8);
        byte[] encryptedBytes = encryptCipher.doFinal(plainBytes);
        byte[] encryptIv = encryptCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
        byte[] cipherText = new byte[encryptedBytes.length + encryptIv.length];
        System.arraycopy(encryptIv, 0, cipherText, 0, encryptIv.length);
        System.arraycopy(encryptedBytes, 0, cipherText, encryptIv.length, encryptedBytes.length);
        return DatatypeConverter.printBase64Binary(cipherText);
    } catch (IllegalBlockSizeException e) {
        log.error("Encyption.encrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Block Size is not valid");
    } catch (BadPaddingException e) {
        log.error("Encyption.encrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Padding is not valid");
    } catch (InvalidParameterSpecException e) {
        log.error("Encyption.encrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Parameter Sepcification is not valid");
    }
}

From source file:TDS.Shared.Web.Encryption.java

/**
 * Encrypts a string/*  w ww.  j  a  v a  2 s. c  o  m*/
 * 
 * @param stringToEncrypt
 * @return encrypted string
 * @throws TDSEncryptionException
 */
private synchronized String encrypt(String stringToEncrypt) {
    try {

        byte[] plainBytes = stringToEncrypt.getBytes(CHARSET_NAME);
        byte[] encryptedBytes = _encryptCipher.doFinal(plainBytes);
        byte[] encryptIv = _encryptCipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
        byte[] cipherText = new byte[encryptedBytes.length + encryptIv.length];
        System.arraycopy(encryptIv, 0, cipherText, 0, encryptIv.length);
        System.arraycopy(encryptedBytes, 0, cipherText, encryptIv.length, encryptedBytes.length);
        return DatatypeConverter.printBase64Binary(cipherText);
    } catch (UnsupportedEncodingException e) {
        _logger.error("Encyption.encrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Encoding is not valid");
    } catch (IllegalBlockSizeException e) {
        _logger.error("Encyption.encrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Block Size is not valid");
    } catch (BadPaddingException e) {
        _logger.error("Encyption.encrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Padding is not valid");
    } catch (InvalidParameterSpecException e) {
        _logger.error("Encyption.encrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Parameter Sepcification is not valid");
    }

}

From source file:uk.ac.gate.cloud.client.RestClient.java

/**
 * Create a client using a specified base URL (for advanced use only -
 * the default URL will work for all normal cases).
 * /*from   w w w  . j a v a 2s .co  m*/
 * @param url API base URL
 * @param apiKeyId API key identifier for authentication
 * @param apiPassword API key password
 */
public RestClient(URL url, String apiKeyId, String apiPassword) {
    baseUrl = url;
    if (apiKeyId != null) {
        try {
            // HTTP header is "Basic base64(username:password)"
            authorizationHeader = "Basic "
                    + DatatypeConverter.printBase64Binary((apiKeyId + ":" + apiPassword).getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // should never happen
            throw new RuntimeException("JVM claims not to support UTF-8 encoding...", e);
        }
    }
}

From source file:Utility.EncryptionManager.java

/**
 * this method change the message String to encrypt String using 64bits AES
 * //from   ww  w  .j a va2 s .  com
 * @param Data
 * @return
 */
public static String encrypt64bits(String Data) {
    Key key;
    Cipher c;
    byte[] encVal = null;
    try {
        key = generateKey64bits();
        c = Cipher.getInstance(Settings.ALGO);
        c.init(Cipher.ENCRYPT_MODE, key);
        encVal = c.doFinal(Data.getBytes());
        String encryptedValue = DatatypeConverter.printBase64Binary(encVal);
        return encryptedValue;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        System.out.println(e);
    }
    return null;
}