<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    俊星的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 俊星 閱讀(770) 評論(0)  編輯  收藏 所屬分類: 代碼庫

    主站蜘蛛池模板: 日韩视频在线免费观看| 国产精品亚洲mnbav网站| 亚洲AV中文无码字幕色三| 黄色网页在线免费观看| 免费在线中文日本| 国精无码欧精品亚洲一区| 在线观看免费无码视频| 亚洲妇熟XXXX妇色黄| 久久国产精品成人免费| 亚洲一区二区三区四区在线观看 | 亚洲国产成人久久| 成人免费大片免费观看网站| 亚洲av永久无码精品天堂久久| 久久久久久国产精品免费免费| 亚洲综合av一区二区三区| 国产精品免费播放| 久久精品国产亚洲AV网站| 一级毛片不卡片免费观看| 亚洲国产精品日韩在线观看| 福利免费观看午夜体检区| 亚洲精品成a人在线观看☆| 亚洲精品无码久久久久AV麻豆| 久久久久久久国产免费看| 亚洲av日韩av高潮潮喷无码| 91免费播放人人爽人人快乐| 亚洲国产aⅴ成人精品无吗| 四虎亚洲国产成人久久精品 | 亚洲人成网网址在线看| 国产嫩草影院精品免费网址| 一级免费黄色大片| 国产成人免费a在线视频app| 日韩免费码中文在线观看| 亚洲AV福利天堂一区二区三| 成年女人午夜毛片免费看| 色爽黄1000部免费软件下载| 亚洲国产成人一区二区三区| 国产成人A在线观看视频免费| 男人和女人高潮免费网站| 精品亚洲成a人片在线观看| 韩国日本好看电影免费看| 免费国产成人午夜在线观看|