日期:2014-05-17  浏览次数:20912 次

以下c++代码如何转为c#,本人菜鸟急求
bool Base64Decode(const char *src, int src_len, char *dst, int &dst_len)
{
int i = 0, j = 0;
unsigned char base64_decode_map[256] =
{
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,
255, 255, 255, 0, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 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, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 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, 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, 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, 255,
255, 255, 255
};

for (; i < src_len; i += 4)
{
dst[j++] = base64_decode_map[src[i]] << 2 | base64_decode_map[src[i + 1]] >> 4;
if (j > dst_len) // 越界判断
{
return false;
}

dst[j++] = base64_decode_map[src[i + 1]] << 4 | base64_decode_map[src[i + 2]] >> 2;
if (j > dst_len) // 越界判断
{
return false;
}

dst[j++] = base64_decode_map[src[i + 2]] << 6 | base64_decode_map[src[i + 3]];
if (j > dst_len) // 越界判断
{
return false;
}
}

int equalCount = 0;
for(int k = src_len -1 ; k >= 0 ; --k)//从末尾判断加密字符串的=号数量
{
if(src[k] != '=')
{
break;
}
else
{
++equalCount;
}
}
if(equalCount >=0 && equalCount <= 2)
{
j -= equalCount;
}
else
{
return false;
}

dst[j] = '\0';
dst_len = j;
return true;
}

------解决方案--------------------
这么眼熟,好像转过,char* 可以用string,第几个字符可以通过索引访问 比如 string str= "abc",那么str[0]='a';
------解决方案--------------------
.net 有base64解码函数

Convert.FromBase64String

http://msdn.microsoft.com/en-us/library/system.convert.frombase64string.aspx
------解决方案--------------------
Base64的算法是通用的,没有加密解密的说法。我对各种语言创建的Base64字符串都成功解析,更何况C++和C#同属微软一家,没理由不能交互,除非那个是手动做了手脚的,非标准Base64算法。