Example usage for org.apache.commons.codec.binary Base64 encodeBase64URLSafeString

List of usage examples for org.apache.commons.codec.binary Base64 encodeBase64URLSafeString

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base64 encodeBase64URLSafeString.

Prototype

public static String encodeBase64URLSafeString(final byte[] binaryData) 

Source Link

Document

Encodes binary data using a URL-safe variation of the base64 algorithm but does not chunk the output.

Usage

From source file:com.zotoh.core.crypto.JavaOfuscator.java

private String encrypt(String clearText) throws Exception {
    if (isEmpty(clearText)) {
        return clearText;
    }//  ww  w  . j  a  v  a 2 s  .  com
    Cipher c = getCipher(Cipher.ENCRYPT_MODE);
    ByteOStream baos = new ByteOStream();
    byte[] p = asBytes(clearText);
    byte[] out = new byte[Math.max(4096, c.getOutputSize(p.length))];
    int n = c.update(p, 0, p.length, out, 0);
    if (n > 0) {
        baos.write(out, 0, n);
    }
    n = c.doFinal(out, 0);
    if (n > 0) {
        baos.write(out, 0, n);
    }

    return Base64.encodeBase64URLSafeString(baos.asBytes());
}

From source file:de.kbs.acavis.service.SerializationHelper.java

public static String serializeAuthorIdentifierBase64(AuthorIdentifier identifier) throws IOException {
    return Base64.encodeBase64URLSafeString(serializeAuthorIdentifier(identifier).getBytes("UTF-8"));
}

From source file:com.ro.ssc.app.client.licensing.TrialKeyGenerator.java

public static String generateKey(String toEncode) {

    String encoded = "";
    try {/*  w w w.  j  a v  a  2s  .c  o m*/
        byte[] saltEncrypt = SALT_ENCRYPT.getBytes();
        SecretKeyFactory factoryKeyEncrypt = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY);
        SecretKey tmp = factoryKeyEncrypt.generateSecret(
                new PBEKeySpec(PASS_ENCRYPT.toCharArray(), saltEncrypt, ITERATIONS_ENCRYPT, KEY_LENGTH));
        SecretKeySpec encryptKey = new SecretKeySpec(tmp.getEncoded(), ALGORITHM);
        Cipher aesCipherEncrypt = Cipher.getInstance(CIPHER);
        aesCipherEncrypt.init(Cipher.ENCRYPT_MODE, encryptKey);
        byte[] bytes = StringUtils.getBytesUtf8(toEncode);
        byte[] encryptBytes = aesCipherEncrypt.doFinal(bytes);
        encoded = Base64.encodeBase64URLSafeString(encryptBytes);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encoded;
}

From source file:com.google.u2f.gaedemo.servlets.FinishSignServlet.java

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

    SignSessionData sessionData = dataStore.getSignSessionData(req.getParameter("sessionId"));

    // Simple XSRF protection. We don't want users to be tricked into
    // submitting other people's enrollment data. Here we're just checking 
    // that it's the same user that also started the enrollment - you might
    // want to do something more sophisticated.
    String currentUser = userService.getCurrentUser().getUserId();
    String expectedUser = sessionData.getAccountName();
    if (!currentUser.equals(expectedUser)) {
        throw new ServletException("Cross-site request prohibited");
    }// ww  w . j  a  v  a  2s .co  m

    SignResponse signResponse = new SignResponse(req.getParameter("clientData"),
            req.getParameter("signatureData"), Base64.encodeBase64URLSafeString(sessionData.getChallenge()),
            req.getParameter("sessionId"), sessionData.getAppId());

    SecurityKeyData securityKeyData;
    try {
        securityKeyData = u2fServer.processSignResponse(signResponse);
    } catch (U2FException e) {
        throw new ServletException("signature didn't verify", e);
    }

    resp.setContentType("application/json");
    resp.getWriter().println(new TokenStorageData(securityKeyData).toJson().toString());
}

From source file:com.cloud.consoleproxy.ConsoleProxyPasswordBasedEncryptor.java

public String encryptText(String text) {
    if (text == null || text.isEmpty())
        return text;

    assert (password != null);
    assert (!password.isEmpty());

    try {//from  w w w  .java2 s.  c o  m
        Cipher cipher = Cipher.getInstance("DES");
        int maxKeySize = 8;
        SecretKeySpec keySpec = new SecretKeySpec(normalizeKey(password.getBytes(), maxKeySize), "DES");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        byte[] encryptedBytes = cipher.doFinal(text.getBytes());
        return Base64.encodeBase64URLSafeString(encryptedBytes);
    } catch (NoSuchAlgorithmException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (NoSuchPaddingException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (IllegalBlockSizeException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (BadPaddingException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    } catch (InvalidKeyException e) {
        s_logger.error("Unexpected exception ", e);
        return null;
    }
}

From source file:com.parleys.server.frontend.web.ipad.filters.AESEncrypter.java

public String encrypt(final String str) {
    try {// ww w .j  ava2  s.co m
        // Encode the string into bytes using utf-8
        final byte[] utf8 = str.getBytes(ENCODING);

        // Encrypt
        final byte[] enc = ecipher.doFinal(utf8);

        // Encode bytes to base64 to get a string
        return Base64.encodeBase64URLSafeString(enc);
    } catch (Exception e) {
        LOGGER.fatal(e);
        throw new RuntimeException("Error encrypting string");
    }
}

From source file:fr.ortolang.diffusion.security.authentication.TicketHelper.java

public static String makeTicket(String username, String hash, long ticketValidity) {
    Cipher cipher;//from  ww w  . jav a2 s .c o m
    try {
        cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        Ticket ticket = new Ticket(username, hash, ticketValidity);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(ticket);
        MessageDigest md = MessageDigest.getInstance(MESSAGE_DIGEST_ALGORITHM);
        byte[] digest = md.digest(bos.toByteArray());
        bos.write(digest);
        byte[] encryptedBytes = cipher.doFinal(bos.toByteArray());
        return Base64.encodeBase64URLSafeString(encryptedBytes);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException
            | BadPaddingException | IllegalBlockSizeException e) {
        LOGGER.log(Level.SEVERE, "Error when making a ticket", e);
    }
    return "";
}

From source file:com.thinkbiganalytics.policy.standardization.Base64Encode.java

@Override
public Object convertRawValue(Object value) {
    try {//from w w w.  ja  v a 2s. c om
        byte[] val = null;
        if (value instanceof byte[]) {
            val = (byte[]) value;
        } else {
            val = ((String) value).getBytes("UTF-8");
        }
        if (Base64Output.BINARY.equals(base64Output)) {
            byte[] encoded = Base64.encodeBase64(val);
            return encoded;
        } else if (Base64Output.STRING.equals(base64Output)) {
            return Base64.encodeBase64URLSafeString(val);
        }

    } catch (UnsupportedEncodingException e) {
        return null;
    }
    return null;
}

From source file:com.cilogi.lid.util.TEA.java

public String encrypt(@NonNull String clear) {
    byte[] clearBytes = clear.getBytes(Charsets.UTF_8);
    return Base64.encodeBase64URLSafeString(encrypt(clearBytes));
}

From source file:eu.openanalytics.rsb.component.DataDirectoriesResource.java

@PostConstruct
public void setupRootMap() throws IOException {
    final List<File> dataDirectoryRoots = getConfiguration().getDataDirectories();
    if (dataDirectoryRoots != null) {
        for (final File dataDirectoryRoot : dataDirectoryRoots) {
            final String rootKey = Base64.encodeBase64URLSafeString(
                    DigestUtils.md5Digest(dataDirectoryRoot.getCanonicalPath().getBytes()));
            rootMap.put(rootKey, dataDirectoryRoot);
        }/*  www  .jav a 2s.  c  o  m*/
    }
}