Base64编码/解码(源代码)

Refer to: http://en.wikipedia.org/wiki/Base64

Base64 Content-Transfer-Encoding ( RFC2045 )  可对任何文件进行base64 编解码,主要用于MIME邮件内容编解码

// 11111100 0xFC // 11000000 0x3 // 11110000 0xF0 // 00001111 0xF // 11000000 0xC0 // 00111111 0x3F

byte *lmMimeEncodeBase64(const byte *octetSource, int size) {     byte *m_Base64_Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    int base64size =  (((((size) + 2) / 3) * 4) + 1) / 76 * 78 + 78; //add "/r/n" for each line'

    byte *strEncode = (byte *)MALLOC(base64size);     byte cTemp[4];

    //By RFC2045 --The encoded output stream must be represented in lines of no more than 76 characters each     int LineLength=0;     int i, len, j=0;

    MEMSET(strEncode, 0,  base64size);

    for(i=0; i<size; i+=3)     {         MEMSET(cTemp,0,4);

        //cTemp[0]=octetSource[i];         //cTemp[1]=octetSource[i+1];         //cTemp[2]=octetSource[i+2];

        //len=strlen((char *)cTemp);

        if(i<size-3){             len = 3;             cTemp[0]=octetSource[i];             cTemp[1]=octetSource[i+1];             cTemp[2]=octetSource[i+2];         }         else{             len = 0;             if(i<size){                 cTemp[0]=octetSource[i];                 ++len;             }             if(i<size-1){                 cTemp[1]=octetSource[i+1];                 ++len;             }             if(i<size-2){                 cTemp[2]=octetSource[i+2];                 ++len;             }             //DBGPRINTF("temp[0] = %d", cTemp[0]);             //DBGPRINTF("temp[1] = %d", cTemp[1]);             //DBGPRINTF("temp[2] = %d", cTemp[2]);             //DBGPRINTF("strEncode[0] = %d", ((int)cTemp[0] & 0xFC)>>2);             //DBGPRINTF("strEncode[1] = %d", ((int)cTemp[0] & 0x3)<<4 | ((int)cTemp[1] & 0xF0)>>4);             //DBGPRINTF("strEncode[2] = %d", ((int)cTemp[1] & 0xF)<<2 | ((int)cTemp[2] & 0xC0)>>6);             //DBGPRINTF("strEncode[3] = %d", (int)cTemp[2] & 0x3F);             //DBGPRINTF("strEncode[0] = %c", m_Base64_Table[((int)cTemp[0] & 0xFC)>>2]);             //DBGPRINTF("strEncode[1] = %c", m_Base64_Table[((int)cTemp[0] & 0x3)<<4 | ((int)cTemp[1] & 0xF0)>>4]);             //DBGPRINTF("strEncode[2] = %c", m_Base64_Table[((int)cTemp[1] & 0xF)<<2 | ((int)cTemp[2] & 0xC0)>>6]);             //DBGPRINTF("strEncode[3] = %c", m_Base64_Table[(int)cTemp[2] & 0x3F]);         }

        if(len==3)         {             strEncode[j++] = m_Base64_Table[((int)cTemp[0] & 0xFC)>>2];             strEncode[j++] = m_Base64_Table[((int)cTemp[0] & 0x3)<<4 | ((int)cTemp[1] & 0xF0)>>4];             strEncode[j++] = m_Base64_Table[((int)cTemp[1] & 0xF)<<2 | ((int)cTemp[2] & 0xC0)>>6];             strEncode[j++] = m_Base64_Table[(int)cTemp[2] & 0x3F];             LineLength+=4;             if(LineLength>=76) {strEncode[j++]='/r'; strEncode[j++]='/n'; LineLength=0;}         }         else if(len==2)         {             strEncode[j++] = m_Base64_Table[((int)cTemp[0] & 0xFC)>>2];             strEncode[j++] = m_Base64_Table[((int)cTemp[0] & 0x3 )<<4 | ((int)cTemp[1] & 0xF0 )>>4];             strEncode[j++] = m_Base64_Table[((int)cTemp[1] & 0x0F)<<2];             strEncode[j++] = '=';             LineLength+=4;             if(LineLength>=76) {strEncode[j++]='/r'; strEncode[j++]='/n'; LineLength=0;}         }         else if(len==1)         {             strEncode[j++] = m_Base64_Table[((int)cTemp[0] & 0xFC)>>2];             strEncode[j++] = m_Base64_Table[((int)cTemp[0] & 0x3 )<<4];             strEncode[j++] = '=';             strEncode[j++] = '=';             LineLength+=4;             if(LineLength>=76) {strEncode[j++]='/r'; strEncode[j++]='/n'; LineLength=0;}         }         memset(cTemp,0,4);     }     //strEncode[j] = '/0';     //DBGPRINTF("--finished encode base64size = %d, j = %d", base64size, j);     //for(i=j; i<base64size; i++){     //    DBGPRINTF("--rest char is: %c", strEncode[i]);     //}     return strEncode; }

byte GetBase64Value(char ch) { if ((ch >= 'A') && (ch <= 'Z')) return ch - 'A'; if ((ch >= 'a') && (ch <= 'z')) return ch - 'a' + 26; if ((ch >= '0') && (ch <= '9')) return ch - '0' + 52; switch (ch) { case '+': return 62; case '/': return 63; case '=': /* base64 padding */ return 0; default: return 0; } }

byte *lmMimeDecodeBase64(const byte *strSource, int *psize) {     byte *m_Base64_Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";     int i, j, k, size = STRLEN((char *)strSource);     int n = 0;     //return value     byte *octetDecode = (byte *)MALLOC( (((size) - 1) / 4) *3  );     byte cTemp[5];     int Length=0;     int asc[4];     for(i=0;i<size;i+=4)     {         MEMSET(cTemp,0,5);

        cTemp[0]=strSource[i];         cTemp[1]=strSource[i+1];         cTemp[2]=strSource[i+2];         cTemp[3]=strSource[i+3];

        Length+=4;         if(Length==76)         {             i+=2;             Length=0;         }

        for(j=0;j<4;j++)         {             //if(cTemp[j]=='='){             //    asc[j]=0;             //}else{                 for(k=0;k<(int)STRLEN((char *)m_Base64_Table);k++)                 {                     if(cTemp[j]==m_Base64_Table[k]) asc[j]=k;                 }             //}         }         if('='==cTemp[2] && '='==cTemp[3])         {             octetDecode[n++] = (byte)(int)(asc[0] << 2 | asc[1] << 2 >> 6);         }         else if('='==cTemp[3])         {             octetDecode[n++] = (byte)(int)(asc[0] << 2 | asc[1] << 2 >> 6);             octetDecode[n++] = (byte)(int)(asc[1] << 4 | asc[2] << 2 >> 4);         }         else         {             octetDecode[n++] = (byte)(int)(asc[0] << 2 | asc[1] << 2 >> 6);             octetDecode[n++] = (byte)(int)(asc[1] << 4 | asc[2] << 2 >> 4);             octetDecode[n++] = (byte)(int)(asc[2] << 6 | asc[3] << 2 >> 2);         }

        //cTemp[0] = GetBase64Value((char)strSource[i]);         //cTemp[1] = GetBase64Value((char)strSource[i+1]);          //cTemp[2] = GetBase64Value((char)strSource[i+2]);          //cTemp[3] = GetBase64Value((char)strSource[i+3]);

        //Length+=4;         //if(Length==76)         //{         //    i+=2;         //    Length=0;         //}

        //octetDecode[n++] = (cTemp[0] << 2) | (cTemp[1] >> 4);         //octetDecode[n++] = (cTemp[1] << 4) | (cTemp[2] >> 2);         //octetDecode[n++] = (cTemp[2] << 6) | (cTemp[3]);

    }     *psize = n;     return octetDecode; }

原文地址:https://www.cnblogs.com/secbook/p/2655472.html