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.rs.worldserver.io.IOClient.java

public String encrypt(String plaintext) {
    MessageDigest md = null;//from   w  w w  . j a v a 2  s  .c o m
    try {
        md = MessageDigest.getInstance("SHA"); //step 2
    } catch (NoSuchAlgorithmException e) {
    }
    try {
        md.update(plaintext.getBytes("UTF-8")); //step 3
    } catch (UnsupportedEncodingException e) {
    }
    byte raw[] = md.digest(); //step 4
    String hash = (new Base64()).encodeToString(raw); //step 5
    return hash; //step 6
}

From source file:com.forsrc.utils.MyRsa2Utils.java

/**
 * Gets public key.// ww  w . j  a v  a2s  . c o m
 *
 * @param key the key
 * @return the public key
 * @throws RsaException the rsa exception
 */
public static PublicKey getPublicKey(String key) throws RsaException {
    byte[] keyBytes;
    try {
        keyBytes = (new Base64()).decode(key);
    } catch (Exception e) {
        throw new RsaException(e);
    }
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = null;
    try {
        keyFactory = KeyFactory.getInstance(RsaKey.ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        throw new RsaException(e);
    }
    PublicKey publicKey = null;
    try {
        publicKey = keyFactory.generatePublic(keySpec);
    } catch (InvalidKeySpecException e) {
        throw new RsaException(e);
    }
    return publicKey;
}

From source file:net.instantcom.mm7.DeliverReqTest.java

@Test
public void readCaiXinDatafromHW() throws IOException, MM7Error {
    String ct = "multipart/related; boundary=\"--NextPart_0_9094_20600\"; type=text/xml";
    InputStream in = DeliverReq.class.getResourceAsStream("caixin.txt");
    DeliverReq req = (DeliverReq) MM7Response.load(in, ct, new MM7Context());

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost post = null;//w  w w .j  a v a  2 s  .  c  om
    try {
        post = new HttpPost("http://127.0.0.1:8081/mm7serv/10085receiver");
        //post = new HttpPost("http://42.96.185.95:8765");
        post.addHeader("Content-Type", req.getSoapContentType());
        post.addHeader("SOAPAction", "");
        ByteArrayOutputStream byteos = new ByteArrayOutputStream();
        MM7Message.save(req, byteos, new MM7Context());
        post.setEntity(new ByteArrayEntity(byteos.toByteArray()));
        HttpResponse resp = httpclient.execute(post);
        HttpEntity entity = resp.getEntity();
        String message = EntityUtils.toString(entity, "utf-8");
        System.out.println(message);
    } catch (Exception e) {

    } finally {
        if (post != null)
            post.releaseConnection();
    }

    for (Content c : req.getContent()) {
        ContentType ctype = new ContentType(c.getContentType());
        if (ctype.getPrimaryType().equals("image")) {
            BinaryContent image = (BinaryContent) c;
            String fileName = DeliverReq.class.getResource("caixin.txt").getFile() + ".jpg";
            System.out.println(fileName);
            Base64 base64 = new Base64();
            FileUtils.writeByteArrayToFile(new File(fileName), base64.encode(image.getData()));
        }
    }
    ByteArrayOutputStream byteos = new ByteArrayOutputStream();
    MM7Message.save(req, byteos, new MM7Context());

    req = (DeliverReq) MM7Response.load(new ByteArrayInputStream(byteos.toByteArray()),
            req.getSoapContentType(), new MM7Context());
    assertEquals("+8618703815655", req.getSender().toString());
}

From source file:eu.europa.ec.markt.dss.validation.tsp.OnlineTSPSource.java

/**
 * Get timestamp token - communications layer
 * //from   w  ww  .j  a v a2 s  .c o m
 * @return - byte[] - TSA response, raw bytes (RFC 3161 encoded)
 */
protected byte[] getTSAResponse(byte[] requestBytes) throws IOException {
    // Setup the TSA connection

    URL tspUrl = new URL(tspServer);
    URLConnection tsaConnection = tspUrl.openConnection();

    tsaConnection.setDoInput(true);
    tsaConnection.setDoOutput(true);
    tsaConnection.setUseCaches(false);
    tsaConnection.setRequestProperty("Content-Type", "application/timestamp-query");
    // tsaConnection.setRequestProperty("Content-Transfer-Encoding",
    // "base64");
    tsaConnection.setRequestProperty("Content-Transfer-Encoding", "binary");

    OutputStream out = tsaConnection.getOutputStream();
    out.write(requestBytes);
    out.close();

    // Get TSA response as a byte array
    InputStream inp = tsaConnection.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    while ((bytesRead = inp.read(buffer, 0, buffer.length)) >= 0) {
        baos.write(buffer, 0, bytesRead);
    }
    byte[] respBytes = baos.toByteArray();

    String encoding = tsaConnection.getContentEncoding();
    if (encoding != null && encoding.equalsIgnoreCase("base64")) {
        respBytes = new Base64().decode(respBytes);
    }
    return respBytes;
}

From source file:jlib.xml.XMLUtil.java

public static byte[] decode64(byte[] arrBytes) {
    Base64 base64 = new Base64();
    return base64.decode(arrBytes);
}

From source file:jlib.xml.XMLUtil.java

public static String encode64AsString(byte[] arrBytes) {
    Base64 base64 = new Base64();
    byte[] tOut = base64.encode(arrBytes);
    String cs = new String(tOut);
    return cs;/*from  w w w. j a va 2  s  .c o  m*/
}

From source file:com.beetle.framework.util.OtherUtil.java

public static final String md5AndBase64Encode(String str) {
    byte[] b = md5(str);
    if (b == null) {
        throw new AppRuntimeException("md5 encode err!");
    }//w  w  w.  j  av  a 2s  .  c o  m
    Base64 b64 = new Base64();
    b = b64.encode(b);
    return new String(b);
}

From source file:es.logongas.ix3.web.security.impl.WebSessionSidStorageImplAbstractJws.java

private Serializable unserialize(String s) {
    try {//from www . j a va 2  s . c  om
        Base64 base64 = new Base64();
        byte b[] = base64.decode(s);
        ByteArrayInputStream bi = new ByteArrayInputStream(b);
        ObjectInputStream si = new ObjectInputStream(bi);
        Serializable serializable = (Serializable) si.readObject();

        return serializable;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

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

public void verificar() 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();
    Signature sig = Signature.getInstance("SHA1withRSA");
    sig.initVerify(cert);//from  www  . j a  va  2 s . c  o m
    sig.update(bytes);
    boolean bool = sig.verify(signature);
    if (!bool) {
        throw new Exception("Invalid signature");
    }
}

From source file:mx.bigdata.sat.cfd.CFDv2.java

public void verificar(Certificate cert) throws Exception {
    String sigStr = document.getSello();
    Base64 b64 = new Base64();
    byte[] signature = b64.decode(sigStr);
    byte[] bytes = getOriginalBytes();
    boolean md5 = true;
    if (getYear() < 2011) {
        Signature sig = Signature.getInstance("MD5withRSA");
        sig.initVerify(cert);//from w  w w.ja  v  a2s. com
        sig.update(bytes);
        try {
            sig.verify(signature);
        } catch (SignatureException e) {
            // Not MD5
            md5 = false;
        }
    }
    if (getYear() > 2010 || !md5) {
        Signature sig = Signature.getInstance("SHA1withRSA");
        sig.initVerify(cert);
        sig.update(bytes);
        boolean bool = sig.verify(signature);
        if (!bool) {
            throw new Exception("Invalid signature");
        }
    }
}