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

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

Introduction

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

Prototype

public Base64() 

Source Link

Document

Creates a Base64 codec used for decoding (all modes) and encoding in URL-unsafe mode.

Usage

From source file:aajavafx.Kripto.java

public String encrypt(String plainText) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
    byte[] byteCipher = cipher.doFinal(plainText.getBytes("UTF-8"));
    Base64 base64 = new Base64();
    String stringToStore = new String(base64.encode(byteCipher));
    //byte[] restoredBytes = Base64.decode(stringToStore.getBytes());
    return stringToStore;
}

From source file:de.hybris.platform.acceleratorservices.payment.utils.impl.DefaultAcceleratorDigestUtils.java

@Override
public String getPublicDigest(final String customValues, final String key)
        throws NoSuchAlgorithmException, InvalidKeyException {
    final Base64 encoder = new Base64();
    final Mac sha1Mac = Mac.getInstance(getMacAlgorithm());
    final SecretKeySpec publicKeySpec = new SecretKeySpec(key.getBytes(), getMacAlgorithm());
    sha1Mac.init(publicKeySpec);//w  ww. j  av a 2s .  c  o  m

    final byte[] publicBytes = sha1Mac.doFinal(customValues.getBytes());
    final String publicDigest = new String(encoder.encode(publicBytes));

    return publicDigest.replaceAll("\n", "");
}

From source file:com.wizecommerce.hecuba.CassandraMapResultSet.java

@Override
public byte[] getByteArray(N fieldName) {
    Base64 base64 = new Base64();
    return base64.decode(getString(fieldName));
}

From source file:net.paygate.saml.util.RequestUtil.java

/**
 * Generates an encoded and compressed String from the specified XML-formatted
 * String. The String is encoded in the following order:
 * <p>//from  ww w  . j ava  2 s  . c  o  m
 * 1. URL encode <br>
 * 2. Base64 encode <br>
 * 3. Deflate <br>
 * 
 * @param xmlString XML-formatted String that is to be encoded
 * @return String containing the encoded contents of the specified XML String
 */
public static String encodeMessage(String xmlString) throws IOException, UnsupportedEncodingException {
    // first DEFLATE compress the document (saml-bindings-2.0,
    // section 3.4.4.1)
    byte[] xmlBytes = xmlString.getBytes("UTF-8");
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteOutputStream);
    deflaterOutputStream.write(xmlBytes, 0, xmlBytes.length);
    deflaterOutputStream.close();

    // next, base64 encode it
    Base64 base64Encoder = new Base64();
    byte[] base64EncodedByteArray = base64Encoder.encode(byteOutputStream.toByteArray());
    String base64EncodedMessage = new String(base64EncodedByteArray);

    // finally, URL encode it
    String urlEncodedMessage = URLEncoder.encode(base64EncodedMessage);

    return urlEncodedMessage;
}

From source file:com.integrareti.integraframework.util.RequestUtil.java

/**
 * Generates an encoded and compressed String from the specified
 * XML-formatted String. The String is encoded in the following order:
 * <p>/*from   ww w.  ja va  2s.  c om*/
 * 1. URL encode <br>
 * 2. Base64 encode <br>
 * 3. Deflate <br>
 * 
 * @param xmlString
 *            XML-formatted String that is to be encoded
 * @return String containing the encoded contents of the specified XML
 *         String
 */
public static String encodeMessage(String xmlString) throws IOException, UnsupportedEncodingException {
    // first DEFLATE compress the document (saml-bindings-2.0,
    // section 3.4.4.1)
    byte[] xmlBytes = xmlString.getBytes("UTF-8");
    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteOutputStream);
    deflaterOutputStream.write(xmlBytes, 0, xmlBytes.length);
    deflaterOutputStream.close();
    // next, base64 encode it
    Base64 base64Encoder = new Base64();
    byte[] base64EncodedByteArray = base64Encoder.encode(byteOutputStream.toByteArray());
    String base64EncodedMessage = new String(base64EncodedByteArray);
    // finally, URL encode it
    String urlEncodedMessage = URLEncoder.encode(base64EncodedMessage, "UTF-8");
    return urlEncodedMessage;
}

From source file:model.Encryption.java

/**
 * @param plainText/*from   w w  w.  j a  v  a2  s.  co  m*/
 * @return
 * @throws Exception
 */
public String encrypt(String plainText) throws Exception {
    if (secret == null)
        generateSecretKey();

    //encrypt the message
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8"));

    return new Base64().encodeBase64String(encryptedTextBytes);
}

From source file:ch.cern.security.saml2.utils.xml.XMLUtils.java

public static String xmlDecodeAndInflate(String encodedXmlString, boolean isDebugEnabled)
        throws UnsupportedEncodingException, DataFormatException {

    // URL decode
    // No need to URL decode: auto decoded by request.getParameter() method (GET)

    if (isDebugEnabled)
        nc.notice("Encoded XML String: " + encodedXmlString);

    // Base64 decode
    Base64 base64Decoder = new Base64();
    byte[] xmlBytes = encodedXmlString.getBytes("UTF-8");
    byte[] base64DecodedByteArray = base64Decoder.decode(xmlBytes);

    if (isDebugEnabled)
        nc.notice("Base64 decoded bytes: " + new String(base64DecodedByteArray, "UTF-8"));

    // Inflate (uncompress) the AuthnRequest data
    // First attempt to unzip the byte array according to DEFLATE (rfc 1951)
    Inflater inflater = new Inflater(true);
    inflater.setInput(base64DecodedByteArray);
    // since we are decompressing, it's impossible to know how much space we
    // might need; hopefully this number is suitably big
    byte[] xmlMessageBytes = new byte[5000];
    int resultLength = inflater.inflate(xmlMessageBytes);

    if (!inflater.finished()) {
        throw new RuntimeException("didn't allocate enough space to hold " + "decompressed data");
    }/*  w w  w. j  a v  a 2 s. c  o m*/

    inflater.end();

    String decodedString = new String(xmlMessageBytes, 0, resultLength, "UTF-8");

    if (isDebugEnabled)
        nc.notice("Decoded and inflated string (UTF-8): " + new String(base64DecodedByteArray));

    return decodedString;
}

From source file:com._64bitlabs.util.Encryptors.java

/**
 * Given AES encryption algorithm, decrypts the string value.
 * @param encryptedData/*  www .jav a 2 s  . c  o m*/
 * @return decrypted String
 */
public static String decrypt(String encryptedData) {
    Key key = generateKey();
    try {
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);

        Base64 dec = new Base64();
        byte[] decodedValue = dec.decode(encryptedData.getBytes());
        byte[] decValue = c.doFinal(decodedValue);

        return new String(decValue);
    } catch (Exception e) {
        return null;
    }
}

From source file:com.socialize.oauth.signpost.signature.OAuthMessageSigner.java

public OAuthMessageSigner() {
    this.base64 = new Base64();
}

From source file:net.couchcontroller.Connection.java

private void authenticate(HttpURLConnection con) {
    if (username != null && pass != null) {
        //con.setRequestProperty("User-Agent", USER_AGENT);
        String basicAuth = "Basic "
                + new String(new Base64().encode(new String(username + ":" + pass).getBytes()));
        con.setRequestProperty("Authorization", basicAuth);
    }/*from  www . j  a  v  a 2  s  . c o m*/
}