我去取一段邮件的附件信息,可是就是不能正确地显示出来他的真面目,希望各位高手能够帮帮忙,万分感谢!!!我用的是VC6.0,附件内容是用base64编码的,我也用解码的函数解了,可是解出来是一堆乱码,是不是还要在转换成像utf-8什么的?
如果是,该怎么转?如果不是,又该怎么办?希望各位高手能够帮帮我,这个问题困扰好几天了我的解码程序:
const BYTE base64_alphabet[]= 
{
255,255,255,255,  255,255,255,255,  255,255,255,255,  255,255,255,255,
255,255,255,255,  255,255,255,255,  255,255,255,255,  255,255,255,255,
255,255,255,255,  255,255,255,255,  255,255,255,
62,                                 //   '/'
255,255,255,
63,                                 //   '='
52,53,54,55,56,57,58,59,60,61,      //0-9
255,255,255,255,255,255,255,
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14, //A-Z
15,16,17,18,19,20,21,22,23,24,25,
255,255,255,255,255,255,
26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,//a-z
41,42,43,44,45,46,47,48,49,50,51,
255,255,255,255,255
}; char * CEmailAtt::Base64Decode(char *base64code)
{
//TODO:Base64Decode
char *input=base64code;
int len=strlen(input);
int i=0;
int icnt=0;
char *output=new char[len]; char *p=output;
for(i=0;i<len;i++)
{
if(input[i]>127)continue;
if(input[i]=='=')continue;
char ch=input[i]; BYTE a =base64_alphabet[ch];
if(a==255)continue; switch(icnt)
{
case 0:
{
*p=a<<2;
icnt++;
}
break;
case 1:
{
*p++ |=a>>4;
*p=a<<4;
icnt++;
}
break;
case 2:
{
*p++ |=a>>2;
*p=a<<6;
icnt ++;
}
break;
case 3:
{
*p++ |= a;
icnt=0;
}

}
}
*p=NULL;
p=output;
return p;
}

解决方案 »

  1.   

    你应该去确认下,是不是只有base64,是否还有其他加密
    base64 解出来的是二进制,和什么utf是无关的
    发个我的base64 解码你试下
    bool Base64Coding::Decode(const char* source,byte** dest,unsigned int& aLen)
    {
    bool ret=true; int codes[256];
    memset(codes,-1,256);
    {   
    for(int i = 'A';i<='Z';i++)   
    codes[i]=(byte)(i - 'A');   
    for(int i = 'a';i <= 'z';i++)   
    codes[i]=(byte)(26+i-'a');   
    for(int i = '0';i <= '9';i++)   
    codes[i]=(byte)(52+i-'0');   
    codes['+']=62;   
    codes['/']=63;   
    }   
      
    unsigned int srcLen=(unsigned int)strlen(source);
        unsigned int   len   =   ((srcLen  +   3)   /   4)   *   3;   
        if   (srcLen>0   &&   source[srcLen-1]   ==   '=')   
    --len;   
        if   (srcLen>1   &&   source[srcLen-2]   ==   '=')   
    --len;   
        byte*   out   =   new   byte[len];       int   shift   =   0;       //   #   of   excess   bits   stored   in   accum   
        int   accum   =   0;       //   excess   bits   
        unsigned int   index   =   0;       for   (unsigned int   ix=0;   ix<srcLen;   ix++)   
        {   
            int   value   =   codes[   source[ix]   &   0xFF   ];       //   ignore   high   byte   of   char   
            if   (   value   >=   0   )                                             //   skip   over   non-code   

    accum   <<=   6;                         //   bits   shift   up   by   6   each   time   thru   
    shift   +=   6;                           //   loop,   with   new   bits   being   put   in   
    accum   |=   value;                   //   at   the   bottom.   
    if   (   shift   >=   8   )              //   whenever   there   are   8   or   more   shifted   in,   
    {
    shift   -=   8;                   //   write   them   out   (from   the   top,   leaving   any   
    if(index<len)
    out[index++]   =  (byte)   ((accum   >>   shift)   &   0xff);   //   excess   at   the   bottom   for   next   iteration.   
    else
    {
    ret=false;
    break;
    }

    }
    }
        }       
    if(ret && index==len)
    {
    *dest=out;
    aLen=len;
    }
    else
    {
    ret=false;
    delete [] out;
    } return ret;
    }
      

  2.   

    我是使用的CString 对象,因为邮件中的附件每行限制最多76个字符,所以又使用了CStdioFile对象,调用ReadString()每次读取一行,然后将这一行作为字符串传给Base64Decode进行解码的另外邮件的分隔符已经找到,现在读的已经是附件部分了我再试一下skywoodsky的解码程序
      

  3.   

    bool Base64Coding::Decode(const char* source,byte** dest,unsigned int& aLen)
    {}
    byte** dest表示解码后放的位置串?
    unsigned int& aLen表示每个串的长度?为什么用个&呢?
      

  4.   

    Windows自己有CAPICOM支持Base64解码,为何还要自己写?
    http://msdn.microsoft.com/en-us/library/aa388177(VS.85).aspx
    不过CAPICOM的SDK好像在Windows Platform SDK(现在是Windows SDK)里。
      

  5.   

    base64解码输入都是字符串,输出为二进制,二进制表示什么内容,是文字还是其他就需要照协议来解释了。
      

  6.   

    这是我以前用void DeCode(char *src, int src_len, char *dst){
    int i = 0, j = 0;
    for (; i < src_len; i += 4) {                dst[j++] = DecodeTable[src[i]] << 2 |
       DecodeTable[src[i + 1]] >> 4;                dst[j++] = DecodeTable[src[i + 1]] << 4 |
       DecodeTable[src[i + 2]] >> 2;                dst[j++] = DecodeTable[src[i + 2]] << 6 |
       DecodeTable[src[i + 3]];
            }
        dst[j] = '\0';
    }
      

  7.   


    const char m_staticBase64CodeTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; bool Base64Encode( const BYTE* szSrcData, int nSrcLen, BYTE* szDestData, int* pnDestLen ){

    if( !( szSrcData && nSrcLen && szDestData && pnDestLen && *pnDestLen ) ) 

            return false; 


    if( *pnDestLen < Base64EncodeLen( nSrcLen ) ) 

    return false; 


    int nWritten = 0 ; 
    int nLen1 = ( nSrcLen / 3 ) * 4; 
    int nLen2 = nLen1 / 76; 
    int nLen3 = 19; 

    for( int i = 0; i <= nLen2; i++ )

    if( i == nLen2 ) 

    nLen3 = ( nLen1 % 76 ) / 4; 

    for( int j = 0; j < nLen3; j++ ) 

    DWORD dwCurr = 0; 
    for( int n = 0; n < 3; n++ ) 

    dwCurr |= *szSrcData++; 
    dwCurr <<= 8; 

    for( int k = 0; k < 4; k++ ) 

    BYTE b = ( BYTE )( dwCurr >> 26 ); 
    *szDestData++ = m_staticBase64CodeTable[ b ]; 
    dwCurr <<= 6; 


    nWritten += nLen3 * 4; 

    *szDestData++ = '\r'; 
    *szDestData++ = '\n'; 
    nWritten+= 2; 


    if( nWritten ) 

    szDestData-= 2; 
    nWritten -= 2; 


    nLen2 = ( nSrcLen % 3 ) ? ( nSrcLen % 3 ) + 1 : 0; 
    if( nLen2 ) 

    DWORD dwCurr = 0; 
    for( int n = 0; n < 3; n++ ) 

    if( n < ( nSrcLen % 3 ) ) 

    dwCurr |= *szSrcData++; 

    dwCurr <<= 8; 

    for( int k = 0; k < nLen2; k++ ) 

    BYTE b = ( BYTE ) ( dwCurr >> 26 ); 
    *szDestData++ = m_staticBase64CodeTable[ b ]; 
    dwCurr <<= 6; 

    nWritten += nLen2; 

    nLen3 = nLen2 ? 4 - nLen2 : 0; 
    for( int j = 0; j < nLen3; j++ ) 

    *szDestData++ = '='; 

    nWritten += nLen3; 

    *pnDestLen = nWritten; 
    //szDestData[*pnDestLen + 1] = '\0';
    return true; 
    }
    int Base64EncodeLen( int nSrcLen ){
    int nRet = nSrcLen * 4 / 3 + nSrcLen % 3; 
        int nCRLFs = ( nRet / 76 + 1 ) * 2;                  // 每行多加"\r\n" 
    int nOnLastLine = nRet % 76; 
    nRet += nCRLFs; 
    if( nOnLastLine && nOnLastLine % 4 ) 

    nRet += 4 - ( nOnLastLine % 4 ); 

    return nRet; 
      

  8.   

    base64是将3个字节编码成可视的4个字节,你先用几个英文字母测试下, 长度是否对
    邮件还有头信息的, 或许你获取到的是压缩或加密过的信息