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.intera.roostrap.util.EncryptionUtil.java

public static String encrypt(String encryptionKey, String plainText) throws InvalidKeyException {
    InputStream inStream = new ByteArrayInputStream(toBytes(plainText));
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    try {/*from   w w  w . java  2s. c o  m*/
        encrypt(encryptionKey, inStream, outStream);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    Base64 base64 = new Base64();
    return toString(base64.encode(outStream.toByteArray()));
}

From source file:de.openflorian.crypt.provider.BlowfishCipher.java

@Override
public String encrypt(String str) throws GeneralSecurityException {
    if (key == null || key.isEmpty())
        throw new IllegalStateException("The Blowfish Secret is not set or is length=0.");

    if (str == null)
        return null;

    try {/*from w  w w  .ja  v a 2 s . co  m*/
        SecretKeySpec keySpec;
        keySpec = new SecretKeySpec(key.getBytes("UTF8"), "Blowfish");

        Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);

        return new String(new Base64().encode(cipher.doFinal(str.getBytes("UTF8")))).trim();
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new GeneralSecurityException(e.getMessage(), e);
    }
}

From source file:com.smash.revolance.ui.model.helper.ImageHelper.java

public static BufferedImage decodeToImage(String imageString) throws Exception {
    imageString = imageString.split(BASE64_IMAGE_PNG)[1];
    BufferedImage image = null;//from   w  w  w  . j a va  2 s .co m
    byte[] imageByte;

    Base64 decoder = new Base64();
    imageByte = decoder.decode(imageString);
    ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
    try {
        image = ImageIO.read(bis);
    } finally {
        IOUtils.closeQuietly(bis);
    }

    return image;
}

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

private static String base64Encode(String plaintext) {
    try {/*w  w  w .  j a v a  2  s.c o m*/
        return new String(new Base64().encode(plaintext.getBytes(CHAR_SET)));
    } catch (UnsupportedEncodingException e) {
        return new String(new Base64().encode(plaintext.getBytes()));
    }
}

From source file:com.google.gsa.valve.saml.SAMLArtifactProcessor.java

/**
 * Creates the artifact string//from  w ww  .j a  v a2s.co  m
 * 
 * @return the artifact
 */
public static String createArtifact() {
    Base64 base64 = new Base64();
    String artifact = createRandomHexString(20);
    return new String(base64.encode(artifact.getBytes()));

}

From source file:mercury.DigitalMediaController.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {
        ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
        try {/*from   w w w.ja va2  s.c o  m*/
            List items = upload.parseRequest(request);
            Iterator iter = items.iterator();

            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();

                if (!item.isFormField()) {
                    Integer serial = (new DigitalMediaDAO()).uploadToDigitalMedia(item);

                    String filename = item.getName();
                    if (filename.lastIndexOf('\\') != -1) {
                        filename = filename.substring(filename.lastIndexOf('\\') + 1);
                    }
                    if (filename.lastIndexOf('/') != -1) {
                        filename = filename.substring(filename.lastIndexOf('/') + 1);
                    }

                    String id = serial + ":" + filename;
                    String encodedId = new String(new Base64().encode(id.getBytes()));
                    encodedId = encodedId.replaceAll("\\\\", "_");
                    if (serial != null && serial != 0) {
                        response.getWriter().write("{success: true, id: \"" + encodedId + "\"}");
                        return;
                    }
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }
        response.getWriter().write("{success: false}");
    } else {
        String decodedId = null;
        DigitalMediaDTO dto = null;

        try {
            String id = request.getParameter("id");
            id = id.replaceAll("_", "\\\\");
            decodedId = new String(new Base64().decode(id.getBytes()));

            String[] splitId = decodedId.split(":");
            if (splitId.length == 2 && splitId[0].matches("^\\d+$")) {
                dto = (new DigitalMediaDAO()).getDigitalMedia(Integer.valueOf(splitId[0]), splitId[1]);
            }
        } catch (Exception e) {
            // dto should be null here
        }

        InputStream in = null;
        byte[] bytearray = null;
        int length = 0;
        String defaultFile = request.getParameter("default");
        response.reset();
        try {
            if (dto != null && dto.getIn() != null) {
                response.setContentType(dto.getMimeType());
                response.setHeader("Content-Disposition", "filename=" + dto.getFileName());
                length = dto.getLength();
                in = dto.getIn();
            }

            if (in == null && StringUtils.isNotBlank(defaultFile)) {
                String path = getServletContext().getRealPath("/");
                File file = new File(path + defaultFile);
                length = (int) file.length();
                in = new FileInputStream(file);
            }

            if (in != null) {
                bytearray = new byte[length];
                int index = 0;
                OutputStream os = response.getOutputStream();
                while ((index = in.read(bytearray)) != -1) {
                    os.write(bytearray, 0, index);
                }
                in.close();
            } else {
                response.getWriter().write("{success: false}");
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().write("{success: false}");
        }
        response.flushBuffer();
    }
}

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

/**
 * Encrypt string./*from  ww  w. j  a  v a 2 s  . co  m*/
 *
 * @param des the des
 * @param src the src
 * @return String string
 * @throws DesException the des exception
 * @Title: encrypt
 * @Description:
 */
public static String encrypt(DesKey des, String src) throws DesException {
    if (des == null) {
        throw new DesException("DesKey is null.");
    }
    if (src == null) {
        throw new DesException("Encrypted code is null.");
    }
    byte[] encrypted = null;
    try {
        encrypted = des.getCipher(true).doFinal(src.getBytes(CHARSET_UTF8));
    } catch (IllegalBlockSizeException e) {
        throw new DesException(e);
    } catch (BadPaddingException e) {
        throw new DesException(e);
    } catch (UnsupportedEncodingException e) {
        throw new DesException(e);
    }
    return new String(new Base64().encode(encrypted));
}

From source file:be.wolkmaan.klimtoren.security.digest.StandardStringDigester.java

private StandardStringDigester(final StandardByteDigester standardByteDigester) {
    super();/*from w w  w. ja  v a 2 s . c  o m*/
    this.byteDigester = standardByteDigester;
    this.base64 = new Base64();
}

From source file:license.ExtraTestWakeLicense.java

/**
 * wakelicense/*w  w  w  . j a  v a  2s  . c o  m*/
 * @throws Exception
 */
@Test
public void testWake() throws Exception {

    pi.setClient("");
    pi.setName("");
    pi.setContact("");
    pi.setTel("15000100001");
    pi.setEmail("dcm19890101@163.com");

    String info = projectInfoToString(pi);
    System.out.println(
            "---===(*****?info.getBytes()?Register.data)"
                    + info);

    System.out.println();
    System.out.println();

    readProjectInfo(pi, info);

    System.out.println();
    System.out.println();

    setInput(pi);

    System.out.println();
    System.out.println();

    //??testData
    testData = toString(pi);
    System.out.println(testData);

    System.out.println(extraData());

    System.out.println();
    System.out.println();

    //
    //wake*?*license?
    //
    String[] ss = testData.split("\n");
    String blowfishInfo = new Blowfish(Date.class.getName()).decryptString(ss[0]);
    byte[] rmd5 = md5(blowfishInfo);
    //?base64?
    byte[] lmd5 = rsa(new Base64().decode(ss[1]));
    System.out.println("?? " + equalsBytes(rmd5, lmd5));

}

From source file:com.hp.alm.ali.idea.cfg.AliConfiguration.java

public synchronized Element getState() {
    Element element = new Element(getClass().getSimpleName());
    addProperty(element, PROPERTY_LOCATION, ALM_LOCATION);
    addProperty(element, PROPERTY_DOMAIN, ALM_DOMAIN);
    addProperty(element, PROPERTY_PROJECT, ALM_PROJECT);
    addProperty(element, PROPERTY_USERNAME, ALM_USERNAME);
    if (STORE_PASSWORD) {
        addProperty(element, PROPERTY_PASSWORD, new String(new Base64().encode(ALM_PASSWORD.getBytes())));
    }//  ww  w  .j  a v a2  s . c  o  m
    addProperty(element, PROPERTY_STORE_PASSWORD, STORE_PASSWORD);
    element.addContent(getStoredFilters());
    addProperty(element, PROPERTY_STATUS_TRANSITION, STATUS_TRANSITION);
    addProperty(element, PROPERTY_SPELL_CHECKER, String.valueOf(spellChecker));
    addProperty(element, PROPERTY_DEV_MOTIVE_ANNOTATION, String.valueOf(devMotiveAnnotation));
    return element;
}