本文整理汇总了Java中org.apache.commons.codec.binary.Base64.encode方法的典型用法代码示例。如果您正苦于以下问题:Java Base64.encode方法的具体用法?Java Base64.encode怎么用?Java Base64.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.codec.binary.Base64
的用法示例。
在下文中一共展示了Base64.encode方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sign
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
/**
* @brief 签名
* @author [email protected]
* @date 2014-08-13 21:07:27
*
* @param signStr 被加密串
* @param secret 加密密钥
*
* @return
*/
public static String sign(String signStr, String secret)
throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException
{
String sig = null;
Mac mac = Mac.getInstance(HMAC_ALGORITHM);
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(CONTENT_CHARSET), mac.getAlgorithm());
mac.init(secretKey);
byte[] hash = mac.doFinal(signStr.getBytes(CONTENT_CHARSET));
// base64
//sig = new String(new BASE64Encoder().encode(hash).getBytes());
//sig = new String(Base64.encodeBase64(hash));
sig = new String(Base64.encode(hash));
return sig;
}
示例2: base64AndMD5
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
/**
* 先进行MD5摘要再进行Base64编码获取摘要字符串
*
* @return
*/
public static String base64AndMD5(byte[] bytes) {
if (bytes == null) {
throw new IllegalArgumentException("bytes can not be null");
}
try {
final MessageDigest md = MessageDigest.getInstance("MD5");
md.reset();
md.update(bytes);
final Base64 base64 = new Base64();
final byte[] enbytes = base64.encode(md.digest());
return new String(enbytes);
} catch (final NoSuchAlgorithmException e) {
throw new IllegalArgumentException("unknown algorithm MD5");
}
}
示例3: testEncode
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
@Test
public void testEncode(){
String message = "abcd";
byte[] b=message.getBytes();
Base64 base64=new Base64();
byte[] b2 = base64.encode(b);
String s=new String(b2);
System.out.println(s);
byte[] b3 = s.getBytes();
byte[] b4 = base64.decode(b3);
String s2=new String(b4);
System.out.println(s2);
Assert.assertTrue( message.equals(s2) );
}
示例4: encode
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
/**
* 使用Base64对数据进行加密
*
* @param plainText
* @return
*/
public static String encode(byte[] bytes) {
Base64 base64 = new Base64();
bytes = base64.encode(bytes);
String s = new String(bytes);
return s;
}
示例5: encodeMessage
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
protected static String encodeMessage(final String xmlString) throws IOException {
byte[] xmlBytes = xmlString.getBytes("UTF-8");
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(
byteOutputStream);
deflaterOutputStream.write(xmlBytes, 0, xmlBytes.length);
deflaterOutputStream.close();
// next, base64 encode it
Base64 base64Encoder = new Base64();
byte[] base64EncodedByteArray = base64Encoder.encode(byteOutputStream
.toByteArray());
return new String(base64EncodedByteArray);
}
示例6: encode64AsString
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
public static String encode64AsString(byte[] arrBytes)
{
Base64 base64 = new Base64();
byte[] tOut = base64.encode(arrBytes);
String cs = new String(tOut);
return cs;
}
示例7: encode64
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
public static byte[] encode64(byte[] arrBytes)
{
Base64 base64 = new Base64();
return base64.encode(arrBytes);
}
示例8: toBase64Str
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
public static String toBase64Str(byte[] srcByte) {
Base64 base64=new Base64();
byte[] b=base64.encode(srcByte);
String s=new String(b);
return s;
}
示例9: encode
import org.apache.commons.codec.binary.Base64; //导入方法依赖的package包/类
/**
* 编码
* @param content
* @param encoding
* @return
* @throws UnsupportedEncodingException
*/
public static String encode(String content,String encoding) throws UnsupportedEncodingException{
byte[] b = content.getBytes(encoding);
Base64 base64 = new Base64();
byte[] b2 = base64.encode(b);
return new String(b2,"UTF-8");
}