Base64
Base64是一种基于64个字符的编码算法,根据RFC2045的定义:“Base64内容传送编码是一种以任意8位字节序列组合的描述形式,
这种形式不易被人直接识别”,RFC2045还规定,在电子邮件中,每行为76个字符,每行末需添加一个回车换行符("\r\n")
经过Base64编码后的数据会比原始数据长,为原来的4/3倍,编码后的字符串的字符数是4的整数倍。
Base64算法很好的解决了非ASC码字符的传输问题,比如中文字符的传输问题。
Java包中有Base64 的分标准实现 不建议使用,一般建议使用Apache的Commons Codec
注:sun.misc是sun内部专用的包,在编译时经常会报错。
base64 java实现
- package com.zzq.base64;
- import org.bouncycastle.util.encoders.Base64;
- import org.apache.commons.codec.binary.*;
- public abstract class Base64Coder {
- public final static String ENCODING = "UTF-8" ;
- //Bouncy castle实现base64编码
- public static String BCencode(String data) throws Exception {
- byte [] b = org.bouncycastle.util.encoders.Base64.encode(data.getBytes(ENCODING));
- return new String(b,ENCODING);
- }
- /*
- * Bouncy castle实现base64解码
- */
- public static String BCdecode(String data) throws Exception {
- byte [] b = org.bouncycastle.util.encoders.Base64.decode(data.getBytes(ENCODING));
- return new String(b,ENCODING);
- }
- //commons Codec 实现base64编码
- public static String CCencode(String data) throws Exception {
- byte [] b = org.apache.commons.codec.binary.Base64.encodeBase64(data.getBytes(ENCODING));
- return new String(b,ENCODING);
- }
- //commons Codec 实现base64编码,按RFC2045标准执行 (加密后每行有换行符)
- public static String CCencodeSafe(String data) throws Exception {
- byte [] b = org.apache.commons.codec.binary.Base64.encodeBase64(data.getBytes(ENCODING), true );
- return new String(b,ENCODING);
- }
- //Bouncy castle实现base64解码
- public static String CCdecode(String data) throws Exception {
- byte [] b = org.apache.commons.codec.binary.Base64.decodeBase64(data.getBytes(ENCODING));
- return new String(b,ENCODING);
- }
- }
- /**
- *
- */
- package com.zzq.base64;
- import org.junit.After;
- import org.junit.Test;
- import static org.junit.Assert.*;
- import sun.misc.*;
- /**
- * @author zhiqiang
- *
- */
- public class TestBase64 {
- @Test
- public void test() throws Exception {
- String inputStr = "Base64测试" ;
- System.out.println("原文:" +inputStr);
- //bc编码解码
- String code = Base64Coder.BCencode(inputStr);
- System.err.println("Base64后:" +code);
- String outputStr = Base64Coder.BCdecode(code);
- System.err.println("解码后:" +outputStr);
- assertEquals(inputStr,outputStr);
- //cc编码解码
- String code1 = Base64Coder.CCencode(inputStr);
- System.err.println("Base64后:" +code1);
- String outputStr1 = Base64Coder.CCdecode(code1);
- System.err.println("解码后:" +outputStr1);
- assertEquals(inputStr,outputStr1);
- //按RFC2045标准执行 cc编码解码
- String code2 = Base64Coder.CCencodeSafe(inputStr);
- System.err.println("Base64后:" +code2);
- String outputStr2 = Base64Coder.CCdecode(code2);
- System.err.println("解码后:" +outputStr2);
- assertEquals(inputStr,outputStr2);
- String outputStr3 = Base64Coder.BCdecode(code2);
- System.err.println("解码后:" +outputStr3);
- assertEquals(inputStr,outputStr3);
- // java包自带base64编码解码 默认RFC2045标准执行 不建议使用
- // BASE64Encoder encoder = new BASE64Encoder();
- // String code3 = encoder.encodeBuffer(inputStr.getBytes());
- // System.err.println("Base64后:"+code3);
- //
- // BASE64Decoder decoder = new BASE64Decoder();
- // byte[] output = decoder.decodeBuffer(code3);
- // System.err.println("解码后:"+new String(output));
- }
- }
base64 erlang 实现
- 10 > base64:encode_to_string( "Base64测试" ).
- "QmFzZTY0suLK1A=="
- 11 > base64:decode_to_string( "QmFzZTY0suL K1A==" ).
- "Base64测试"
- 12 > base64:decode_to_string( "QmFzZTY0suL %^ K1A==" ).
- ** exception error: no function clause matching base64:b64d_ok(bad) (base64
- line 363 )
- in function base64:decode/2 (base64.erl, line 264 )
- 13 > base64:mime_decode_to_string( "QmFzZTY0suL K1A==" ).
- "Base64测试"
- 14 > base64:mime_decode_to_string( "QmFzZTY0suL %^ K1A==" ).
- "Base64测试"
Decodes a base64 encoded string to plain ASCII. See RFC4648.
mime_decode/1 and mime_decode_to_string/1 strips away illegal
characters, while decode/1 and decode_to_string/1 only strips away
whitespace characters
如代码所示,mime_decode/1 and mime_decode_to_string/1方法会忽略64个字符以外的其他字符 而其他方法只能忽略空字符。
java的实现也只能过滤空字符
注:上述erlang代码在werl里测试的时候会报错,可能是中文编码问题,在控制台里测试正常
转自 http://liangjianss.iteye.com/blog/1504086