俊星的BLOG

          JAVA MAIL之BASE64編碼解碼

          關于BASE64編碼,建議參看WIKI中的相關說明
          1、編碼:

              public static byte[] base64encode(byte[] inbuf) {
                  
          int size = inbuf.length;
                  
          byte[] outbuf = new byte[((inbuf.length + 2/ 3* 4];
                  
          int inpos, outpos;
                  
          int val;
                  
          // 情況1:大于等于3
                  for (inpos = 0, outpos = 0; size >= 3; size -= 3, outpos += 4{
                      val 
          = inbuf[inpos++& 0xff;
                      val 
          <<= 8;
                      val 
          |= inbuf[inpos++& 0xff;
                      val 
          <<= 8;
                      val 
          |= inbuf[inpos++& 0xff;
                      
          // 到此val中存儲了3*8=24個二進制位,然后分4次,每次6個二進制位輸出
                      outbuf[outpos + 3= (byte) pem_array[val & 0x3f];
                      val 
          >>= 6;
                      outbuf[outpos 
          + 2= (byte) pem_array[val & 0x3f];
                      val 
          >>= 6;
                      outbuf[outpos 
          + 1= (byte) pem_array[val & 0x3f];
                      val 
          >>= 6;
                      outbuf[outpos 
          + 0= (byte) pem_array[val & 0x3f];
                  }

                  
          // 情況2:等于1或者等于2
                  if (size == 1{
                      val 
          = inbuf[inpos++& 0xff;
                      val 
          <<= 4;
                      
          // 到此val中實際有效二進制位8+4=12個,并且后4個都為0
                      outbuf[outpos + 3= (byte'='// pad character;
                      outbuf[outpos + 2= (byte'='// pad character;
                      outbuf[outpos + 1= (byte) pem_array[val & 0x3f];
                      val 
          >>= 6;
                      outbuf[outpos 
          + 0= (byte) pem_array[val & 0x3f];
                  }
           else if (size == 2{
                      val 
          = inbuf[inpos++& 0xff;
                      val 
          <<= 8;
                      val 
          |= inbuf[inpos++& 0xff;
                      val 
          <<= 2;
                      
          // 得到此val中實際有效二進制位為8+8+2=18個,并且后2個為0
                      outbuf[outpos + 3= (byte'='// pad character;
                      outbuf[outpos + 2= (byte) pem_array[val & 0x3f];
                      val 
          >>= 6;
                      outbuf[outpos 
          + 1= (byte) pem_array[val & 0x3f];
                      val 
          >>= 6;
                      outbuf[outpos 
          + 0= (byte) pem_array[val & 0x3f];
                  }

                  
          return outbuf;
              }


              
          private final static char pem_array[] = 
                      
          'A''B''C''D''E''F''G''H'// 0
                      'I''J''K''L''M''N''O''P'// 1
                      'Q''R''S''T''U''V''W''X'// 2
                      'Y''Z''a''b''c''d''e''f'// 3
                      'g''h''i''j''k''l''m''n'// 4
                      'o''p''q''r''s''t''u''v'// 5
                      'w''x''y''z''0''1''2''3'// 6
                      '4''5''6''7''8''9''+''/' // 7
              }
          ;

          測試:
              public static void main(String[] args) {
                  String [] strs 
          = {"leasure.","easure.","asure.","sure."};
                  
          for(String str:strs){
                      System.out.println(
          new String(base64encode(str.getBytes())));
                  }

              }

          輸出:
          bGVhc3VyZS4=
          ZWFzdXJlLg
          ==
          YXN1cmUu
          c3VyZS4
          =

          2、反編碼:
              public static byte[] base64decode(byte[] inbuf) {
                  
          int size = (inbuf.length / 4* 3;
                  
          if (inbuf[inbuf.length - 1== '='{
                      size
          --;
                      
          if (inbuf[inbuf.length - 2== '=')
                          size
          --;
                  }

                  
          byte[] outbuf = new byte[size];

                  
          int inpos = 0, outpos = 0;
                  size 
          = inbuf.length;
                  
          while (size > 0{
                      
          int val;
                      
          int osize = 3;
                      val 
          = pem_convert_array[inbuf[inpos++& 0xff];
                      val 
          <<= 6;
                      val 
          |= pem_convert_array[inbuf[inpos++& 0xff];
                      val 
          <<= 6;
                      
          if (inbuf[inpos] != '='// End of this BASE64 encoding
                          val |= pem_convert_array[inbuf[inpos++& 0xff];
                      
          else
                          osize
          --;
                      val 
          <<= 6;
                      
          if (inbuf[inpos] != '='// End of this BASE64 encoding
                          val |= pem_convert_array[inbuf[inpos++& 0xff];
                      
          else
                          osize
          --;
                      
          if (osize > 2)
                          outbuf[outpos 
          + 2= (byte) (val & 0xff);
                      val 
          >>= 8;
                      
          if (osize > 1)
                          outbuf[outpos 
          + 1= (byte) (val & 0xff);
                      val 
          >>= 8;
                      outbuf[outpos] 
          = (byte) (val & 0xff);
                      outpos 
          += osize;
                      size 
          -= 4;
                  }

                  
          return outbuf;
              }


              
          private final static char pem_array[] = 
                      
          'A''B''C''D''E''F''G''H'// 0
                      'I''J''K''L''M''N''O''P'// 1
                      'Q''R''S''T''U''V''W''X'// 2
                      'Y''Z''a''b''c''d''e''f'// 3
                      'g''h''i''j''k''l''m''n'// 4
                      'o''p''q''r''s''t''u''v'// 5
                      'w''x''y''z''0''1''2''3'// 6
                      '4''5''6''7''8''9''+''/' // 7
              }
          ;

              
          private final static byte pem_convert_array[] = new byte[256];

              
          static {
                  
          // 將數組中的每個元素的所有二進制位均初始化為1
                  for (int i = 0; i < 255; i++)
                      pem_convert_array[i] 
          = -1;
                  for (int i = 0; i < pem_array.length; i++)
                      pem_convert_array[pem_array[i]] 
          = (byte) i;
              }

          測試:
              public static void main(String[] args) {
                  String strs[] 
          = "bGVhc3VyZS4=""ZWFzdXJlLg==""YXN1cmUu""c3VyZS4=" };
                  
          for(String str:strs){
                      System.out.println(
          new String(base64decode(str.getBytes())));
                  }

              }

          輸出:
          leasure.
          easure.
          asure.
          sure.

          posted on 2009-04-23 23:29 俊星 閱讀(774) 評論(0)  編輯  收藏 所屬分類: 代碼庫

          主站蜘蛛池模板: 从江县| 巴马| 广西| 苍南县| 东兰县| 布拖县| 神池县| 怀宁县| 乳山市| 青州市| 闵行区| 额尔古纳市| 西林县| 习水县| 新津县| 巨鹿县| 确山县| 霍州市| 乌兰县| 团风县| 河北区| 海南省| 确山县| 旬邑县| 湘潭市| 黔西县| 保定市| 抚州市| 合江县| 鄯善县| 永安市| 宜昌市| 揭西县| 天水市| 朝阳县| 东宁县| 舟山市| 渑池县| 廉江市| 桃源县| 泸水县|