C ++ Base64 Unicode-空字节 [英] C++ Base64 Unicode - null bytes

查看:103
本文介绍了C ++ Base64 Unicode-空字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对unicode字符串进行base64编码.我遇到了问题,在编码之后,输出是我的字符串base64'ed,但是,在整个代码中的随机位置都存在空字节,我不知道为什么,或者如何将它们取出.

I am trying to base64 encode a unicode string. I am running into problems, after the encoding, the output is my string base64'ed however, there is null bytes at random places in throughout the code, I don't know why, or how to get them out.

这是我的Base64Encode函数:

Here is my Base64Encode function:

static char Base64Digits[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int Base64Encode(const BYTE* pSrc, int nLenSrc, wchar_t* pDst, int nLenDst)
{
   int nLenOut= 0;
   while ( nLenSrc > 0 ) {
  if (nLenOut+4 > nLenDst) return(0); // error

  // read three source bytes (24 bits) 
  BYTE s1= pSrc[0];   // (but avoid reading past the end)
  BYTE s2= 0; if (nLenSrc>1) s2=pSrc[1]; //------ corrected, thanks to  jprichey
  BYTE s3= 0; if (nLenSrc>2) s3=pSrc[2];

  DWORD n;
  n =  s1;    // xxx1
  n <<= 8;    // xx1x
  n |= s2;    // xx12  
  n <<= 8;    // x12x
  n |= s3;    // x123  

  //-------------- get four 6-bit values for lookups
  BYTE m4= n & 0x3f;  n >>= 6;
  BYTE m3= n & 0x3f;  n >>= 6;
  BYTE m2= n & 0x3f;  n >>= 6;
  BYTE m1= n & 0x3f;  

  //------------------ lookup the right digits for output
  BYTE b1 = Base64Digits[m1];
  BYTE b2 = Base64Digits[m2];
  BYTE b3 = Base64Digits[m3];
  BYTE b4 = Base64Digits[m4];

  //--------- end of input handling
  *pDst++ = b1;
  *pDst++ = b2;
  if ( nLenSrc >= 3 ) {  // 24 src bits left to encode, output xxxx
     *pDst++ = b3;
     *pDst++ = b4;
  }
  if ( nLenSrc == 2 ) {  // 16 src bits left to encode, output xxx=
     *pDst++ = b3;
     *pDst++ = '=';
     }
  if ( nLenSrc == 1 ) {  // 8 src bits left to encode, output xx==
     *pDst++ = '=';
     *pDst++ = '=';
  }
  pSrc    += 3;
  nLenSrc -= 3;
  nLenOut += 4;
 }
 // Could optionally append a NULL byte like so:
 // *pDst++= 0; nLenOut++;
 return( nLenOut );  
}

不要欺骗任何人,但是我从此处

Not to fool anyone, but I copied the function from here

这是我调用函数的方式:

Here is how I call the function:

wchar_t base64[256];

Base64Encode((const unsigned char *)UserLoginHash, lstrlenW(UserLoginHash) * 2, base64, 256);

那么,为什么在生成的哈希中存在随机的空字节或空白"?应该进行哪些更改以使我摆脱它们?

So, why is there random null-bytes or "whitespaces" in the generated hash? What should be changed so that I can get rid of them?

推荐答案

尝试更多类似的方法.从我自己的base64编码器复制的部分:

Try something more like this. Portions copied from my own base64 encoder:

static const wchar_t *Base64Digits = L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int Base64Encode(const BYTE* pSrc, int nLenSrc, wchar_t* pDst, int nLenDst)
{
    int nLenOut = 0;

    while (nLenSrc > 0) {
        if (nLenDst < 4) return(0); // error

        // read up to three source bytes (24 bits) 
        int len = 0;
        BYTE s1 = pSrc[len++];
        BYTE s2 = (nLenSrc > 1) ? pSrc[len++] : 0
        BYTE s3 = (nLenSrc > 2) ? pSrc[len++] : 0;
        pSrc += len;
        nLenSrc -= len;

        //------------------ lookup the right digits for output
        pDst[0] = Base64Digits[(s1 >> 2) & 0x3F];
        pDst[1] = Base64Digits[(((s1 & 0x3) << 4) | ((s2 >> 4) & 0xF)) & 0x3F];
        pDst[2] = Base64Digits[(((s2 & 0xF) << 2) | ((s3 >> 6) & 0x3)) & 0x3F];
        pDst[3] = Base64Digits[s3 & 0x3F];

        //--------- end of input handling
        if (len < 3) {  // less than 24 src bits encoded, pad with '='
          pDst[3] = L'=';
          if (len == 1)
            pDst[2] = L'=';
        }

        nLenOut += 4;
        pDst += 4;
        nLenDst -= 4;
    }

    if (nLenDst > 0) *pDst = 0;

    return (nLenOut);
}

这篇关于C ++ Base64 Unicode-空字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆