Search in sources :

Example 91 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project nem2-sdk-java by nemtech.

the class Base64Encoder method getString.

/**
 * Converts a byte array to a Base64 string.
 *
 * @param bytes The input byte array.
 * @return The output Base64 string.
 */
public static String getString(final byte[] bytes) {
    final Base64 codec = new Base64();
    final byte[] decodedBytes = codec.encode(bytes);
    return StringEncoder.getString(decodedBytes);
}
Also used : Base64(org.apache.commons.codec.binary.Base64)

Example 92 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project dal by ctripcorp.

the class DalBase64 method encodeBase64.

public static byte[] encodeBase64(final byte[] binaryData, final boolean isChunked, final boolean urlSafe, final int maxResultSize) {
    if (binaryData == null || binaryData.length == 0) {
        return binaryData;
    }
    // Create this so can use the super-class method
    // Also ensures that the same roundings are performed by the ctor and the code
    final Base64 b64 = isChunked ? new DalBase64(urlSafe) : new DalBase64(0, CHUNK_SEPARATOR, urlSafe);
    final long len = b64.getEncodedLength(binaryData);
    if (len > maxResultSize) {
        throw new IllegalArgumentException("Input array too big, the output array would be bigger (" + len + ") than the specified maximum size of " + maxResultSize);
    }
    return b64.encode(binaryData);
}
Also used : Base64(org.apache.commons.codec.binary.Base64)

Example 93 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project tech by ffyyhh995511.

the class AESUtils method Encrypt.

/**
 * 字符串加密
 * @param sSrc
 * @param sKey
 * @return
 * @throws Exception
 */
public static String Encrypt(String sSrc, String sKey) throws Exception {
    if (sKey == null) {
        logger.error("Key为空null");
        return null;
    }
    // 判断Key是否为16位
    if (sKey.length() != 16) {
        logger.error("Key长度不是16位");
        return null;
    }
    byte[] raw = sKey.getBytes("utf-8");
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    // "算法/模式/补码方式"
    Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
    // 此处使用BASE64做转码功能,同时能起到2次加密的作用。
    return new Base64().encodeToString(encrypted);
}
Also used : Base64(org.apache.commons.codec.binary.Base64) SecretKeySpec(javax.crypto.spec.SecretKeySpec) Cipher(javax.crypto.Cipher)

Example 94 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project wso2-synapse by wso2.

the class Base64DecodeFunction method decode.

private Object decode(boolean debugOn, String charset, String value) throws FunctionCallException {
    if (value == null || value.isEmpty()) {
        if (debugOn) {
            log.debug("Non empty string value should be provided for decode");
        }
        return NULL_STRING;
    }
    byte[] decodedValue;
    try {
        decodedValue = new Base64().decode(value.getBytes(charset));
    } catch (UnsupportedEncodingException e) {
        String msg = "Unsupported Charset";
        log.error(msg, e);
        throw new FunctionCallException(msg, e);
    }
    String decodedString;
    try {
        decodedString = new String(decodedValue, charset).trim();
    } catch (UnsupportedEncodingException e) {
        String msg = "Unsupported Charset";
        log.error(msg, e);
        throw new FunctionCallException(msg, e);
    }
    if (debugOn) {
        log.debug("Decoded base64 encoded value: " + value + " with charset: " + charset + " to String: " + decodedString);
    }
    return decodedString;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) FunctionCallException(org.jaxen.FunctionCallException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 95 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project wso2-synapse by wso2.

the class Base64EncodeFunction method encode.

private Object encode(boolean debugOn, String encoding, String value) throws FunctionCallException {
    if (value == null || "".equals(value)) {
        if (debugOn) {
            log.debug("Non emprty string value should be provided for encoding");
        }
        return NULL_STRING;
    }
    byte[] encodedValue;
    try {
        encodedValue = new Base64().encode(value.getBytes(encoding));
    } catch (UnsupportedEncodingException e) {
        String msg = "Unsupported Encoding";
        log.error(msg, e);
        throw new FunctionCallException(msg, e);
    }
    String encodedString;
    try {
        encodedString = new String(encodedValue, encoding).trim();
        encodedString = encodedString.replace("\r\n", "");
    } catch (UnsupportedEncodingException e) {
        String msg = "Unsupported Encoding";
        log.error(msg, e);
        throw new FunctionCallException(msg, e);
    }
    if (debugOn) {
        log.debug("Converted string: " + value + " with encoding: " + encoding + " to base64 encoded value: " + encodedString);
    }
    return encodedString;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) FunctionCallException(org.jaxen.FunctionCallException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

Base64 (org.apache.commons.codec.binary.Base64)135 IOException (java.io.IOException)30 Test (org.junit.Test)29 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 InputStream (java.io.InputStream)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 HashMap (java.util.HashMap)10 SecretKeySpec (javax.crypto.spec.SecretKeySpec)9 MessageDigest (java.security.MessageDigest)8 File (java.io.File)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 URL (java.net.URL)7 Mac (javax.crypto.Mac)7 ServletException (javax.servlet.ServletException)7 X509Certificate (java.security.cert.X509Certificate)6 FileNotFoundException (java.io.FileNotFoundException)5 Signature (java.security.Signature)5