`
wuguowei1314
  • 浏览: 130365 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

erlang java 加解密(1)base64 (转载)

    博客分类:
  • java
阅读更多

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实现

Java代码  收藏代码
  1. package  com.zzq.base64;  
  2.   
  3. import  org.bouncycastle.util.encoders.Base64;  
  4. import  org.apache.commons.codec.binary.*;  
  5. public   abstract   class  Base64Coder {  
  6.     public   final   static  String ENCODING =  "UTF-8" ;  
  7.     //Bouncy castle实现base64编码   
  8.      public   static  String BCencode(String data)  throws  Exception {  
  9.          byte [] b = org.bouncycastle.util.encoders.Base64.encode(data.getBytes(ENCODING));  
  10.          return   new  String(b,ENCODING);  
  11.      }  
  12.  /*  
  13.   * Bouncy castle实现base64解码  
  14.   */   
  15.     public   static  String BCdecode(String data)  throws  Exception {  
  16.         byte [] b = org.bouncycastle.util.encoders.Base64.decode(data.getBytes(ENCODING));  
  17.         return   new  String(b,ENCODING);  
  18.     }  
  19.       
  20.     //commons Codec 实现base64编码   
  21.     public   static  String CCencode(String data)  throws  Exception {  
  22.          byte [] b = org.apache.commons.codec.binary.Base64.encodeBase64(data.getBytes(ENCODING));  
  23.          return   new  String(b,ENCODING);  
  24.      }  
  25.     //commons Codec 实现base64编码,按RFC2045标准执行 (加密后每行有换行符)   
  26.     public   static  String CCencodeSafe(String data)  throws  Exception {  
  27.          byte [] b = org.apache.commons.codec.binary.Base64.encodeBase64(data.getBytes(ENCODING), true );  
  28.          return   new  String(b,ENCODING);  
  29.      }  
  30.     //Bouncy castle实现base64解码   
  31.     public   static  String CCdecode(String data)  throws  Exception {  
  32.         byte [] b =  org.apache.commons.codec.binary.Base64.decodeBase64(data.getBytes(ENCODING));  
  33.         return   new  String(b,ENCODING);   
  34.     }  
  35.       
  36. }  


Java代码  收藏代码
  1. /**  
  2.  *   
  3.  */   
  4. package  com.zzq.base64;  
  5.   
  6.   
  7. import  org.junit.After;  
  8. import  org.junit.Test;  
  9. import   static  org.junit.Assert.*;  
  10. import  sun.misc.*;  
  11. /**  
  12.  * @author zhiqiang  
  13.  *  
  14.  */   
  15. public   class  TestBase64 {  
  16.   
  17.     @Test   
  18.     public   void  test()  throws  Exception {  
  19.         String inputStr = "Base64测试" ;  
  20.         System.out.println("原文:" +inputStr);  
  21.         //bc编码解码   
  22.         String code = Base64Coder.BCencode(inputStr);  
  23.         System.err.println("Base64后:" +code);  
  24.           
  25.         String outputStr = Base64Coder.BCdecode(code);  
  26.         System.err.println("解码后:" +outputStr);  
  27.         assertEquals(inputStr,outputStr);  
  28.           
  29.         //cc编码解码   
  30.         String code1 = Base64Coder.CCencode(inputStr);  
  31.         System.err.println("Base64后:" +code1);  
  32.           
  33.         String outputStr1 = Base64Coder.CCdecode(code1);  
  34.         System.err.println("解码后:" +outputStr1);  
  35.         assertEquals(inputStr,outputStr1);  
  36.           
  37.         //按RFC2045标准执行 cc编码解码   
  38.         String code2 = Base64Coder.CCencodeSafe(inputStr);  
  39.         System.err.println("Base64后:" +code2);  
  40.           
  41.         String outputStr2 = Base64Coder.CCdecode(code2);  
  42.         System.err.println("解码后:" +outputStr2);  
  43.         assertEquals(inputStr,outputStr2);  
  44.           
  45.         String outputStr3 = Base64Coder.BCdecode(code2);  
  46.         System.err.println("解码后:" +outputStr3);  
  47.         assertEquals(inputStr,outputStr3);  
  48.           
  49.   
  50.           
  51.         // java包自带base64编码解码 默认RFC2045标准执行 不建议使用   
  52. //      BASE64Encoder encoder = new BASE64Encoder();   
  53. //      String code3 = encoder.encodeBuffer(inputStr.getBytes());   
  54. //      System.err.println("Base64后:"+code3);   
  55. //         
  56. //      BASE64Decoder decoder = new BASE64Decoder();   
  57. //      byte[] output = decoder.decodeBuffer(code3);   
  58. //      System.err.println("解码后:"+new String(output));   
  59.     }  
  60.   
  61.       
  62. }  



base64 erlang 实现


Java代码  收藏代码
  1. 10 > base64:encode_to_string( "Base64测试" ).  
  2. "QmFzZTY0suLK1A=="   
  3. 11 > base64:decode_to_string( "QmFzZTY0suL  K1A==" ).  
  4. "Base64测试"   
  5. 12 > base64:decode_to_string( "QmFzZTY0suL %^ K1A==" ).  
  6. ** exception error: no function clause matching base64:b64d_ok(bad) (base64  
  7.  line 363 )  
  8.      in function  base64:decode/2  (base64.erl, line  264 )  
  9. 13 > base64:mime_decode_to_string( "QmFzZTY0suL  K1A==" ).  
  10. "Base64测试"   
  11. 14 > base64:mime_decode_to_string( "QmFzZTY0suL %^ K1A==" ).  
  12. "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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics