waterwater1
2015-11-09 12:51
采纳率: 100%
浏览 2.7k
已采纳

关于C语言的base64字符串解码

各位大神,我现在急需将一段字符串解码的方法(C语言实现),希望各位出手援助一下。
我需要解码的字符串是:
vU3xynE6ee6KymPB1cpwVGwZMrS/GZdXYBXiPNWfWktX6uq0gdIKB6yWG8dq49ywRKjCljpVipbTWyHIOEhS0jpSVuB/y4q3rWI3bvMGCWK9/rHLkUrdi4NIQLKj+hFEZbvgmWfdAyc0mvdIcjQOtRKVTFzQUekoJ5RvMAAqNt+usa99ym9hG2inmploNH8vP7sIMwBOqZg5qZUZdplZqaWpMkjoPGHvk7Qo05wxV+uW6ZF2Hs08w4x6suEQSYkg4w9GoRgcVihDBnHaDn2J6LCA3r6d6oxXCewrVPtWOdtck9GZ9EJx3njU7ympQ16SK197Rw9x5ByYRe5MwB2HwnavwmLi6gaZYFmv3Fz0XA4/sifZBChDuI0HSqYOLmHP2iZXJqwQ+s+FPJZcPzoXhey2m06jTBEobhKaLlRiHHnKScE/boasE6aQOUQLSKVCu3DKMM0TV3YKGVfCtKD4GcxYTBjdDU4ZKqJIL2QfOmY2fJs9CKFMpHVnz5VdNMJxD22Cc4x9U48hIn3xvNohbtGBwyA+1eLQpVW7u1swH+0H8cQQPJTg7bEsGenHHCwvK6z1WHnjRxeNY9vbSvwDQU/nuGw4w9yTBRgmlcxdWcEYqi2+pSYvRPo7XJikwDl/Cdu/VFf2tFWzjaWFUoO5uTHczXh4mDqwfiMdOqS06UBKzk/mGXKpAhaMQE/FcWXOVz46XyXcgSgP4RGajPPvwjmw43CYPytU9YGwr7Bt1Yovxv5WQJo2JH/DC7jlTwx/KF/xtPT72GYMe8GW4pWATt3SFbUf9DSjshX6xawYLMn/9NFZYVuOc2sHW60f0k5RKoksFUO/XmvE/YGvdDgpvO/28c/7msjBRU0z03Xc/5+rwKZlo2DcJk1fESQbRzfYEsS+YuSe+6IqfZcRm06KQrgChOzHcZdl5avm7yo858uQHC9LaoZoHAMzFJzTHUj+vuVG6S+0RN6PLTwBxOkdVHpSSUSkGyz+NO/ksw8zNq0Wp7x1fDUXFA9Ov3u4/3k7KUqkwxfLSkLRSX8tQDjn448ZZQCdwUKK

这个字符串是经过加密了的。需要先将字符串从base64转成正常串,然后再进行解密。我已经实现了这个功能,不过,对于小的字符串,如200字节长度,可以解出来,但是对于上面这个长的字符串,解出来就只有124字节长度。。。。。
4个字节对应3个字节。。。。肯定解出来不对。
实在是想不出来,希望有知道的人悉心解答,万分感谢。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • waterwater1 2015-11-10 03:20
    已采纳

    const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    int base64_decode( const char * base64, unsigned char * bindata )
    {
    int i, j;
    unsigned char k;
    unsigned char temp[4];
    for ( i = 0, j = 0; base64[i] != '\0' ; i += 4 )
    {
    memset( temp, 0xFF, sizeof(temp) );
    for ( k = 0 ; k < 64 ; k ++ )
    {
    if ( base64char[k] == base64[i] )
    temp[0]= k;
    }
    for ( k = 0 ; k < 64 ; k ++ )
    {
    if ( base64char[k] == base64[i+1] )
    temp[1]= k;
    }
    for ( k = 0 ; k < 64 ; k ++ )
    {
    if ( base64char[k] == base64[i+2] )
    temp[2]= k;
    }
    for ( k = 0 ; k < 64 ; k ++ )
    {
    if ( base64char[k] == base64[i+3] )
    temp[3]= k;
    }

        bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2))&0xFC)) |
                ((unsigned char)((unsigned char)(temp[1]>>4)&0x03));
        if ( base64[i+2] == '=' )
            break;
    
        bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4))&0xF0)) |
                ((unsigned char)((unsigned char)(temp[2]>>2)&0x0F));
        if ( base64[i+3] == '=' )
            break;
    
        bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6))&0xF0)) |
                ((unsigned char)(temp[3]&0x3F));
    }
    return j;
    

    }
    这个是我的base64解密,各位看一下

    已采纳该答案
    打赏 评论
  • zuishikonghuan 2015-11-09 14:13

    你的解码算法设计的不对,应该重新设计

    打赏 评论

相关推荐 更多相似问题