コード例 #1
0
ファイル: Base64.cpp プロジェクト: mycp/mycp
//  aLen 为 aSrc 的长度, aDest 所指的缓冲区必须至少为 aLen 的 1.33 倍!!!
//  返回 aDest 的长度
unsigned int Base64Encode( char * const aDest, const unsigned char * aSrc, unsigned int aLen )
{
    char        * p = aDest;
    unsigned int          i;
    unsigned char t;

    for ( i = 0; i < aLen; i++ )
    {
        switch ( i % 3 )
        {
        case 0 :
            *p++ = Base2Chr( *aSrc >> 2 );
            t = ( *aSrc++ << 4 ) & 0x3F;
            break;
        case 1 :
            *p++ = Base2Chr( t | ( *aSrc >> 4 ) );
            t = ( *aSrc++ << 2 ) & 0x3F;
            break;
        case 2 :
            *p++ = Base2Chr( t | ( *aSrc >> 6 ) );
            *p++ = Base2Chr( *aSrc++ );
            break;
        }
    }
    if ( aLen % 3 != 0 )
    {
        *p++ = Base2Chr( t );
        if ( aLen % 3 == 1 )
            *p++ = '=';
        *p++ = '=';
    }
    *p = 0;  //  aDest is an ASCIIZ string
    return (unsigned int)( p - aDest );  //  exclude the end of zero
}
コード例 #2
0
ファイル: YTSocket.cpp プロジェクト: hbi980/ytnew
// base64加密
int CYTSocket::Base64Encode(const char * inbuf, char * outbuf)
{
	unsigned int  x, y, z;
    unsigned int  i, j;
    unsigned char buf[3];

    x = strlen(inbuf)/3;
    y = strlen(inbuf)%3;
    i = 0;
    j = 0;
    for(z=0; z<x; z++)
    {
        outbuf[i]   = Base2Chr(inbuf[j]>>2);
        outbuf[i+1] = Base2Chr((inbuf[j] & 0x03)<<4 | inbuf[j+1]>>4);
        outbuf[i+2] = Base2Chr((inbuf[j+1] & 0x0F)<<2 | inbuf[j+2]>>6);
        outbuf[i+3] = Base2Chr(inbuf[j+2] & 0x3F);
        i += 4;
        j += 3;
    }
    if(y != 0)
    {
        buf[0] = 0x00;
        buf[1] = 0x00;
        buf[2] = 0x00;
        for(z=0; z<y; z++)
        {
            buf[z] = inbuf[j+z];
        }
        outbuf[i]   = Base2Chr(buf[0]>>2);
        outbuf[i+1] = Base2Chr((buf[0] & 0x03)<<4 | buf[1]>>4);
        outbuf[i+2] = Base2Chr((buf[1] & 0x0F)<<2 | buf[2]>>6);
        outbuf[i+3] = Base2Chr(buf[2] & 0x3F);
        i += 4;
        for(z=0; z<3-y; z++)
        {
            outbuf[i-z-1] = '=';
        }
    }