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.forsrc.utils.MyRsaUtils.java

/**
 * Big integers 2 string string.// ww  w .  jav  a 2 s  . c o  m
 *
 * @param bigIntegers the big integers
 * @return the string
 * @throws IOException the io exception
 */
public static String bigIntegers2String(BigInteger[] bigIntegers) throws IOException {

    StringBuilder plaintext = new StringBuilder(BLOCK_SIZE + 1);

    for (int i = 0; i < bigIntegers.length; i++) {
        plaintext.append(toStr(bigIntegers[i]));
    }

    return new String(new Base64().decode(plaintext.toString()));
}

From source file:net.sourceforge.jaulp.crypto.SimpleEncryptor.java

/**
 * Encrypt the given String./*from  w  w w  .  j av  a  2 s .  co m*/
 * 
 * @param string
 *            The String to encrypt.
 * @return The encrypted String.
 * @throws UnsupportedEncodingException
 *             is thrown if get the bytes from the given String object fails.
 * @throws BadPaddingException
 *             is thrown if {@link Cipher#doFinal(byte[])} fails.
 * @throws IllegalBlockSizeException
 *             is thrown if {@link Cipher#doFinal(byte[])} fails.
 * @throws InvalidAlgorithmParameterException
 *             is thrown if initialization of the cypher object fails.
 * @throws NoSuchPaddingException
 *             is thrown if instantiation of the cypher object fails.
 * @throws InvalidKeySpecException
 *             is thrown if generation of the SecretKey object fails.
 * @throws NoSuchAlgorithmException
 *             is thrown if instantiation of the SecretKeyFactory object fails.
 * @throws InvalidKeyException
 *             is thrown if initialization of the cypher object fails.
 *
 * @see net.sourceforge.jaulp.crypto.interfaces.Encryptor#encrypt(java.lang.String)
 */
@Override
public String encrypt(final String string) throws UnsupportedEncodingException, IllegalBlockSizeException,
        BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException,
        NoSuchPaddingException, InvalidAlgorithmParameterException {
    initialize();
    final byte[] utf8 = string.getBytes(CryptConst.ENCODING);
    final byte[] encrypt = this.cipher.doFinal(utf8);
    String encrypted = new Base64().encodeToString(encrypt);
    return encrypted;
}

From source file:license.rsa.WakeRSA.java

/**
 * ?mac//from  www.  j  av a2  s . c o m
 * @param sb
 * @throws Exception
 */
private static void mac(StringBuilder sb) throws Exception {
    Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
    int i = 12;
    Base64 base64 = new Base64();
    for (NetworkInterface ni : Collections.list(interfaces))
        if (ni.getName().substring(0, 1).equalsIgnoreCase("e")) {
            sb.append(String.valueOf(i)).append('=').append(ni.getName()).append('\n');
            i++;
            byte[] mac = ni.getHardwareAddress();
            sb.append(String.valueOf(i)).append('=').append(base64.encodeAsString(mac)).append('\n');
            i++;
        }
}

From source file:license.regist.ProjectInfo.java

public String toString() {
    try {//from   w  ww  .  j  av a  2 s.  c  om
        return encryptProjectInfo() + "\n" + new Base64().encodeAsString(rsa());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:de.alpharogroup.crypto.simple.SimpleEncryptor.java

/**
 * Encrypt the given String./*from   ww  w . j  a v a2 s  . c  o m*/
 * 
 * @param string
 *            The String to encrypt.
 * @return The encrypted String.
 * @throws UnsupportedEncodingException
 *             is thrown if get the bytes from the given String object fails.
 * @throws BadPaddingException
 *             is thrown if {@link Cipher#doFinal(byte[])} fails.
 * @throws IllegalBlockSizeException
 *             is thrown if {@link Cipher#doFinal(byte[])} fails.
 * @throws InvalidAlgorithmParameterException
 *             is thrown if initialization of the cypher object fails.
 * @throws NoSuchPaddingException
 *             is thrown if instantiation of the cypher object fails.
 * @throws InvalidKeySpecException
 *             is thrown if generation of the SecretKey object fails.
 * @throws NoSuchAlgorithmException
 *             is thrown if instantiation of the SecretKeyFactory object fails.
 * @throws InvalidKeyException
 *             is thrown if initialization of the cypher object fails.
 *
 * @see de.alpharogroup.crypto.interfaces.Encryptor#encrypt(java.lang.String)
 */
@Override
public String encrypt(final String string) throws UnsupportedEncodingException, IllegalBlockSizeException,
        BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException,
        NoSuchPaddingException, InvalidAlgorithmParameterException {
    initialize();
    final byte[] utf8 = string.getBytes(CryptConst.ENCODING);
    final byte[] encrypt = this.cipher.doFinal(utf8);
    final String encrypted = new Base64().encodeToString(encrypt);
    return encrypted;
}

From source file:co.cask.hydrator.plugin.DecoderTest.java

@Test
public void testBase64AsStringDecoder() throws Exception {
    String test = "This is a test for testing string base64 decoding";
    Transform<StructuredRecord, StructuredRecord> encoder = new Encoder(
            new Encoder.Config("a:STRING_BASE64", OUTPUT.toString()));
    encoder.initialize(null);//from w w  w.  j av a2s.c o m

    MockEmitter<StructuredRecord> emitterEncoded = new MockEmitter<>();
    encoder.transform(StructuredRecord.builder(INPUT).set("a", test).set("b", "2").set("c", "3").set("d", "4")
            .set("e", "5").build(), emitterEncoded);

    Base64 base64 = new Base64();
    byte[] expected = base64.encode(test.getBytes("UTF-8"));
    byte[] actual = emitterEncoded.getEmitted().get(0).get("a");
    Assert.assertEquals(2, emitterEncoded.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertArrayEquals(expected, actual);

    Transform<StructuredRecord, StructuredRecord> decoder = new Decoder(
            new Decoder.Config("a:STRING_BASE64", OUTPUTSTR.toString()));
    decoder.initialize(null);
    MockEmitter<StructuredRecord> emitterDecoded = new MockEmitter<>();
    decoder.transform(emitterEncoded.getEmitted().get(0), emitterDecoded);
    Assert.assertEquals(2, emitterDecoded.getEmitted().get(0).getSchema().getFields().size());
    Assert.assertEquals(test, emitterDecoded.getEmitted().get(0).get("a"));
}

From source file:eu.scenari.gephi.importer.ScenariConnector.java

protected String sendRequest(int pRequest) {
    HttpURLConnection vConnection = null;
    String vResult = null;/*from  w  ww .  j  av a  2 s  .c om*/
    try {
        URL vUrl;
        switch (pRequest) {
        case GET_WSP_LIST:
            vUrl = new URL(fUrl + "/s/" + fEnvironnement + "/u/adminWsp?cdaction=List&withWspTypes=true");

            break;

        case GET_GRAPH:
            String vStrUrl = fUrl + "/s/" + fEnvironnement + "/u/search/?param=" + fWorkspace
                    + "&request=<request><select><column%20dataKey=\"";
            if (fOdbBackend)
                vStrUrl += "srcId";
            else
                vStrUrl += "srcUri";
            vStrUrl += "\"/><column%20dataKey=\"itItemObject\"/></select><where><exp%20type=\"ItemSrcType\"%20srcTypes=\"12\"/><exp%20type=\"RootFolder\"%20path=\"\"/></where></request>";
            vUrl = new URL(vStrUrl);
            break;

        default:
            vUrl = new URL(null);
            break;
        }
        vConnection = (HttpURLConnection) vUrl.openConnection();
        vConnection.setRequestMethod("GET");
        if (fLogin != null) {
            Base64 vEncoder = new Base64();
            String vAuth = fLogin + ":" + fPassword;
            String vEncodedAuth = "Basic " + vEncoder.encodeAsString(vAuth.getBytes());
            System.out.println(vEncodedAuth);
            vConnection.setRequestProperty("Authorization", vEncodedAuth);
        }

        vConnection.setDoInput(true);

        //Get Response   
        InputStream vInput = vConnection.getInputStream();
        BufferedReader vReader = new BufferedReader(new InputStreamReader(vInput));
        String vLine;
        StringBuilder vResponse = new StringBuilder();
        while ((vLine = vReader.readLine()) != null) {
            vResponse.append(vLine);
        }
        vReader.close();

        vResult = vResponse.toString();
    } catch (Exception ex) {
        //Exceptions.printStackTrace(ex);
    } finally {
        if (vConnection != null)
            vConnection.disconnect();
    }
    return vResult;
}

From source file:net.duckling.ddl.web.controller.pan.WopiController.java

private String encodeStr(String plainText) {
    byte[] b = plainText.getBytes();
    Base64 base64 = new Base64();
    b = base64.encode(b);//from ww  w  . j  a  v a  2  s  .  c o m
    String s = new String(b);
    return s;
}

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

public void verify() throws Exception {
    String certStr = document.getCertificado();
    Base64 b64 = new Base64();
    byte[] cbs = b64.decode(certStr);
    X509Certificate cert = KeyLoader.loadX509Certificate(new ByteArrayInputStream(cbs));
    cert.checkValidity();/*from ww  w  .  jav a  2  s  .  c  o m*/
    String sigStr = document.getSello();
    byte[] signature = b64.decode(sigStr);
    byte[] bytes = getOriginalBytes();
    Signature sig = Signature.getInstance("SHA1withRSA");
    sig.initVerify(cert);
    sig.update(bytes);
    boolean bool = sig.verify(signature);
    if (!bool) {
        throw new Exception("Invalid signature");
    }
}

From source file:mx.bigdata.cfdi.TFDv1.java

public int verify(Certificate cert) throws Exception {
    if (tfd == null) {
        return 601; //No contiene timbrado
    }/*from ww w. j a  va 2  s  .  c  o  m*/
    Base64 b64 = new Base64();
    String sigStr = tfd.getSelloSAT();
    byte[] signature = b64.decode(sigStr);
    byte[] bytes = getOriginalBytes();
    Signature sig = Signature.getInstance("SHA1withRSA");
    sig.initVerify(cert);
    sig.update(bytes);
    boolean verified = sig.verify(signature);
    return verified ? 600 : 602; //Sello del timbrado no valido
}