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(final int lineLength) 

Source Link

Document

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

Usage

From source file:ml.shifu.shifu.util.Base64Utils.java

public static String base64Encode(String text) {
    if (text == null) {
        return null;
    }/*ww  w  .j a v a 2 s.co m*/

    Base64 encoder = new Base64(-1);
    try {
        return new String(encoder.encode(text.getBytes(Constants.DEFAULT_CHARSET)), Constants.DEFAULT_CHARSET);
    } catch (UnsupportedEncodingException e) {
        return null;
    }
}

From source file:com.laudandjolynn.avf.utils.SecurityCoder.java

/**
 * //from   ww  w  .  ja v a2 s . c  om
 * @param data
 * @param urlSafe
 * @return
 */
public static byte[] base64Decoder(String data, boolean urlSafe) {
    Base64 base64 = new Base64(urlSafe);
    return base64.decode(data);
}

From source file:zipB64.java

protected static String encodeMessage(String messageStr) {
    try {/* ww  w  .ja v a2s .co  m*/
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        Deflater deflater = new Deflater(Deflater.DEFLATED);
        DeflaterOutputStream deflaterStream = new DeflaterOutputStream(bytesOut, deflater);
        deflaterStream.write(messageStr.getBytes("UTF-8"));
        deflaterStream.finish();

        Base64 b = new Base64(-1);
        return new String(b.encode(bytesOut.toByteArray()));
    } catch (Exception e) {
        return "crotte";
    }
}

From source file:ml.shifu.shifu.util.Base64Utils.java

public static String base64Decode(String text) throws IOException {
    if (text == null) {
        return null;
    }// w  w w . ja  va  2 s  . c om

    Base64 decoder = new Base64(-1);
    return new String(decoder.decode(text.getBytes(Constants.DEFAULT_CHARSET)), Constants.DEFAULT_CHARSET);
}

From source file:com.laudandjolynn.avf.utils.SecurityCoder.java

/**
 * /*from  ww  w  .  j  av a 2s . com*/
 * @param data
 * @param urlSafe
 * @return
 */
public static String base64Encoder(byte[] data, boolean urlSafe) {
    Base64 base64 = new Base64(urlSafe);
    return base64.encodeToString(data);
}

From source file:com.spotify.echoprintserver.nativelib.Util.java

public static byte[] b64SafeDecode(String s) {
    return new Base64(true).decodeBase64(s);
}

From source file:com.buddycloud.mediaserver.commons.AuthBean.java

private static String decodeAuth(String authToken) {
    Base64 decoder = new Base64(true);
    return new String(decoder.decode(authToken.getBytes()));
}

From source file:com.floreantpos.ui.dialog.LicenseDialog.java

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.//  w w w  .java 2  s . c  o  m
 */
@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    this.setTitle("License Key POS");

    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    idTF = new javax.swing.JTextField();
    keyTF = new javax.swing.JTextField();
    saveBtn = new javax.swing.JButton();
    cancelBtn = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    //if (StringUtils.isEmpty(AppConfig.getMachineId())) {

    try {

        String[] nets = new Sigar().getNetInterfaceList();
        NetInterfaceConfig c = new Sigar().getNetInterfaceConfig(nets[0]);
        Base64 base64 = new Base64(true);

        String machineId = base64.encodeAsString(c.getHwaddr().getBytes()).trim().toUpperCase();

        idTF.setText(machineId);

    } catch (Exception e) {
        e.printStackTrace();
    }
    //        }
    //    else {
    //            idTF.setText(AppConfig.getMachineId());
    //        }

    jLabel1.setText("ID Mesin");

    jLabel2.setText("License Key");

    saveBtn.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
    saveBtn.setText("S a v e");
    saveBtn.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            saveBtnActionPerformed(evt);
        }
    });

    cancelBtn.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
    cancelBtn.setText("C a n c e l");

    cancelBtn.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cancelBtnActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addGroup(layout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup().addContainerGap()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1).addComponent(jLabel2))
                            .addGap(31, 31, 31)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(idTF).addComponent(keyTF)))
                    .addGroup(layout.createSequentialGroup().addGap(90, 90, 90)
                            .addComponent(saveBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 104,
                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(32, 32, 32).addComponent(cancelBtn).addGap(0, 49, Short.MAX_VALUE)))
                    .addContainerGap()));
    layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addGap(61, 61, 61)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1).addComponent(idTF, javax.swing.GroupLayout.PREFERRED_SIZE,
                                    40, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(32, 32, 32)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2).addComponent(keyTF, javax.swing.GroupLayout.PREFERRED_SIZE,
                                    36, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 46, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(saveBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 40,
                                    javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cancelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 40,
                                    javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(45, 45, 45)));

    pack();
}

From source file:com.vmware.bdd.security.EncryptionGuard.java

/**
 * Encrypt the clear text against given secret key.
 * /* w ww. j av a 2s.c  o  m*/
 * @param clearText
 *           the clear string
 * @return the encrypted string, or null if the clear string is null
 * @throws CommonException
 *            if input arguments is null
 */
public static String encode(String clearText) throws GeneralSecurityException, UnsupportedEncodingException {
    if (clearText == null) {
        return null;
    }

    Key key = GuardKeyStore.getEncryptionKey();
    String salt = SaltGenerator.genRandomString(SALT_SIZE);

    String inputText = salt + clearText; // add salt
    byte[] clearBytes = inputText.getBytes(UTF8_ENCODING);

    Cipher cipher = getCiperInternal(Cipher.ENCRYPT_MODE, key);
    byte[] encryptedBytes = cipher.doFinal(clearBytes);

    Base64 base64 = new Base64(0); // 0 - no chunking
    return salt + base64.encodeToString(encryptedBytes);
}

From source file:com.spartan.springmvc.bean.FacebookSignatureBean.java

/**
 * Parses and verifies a Facebook signed_request parameter. The data of the signed request is returned on successful verification.
 *
 * @param signedRequest//from   w w w .j a  va  2  s.c o m
 * @param appSecret
 * @return 
 * @return
 * @throws FacebookSignatureVerificationFailedException
 */
@SuppressWarnings("unchecked")
public <T> T parseSignature(String signedRequest, String appSecret, Class<T> clazz)
        throws FacebookSignatureVerificationFailedException {

    String[] parts = signedRequest.split("\\.");
    if (parts.length != 2) {
        throw new FacebookSignatureVerificationFailedException("Invalid signature format.");
    }

    String encSig = parts[0];
    String encPayload = parts[1];
    Base64 decoder = new Base64(true);

    try {
        Mac mac = Mac.getInstance("HMACSHA256");
        mac.init(new SecretKeySpec(appSecret.getBytes(), mac.getAlgorithm()));
        byte[] calcSig = mac.doFinal(encPayload.getBytes());
        byte[] decodedSig = decoder.decode(encSig);
        boolean isVerified = Arrays.equals(decodedSig, calcSig);

        if (isVerified) {
            try {
                String unsignedData = new String(decoder.decode(encPayload));
                logger.debug("unsignedData: " + unsignedData);
                ObjectMapper mapper = new ObjectMapper();
                mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                T data = mapper.readValue(unsignedData, (Class<T>) clazz);
                return data;
            } catch (IOException e) {
                throw new FacebookSignatureVerificationFailedException(
                        "Failed to parse JSON data: " + e.getMessage(), e);
            }
        } else {
            throw new FacebookSignatureVerificationFailedException("Signatures do not match.");
        }

    } catch (NoSuchAlgorithmException e) {
        throw new FacebookSignatureVerificationFailedException(e);
    } catch (InvalidKeyException e) {
        throw new FacebookSignatureVerificationFailedException(e);
    }
}