免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1069 | 回复: 1
打印 上一主题 下一主题

base64解码问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-11-30 20:52 |只看该作者 |倒序浏览
求解以下BASE64字符串的解码!哪位有现成的C函数希望能提供一下!我现在有2个函数但是
都不好使!
本来正常解码之后应该是这个样子:~tt.txt
但是我现在的函数解了之后如:  ~<0x00>t<0x00>t<0x00>.<0x00>t<0x00>x<0x00>t
无缘无故地在字符之间加上了 0!这样当我打印的时候就打不出来了。
请问这是为何?如何解决?
各位有现成的解码函数吗?试着解一下这个字符串,看看你们的能正常解开么?
字符串如下:
fgIAAAMAAAACIQAAAAAAAAEAAAB0AHQALgB0AHgAdAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=

论坛徽章:
0
2 [报告]
发表于 2006-12-01 12:42 |只看该作者

  1. /***********************************************************************
  2.                            Mady By ZwelL
  3.                              2004.8.16
  4.                            zwell@sohu.com
  5.                     http://www.donews.net/zwell

  6. Usage:  base.exe <-e|-d> <string>
  7. ***********************************************************************/
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>

  11. const int       BASE64_MAXLINE = 76;
  12. const char      EOL[] = "\r\n";
  13. const char      BASE64_TAB[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  14. const char      DeBase64Tab[] =
  15. {
  16.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  17.         0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  18.         62, //'+'
  19.         0, 0, 0,
  20.         63, //'/'
  21.         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, //'0' - '9'
  22.         0, 0, 0, 0, 0, 0, 0,
  23.         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  24.         13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, //'A' - 'Z'
  25.         0, 0, 0, 0, 0, 0,
  26.         26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  27.         39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, //'a' - 'z'
  28. };

  29. char           *
  30. enBase64(const char *szInANSI)
  31. {
  32.         int             nInLen = strlen(szInANSI);
  33.         char           *szOutBase64;
  34.         char           *returnstr;

  35.         //Set up the parameters prior to the main encoding loop
  36.         int             nInPos = 0;
  37.         int             nOutPos = 0;
  38.         int             nLineLen = 0;
  39.         int             c1, c2, c3;
  40.         int             i;

  41.         szOutBase64 = (char *)malloc(nInLen * 4 / 3 + 1);
  42.         returnstr = szOutBase64;
  43.         memset(szOutBase64, 0, nInLen * 4 / 3 + 1);
  44.         //Input Parameter validation
  45.                 if ((szInANSI == NULL) || (nInLen == 0) || (szOutBase64 == NULL))
  46.                 return 0;


  47.         //Get three characters at a time from the input buffer and encode them
  48.                 for (i = 0; i < nInLen / 3; ++i) {
  49.                 //Get the next 2 characters
  50.                 c1 = szInANSI[nInPos++] & 0xFF;
  51.                 c2 = szInANSI[nInPos++] & 0xFF;
  52.                 c3 = szInANSI[nInPos++] & 0xFF;

  53.                 //Encode into the 4 6 bit characters
  54.         szOutBase64[nOutPos++] = BASE64_TAB[c1 >> 2];
  55.                 szOutBase64[nOutPos++] = BASE64_TAB[((c1 << 4) | (c2 >> 4)) & 0x3F];
  56.                 szOutBase64[nOutPos++] = BASE64_TAB[((c2 << 2) | (c3 >> 6)) & 0x3F];
  57.                 szOutBase64[nOutPos++] = BASE64_TAB[c3 & 0x3F];
  58.                 nLineLen += 4;

  59.                 //Handle the case where we have gone over the max line boundary
  60.                 if (nLineLen > BASE64_MAXLINE - 4) {
  61.                         szOutBase64[nOutPos++] = EOL[0];
  62.                         szOutBase64[nOutPos++] = EOL[1];
  63.                         nLineLen = 0;
  64.                 }
  65.         }

  66.         //Encode the remaining one or two characters in the input buffer
  67.         switch (nInLen % 3) {
  68.         case 0: {
  69.                         szOutBase64[nOutPos++] = EOL[0];
  70.                         szOutBase64[nOutPos++] = EOL[1];
  71.                         break;
  72.                 }
  73.         case 1: {
  74.                         c1 = szInANSI[nInPos] & 0xFF;
  75.                         szOutBase64[nOutPos++] = BASE64_TAB[(c1 & 0xFC) >> 2];
  76.                         szOutBase64[nOutPos++] = BASE64_TAB[((c1 & 0x03) << 4)];
  77.                         szOutBase64[nOutPos++] = '=';
  78.                         szOutBase64[nOutPos++] = '=';
  79.                         szOutBase64[nOutPos++] = EOL[0];
  80.                         szOutBase64[nOutPos++] = EOL[1];
  81.                         break;
  82.                 }
  83.         case 2: {
  84.                         c1 = szInANSI[nInPos++] & 0xFF;
  85.                         c2 = szInANSI[nInPos] & 0xFF;
  86.                         szOutBase64[nOutPos++] = BASE64_TAB[(c1 & 0xFC) >> 2];
  87.                         szOutBase64[nOutPos++] = BASE64_TAB[((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4)];
  88.                         szOutBase64[nOutPos++] = BASE64_TAB[((c2 & 0x0F) << 2)];
  89.                         szOutBase64[nOutPos++] = '=';
  90.                         szOutBase64[nOutPos++] = EOL[0];
  91.                         szOutBase64[nOutPos++] = EOL[1];
  92.                         break;
  93.                 }
  94.         default:
  95.                 {
  96.                         return 0;
  97.                 }
  98.         }

  99.         szOutBase64[nOutPos] = 0;

  100.         return returnstr;
  101. }

  102. char           *
  103. deBase64(const char *pSrc)
  104. {
  105.         int             nDstLen;
  106.         //Output string 's length
  107.         int             nValue;
  108.         //decode used length
  109.         int             i;
  110.         int             nSrcLen;
  111.         unsigned char  *pDst;
  112.         char           *ptmpDst;

  113.         i = 0;
  114.         nDstLen = 0;
  115.         nSrcLen = strlen(pSrc);
  116.         pDst = (unsigned char *)malloc(nSrcLen * 3 / 4 + 1);
  117.         ptmpDst = (char *)pDst;
  118.         memset(pDst, 0, nSrcLen * 3 / 4 + 1);

  119.         //Get four characters at a time from the input buffer and decode them
  120.         while (i < nSrcLen) {
  121.                 if (*pSrc != '\r' && *pSrc != '\n') {
  122.                         nValue = DeBase64Tab[*pSrc++] << 18;
  123.                         nValue += DeBase64Tab[*pSrc++] << 12;
  124.                         *pDst++ = (nValue & 0x00ff0000) >> 16;
  125.                         nDstLen++;

  126.                         if (*pSrc != '=') {
  127.                                 nValue += DeBase64Tab[*pSrc++] << 6;
  128.                                 *pDst++ = (nValue & 0x0000ff00) >> 8;
  129.                                 nDstLen++;

  130.                                 if (*pSrc != '=') {
  131.                                         nValue += DeBase64Tab[*pSrc++];
  132.                                         *pDst++ = nValue & 0x000000ff;
  133.                                         nDstLen++;
  134.                                 }
  135.                         }
  136.                         i += 4;
  137.                 } else
  138.                         //CR LF not care
  139.                 {
  140.                         pSrc++;
  141.                         i++;
  142.                 }
  143.         }

  144.         //the end of the output string
  145.         * pDst = '\0';

  146.         return ptmpDst;
  147. }

  148. void
  149. Usage(char *exename)
  150. {
  151.         printf("Usage:%s -e|-d string\r\n", exename);
  152. }

  153. int
  154. main(int argc, char **argv)
  155. {
  156.         if (argc < 3) {
  157.                 Usage(argv[0]);
  158.                 return -1;
  159.         }
  160.         if (!strcmp(argv[1], "-e")) {
  161.                 printf(enBase64(argv[2]));
  162.         printf("\n");
  163.         } else if (!strcmp(argv[1], "-d")) {
  164.                 printf(deBase64(argv[2]));
  165.         printf("\n");
  166.         } else {
  167.                 Usage(argv[0]);
  168.                 return -1;
  169.         }

  170.         return 0;
  171. }

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP