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:com.ah.ui.actions.home.clientManagement.service.CertificateGenSV.java

private String importCertificate(String customerId, String hmId, String certType, String certName,
        byte[] certPayload, String certPrivateKey, String privateKeyPassword) {
    XStream xs = new XStream(new DomDriver());
    CertificateImportEBO certImport = new CertificateImportEBO();
    certImport.setCustomId(customerId);// ww w .j a  v  a  2 s  .  c om
    certImport.setHmId(hmId);
    if (certType != "" && certType != null) {
        certImport.setCertType(certType);
    }
    if (certPrivateKey != "" && certPrivateKey != null) {
        certImport.setCertPrivateKey(new Base64().encodeToString(certPrivateKey.getBytes()));
    }
    if (certName != "" && certName != null) {
        certImport.setCertName(certName);
    }
    if (privateKeyPassword != "" && privateKeyPassword != null) {
        certImport.setPrivateKeyPassword(privateKeyPassword);
    }
    if (certPayload != null) {
        Base64 b64 = new Base64();
        certImport.setCertPayload(b64.encodeToString(certPayload));
    }
    xs.processAnnotations(CertificateImportEBO.class);
    return xs.toXML(certImport);
}

From source file:com.asquareb.kaaval.MachineKey.java

/**
 * Method to decode string to bytes. Uses Apache implementation of Base 64
 * CODEC//from  w w  w  .j  a v  a  2s.  c  o m
 */
private static byte[] base64Decode(String property) throws IOException {
    return new Base64().decode(property);
}

From source file:com.arm.connector.bridge.core.Utils.java

public static String decodeCoAPPayload(String payload) {
    String decoded = null;/*w  ww.j av  a  2s  . c  o  m*/

    try {
        String b64_payload = payload.replace("\\u003d", "=");
        Base64 decoder = new Base64();
        byte[] data = decoder.decode(b64_payload);
        decoded = new String(data);
    } catch (Exception ex) {
        decoded = "<unk>";
    }

    return decoded;
}

From source file:com.intellij.util.net.HttpConfigurable.java

private String decode(String value) {
    return new String(new Base64().decode(value.getBytes()));
}

From source file:com.intellij.util.net.HttpConfigurable.java

private String encode(String password) {
    return new String(new Base64().encode(password.getBytes()));
}

From source file:ch.cern.security.saml2.utils.UrlUtils.java

/**
 * Preprocess the request and invokes the verification:
 * /*from w w w.j  a  v a  2 s.  c o  m*/
 * @param request
 * @param isDebugEnabled
 * @return
 * @throws Exception
 */
public static boolean verify(HttpServletRequest request, boolean isDebugEnabled) throws Exception {

    StringBuffer data = new StringBuffer();
    String sigAlg = null;
    String signature = null;
    data.append(Constants.SAML_REQUEST);
    data.append(EQUAL);

    if (isDebugEnabled)
        nc.notice("Query String: " + request.getQueryString());

    if (request.getParameter(Constants.SAML_REQUEST) != null) {
        if (isDebugEnabled)
            nc.notice(request.getParameter(Constants.SAML_REQUEST));
        data.append(LogoutUtils.urlEncode(request.getParameter(Constants.SAML_REQUEST),
                Constants.CHARACTER_ENCODING));
    } else {
        nc.error("NO " + Constants.SAML_REQUEST + " parameter");
        throw new Exception("NO " + Constants.SAML_REQUEST + " parameter");
    }

    // Get the sigAlg, it should match with the one declared as context
    // param
    sigAlg = request.getParameter(Constants.SIG_ALG);
    if (isDebugEnabled)
        nc.notice(sigAlg);
    if (((String) request.getSession().getServletContext().getAttribute(Constants.SIG_ALG)).equals(sigAlg)) {
        data.append(AMPERSAND);
        data.append(Constants.SIG_ALG);
        data.append(EQUAL);
        data.append(LogoutUtils.urlEncode(sigAlg, Constants.CHARACTER_ENCODING));
    } else {
        throw new NoSuchAlgorithmException();
    }

    // Get the signature and decode it in Base64
    Base64 base64 = new Base64();
    if (request.getParameter(SIGNATURE) != null) {
        if (isDebugEnabled)
            nc.notice("Signature: " + request.getParameter(SIGNATURE));
        signature = request.getParameter(SIGNATURE);
    } else {
        nc.error("NO " + SIGNATURE + " parameter");
        throw new Exception("NO " + SIGNATURE + " parameter");
    }

    if (isDebugEnabled)
        nc.notice("Data to verify: " + data.toString());

    // Verify
    return SignatureUtils.verify(data.toString().getBytes(), (byte[]) base64.decode(signature.getBytes()),
            (PublicKey) request.getSession().getServletContext().getAttribute(Constants.IDP_PUBLIC_KEY));
}

From source file:mx.bigdata.sat.cfdi.CFDv33.java

public void verificar(InputStream in) throws Exception {
    String certStr = document.getCertificado();
    Base64 b64 = new Base64();
    byte[] cbs = b64.decode(certStr);

    X509Certificate cert = KeyLoaderFactory
            .createInstance(KeyLoaderEnumeration.PUBLIC_KEY_LOADER, new ByteArrayInputStream(cbs)).getKey();

    String sigStr = document.getSello();
    byte[] signature = b64.decode(sigStr);
    byte[] bytes = getOriginalBytes(in);
    Signature sig = Signature.getInstance("SHA256withRSA");
    sig.initVerify(cert);//from  w  ww  . j a  v a  2 s  .co  m
    sig.update(bytes);
    boolean bool = sig.verify(signature);
    if (!bool) {
        throw new Exception("Invalid signature.");
    }
}

From source file:com.aurel.track.item.AddScreenshotAction.java

public static void writeImage(String base64String) throws IOException {
    if (base64String != null) {
        byte[] bytearray = new Base64().decode(base64String);
        BufferedImage imag = ImageIO.read(new ByteArrayInputStream(bytearray));
        ImageIO.write(imag, "jpg", new File(".", "snap.jpg"));
    }//  w w w  .  j  a  v a2  s  .  c  om
}

From source file:com.ning.metrics.collector.endpoint.resources.ScribeEventRequestHandler.java

private Event extractThriftEnvelopeEvent(final String category, final String message) throws TException {
    Event event;//from  w  w w.  ja v  a2 s.  c o  m
    final String[] payload = StringUtils.split(message, ":");

    if (payload == null || payload.length != 2) {
        // Invalid API
        throw new TException("Expected payload separator ':'");
    }

    Long eventDateTime = null;
    try {
        eventDateTime = Long.parseLong(payload[0]);
    } catch (RuntimeException e) {
        log.debug("Event DateTime not specified, defaulting to NOW()");
    }

    // The payload is Base64 encoded
    final byte[] thrift = new Base64().decode(payload[1].getBytes());

    // Assume a ThriftEnvelopeEvent from the eventtracker (uses Java serialization).
    // This is bigger on the wire, but the interface is portable. Serialize using TBinaryProtocol
    // if you care about size (see below).
    ObjectInputStream objectInputStream = null;
    try {
        objectInputStream = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(thrift)));
        event = new ThriftEnvelopeEvent();
        event.readExternal(objectInputStream);

        if (event.getName().equals(category)) {
            return event;
        }
    } catch (Exception e) {
        log.debug(String.format("Payload is not a ThriftEvent: %s", e.getLocalizedMessage()));
    } finally {
        try {
            if (objectInputStream != null) {
                objectInputStream.close();
            }
        } catch (IOException e) {
            log.warn("Unable to close stream when deserializing thrift events", e);
        }
    }

    // Not a ThriftEvent, probably native Thrift serialization (TBinaryProtocol)
    try {
        if (eventDateTime == null) {
            event = ThriftToThriftEnvelopeEvent.extractEvent(category, thrift);
        } else {
            event = ThriftToThriftEnvelopeEvent.extractEvent(category, new DateTime(eventDateTime), thrift);
        }
    } catch (TException e) {
        log.debug("Event doesn't look like a Thrift, assuming plain text");
        if (eventDateTime == null) {
            event = StringToThriftEnvelopeEvent.extractEvent(category, payload[1]);
        } else {
            event = StringToThriftEnvelopeEvent.extractEvent(category, new DateTime(eventDateTime), payload[1]);
        }
    }
    return event;
}

From source file:de.alpharogroup.crypto.key.KeyExtensions.java

/**
 * Read pem private key.//w  w w .j  a  v  a2 s  . c om
 *
 * @param file
 *            the file
 * @param securityProvider
 *            the security provider
 * @return the private key
 * @throws Exception
 *             is thrown if if a security error occur
 */
public static PrivateKey readPemPrivateKey(final File file, final SecurityProvider securityProvider)
        throws Exception {
    final byte[] keyBytes = Files.readAllBytes(file.toPath());

    final String privateKeyAsString = new String(keyBytes).replace(BEGIN_RSA_PRIVATE_KEY_PREFIX, "")
            .replace(END_RSA_PRIVATE_KEY_SUFFIX, "").trim();

    final byte[] decoded = new Base64().decode(privateKeyAsString);

    return readPrivateKey(decoded, securityProvider);
}