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);
}
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);
}
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);
}
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;
}
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;
}
Aggregations