base64编码/解码函数 c语言

  1. /* base64编码函数 */

  2. int base64(char *s,char *d)
  3. {
  4. char CharSet[64]={
  5. 'A','B','C','D','E','F','G','H',
  6. 'I','J','K','L','M','N','O','P',
  7. 'Q','R','S','T','U','V','W','X',
  8. 'Y','Z','a','b','c','d','e','f',
  9. 'g','h','i','j','k','l','m','n',
  10. 'o','p','q','r','s','t','u','v',
  11. 'w','x','y','z','0','1','2','3',
  12. '4','5','6','7','8','9','+','/'};
  13. unsigned char In[3];
  14. unsigned char Out[4];
  15. int cnt=0;

  16. if(!s||!d) return 0;

  17. for(;*s!=0;)
  18. {
  19. if(cnt+4>76)
  20. {
  21. *d++='\n';
  22. cnt=0;
  23. }
  24. if(strlen(s)>=3)
  25. {
  26. In[0]=*s;
  27. In[1]=*(s+1);
  28. In[2]=*(s+2);
  29. Out[0]=In[0]>>2;
  30. Out[1]=(In[0]&0x03)<<4|(In[1]&0xf0)>>4;
  31. Out[2]=(In[1]&0x0f)<<2|(In[2]&0xc0)>>6;
  32. Out[3]=In[2]&0x3f;
  33. *d=CharSet[Out[0]];
  34. *(d+1)=CharSet[Out[1]];
  35. *(d+2)=CharSet[Out[2]];
  36. *(d+3)=CharSet[Out[3]];
  37. s+=3;
  38. d+=4;
  39. }
  40. else if(strlen(s)==1)
  41. {
  42. In[0]=*s;
  43. Out[0]=In[0]>>2;
  44. Out[1]=(In[0]&0x03)<<4|0;
  45. *d=CharSet[Out[0]];
  46. *(d+1)=CharSet[Out[1]];
  47. *(d+2)='=';
  48. *(d+3)='=';
  49. s+=1;
  50. d+=4;
  51. }
  52. else if(strlen(s)==2)
  53. {
  54. In[0]=*s;
  55. In[1]=*(s+1);
  56. Out[0]=In[0]>>2;
  57. Out[1]=(In[0]&0x03)<<4|(In[1]&0xf0)>>4;
  58. Out[2]=(In[1]&0x0f)<<2|0;
  59. *d=CharSet[Out[0]];
  60. *(d+1)=CharSet[Out[1]];
  61. *(d+2)=CharSet[Out[2]];
  62. *(d+3)='=';
  63. s+=2;
  64. d+=4;
  65. }
  66. cnt+=4;
  67. }
  68. *d='\0';
  69. return 1;
  70. }



  71. /* base64解码函数 */

  72. int unbase64char(char ch)
  73. {
  74. char CharSet[64]={
  75. 'A','B','C','D','E','F','G','H',
  76. 'I','J','K','L','M','N','O','P',
  77. 'Q','R','S','T','U','V','W','X',
  78. 'Y','Z','a','b','c','d','e','f',
  79. 'g','h','i','j','k','l','m','n',
  80. 'o','p','q','r','s','t','u','v',
  81. 'w','x','y','z','0','1','2','3',
  82. '4','5','6','7','8','9','+','/'};
  83. int i;
  84. for(i=0;i<=63;i++) if(CharSet[i]==ch) break;
  85. return i;
  86. }

  87. int unbase64(char *s,char *d)
  88. {
  89. unsigned char In[4];
  90. unsigned char Out[3];

  91. if(!s||!d) return 0;

  92. for(;*s!=0;)
  93. {
  94. if(*s=='\n') s++;
  95. In[0]=s[0];
  96. In[1]=s[1];
  97. In[2]=s[2];
  98. In[3]=s[3];
  99. if(In[2]!='='&&In[3]!='=')
  100. {
  101. In[0]=unbase64char(In[0]);
  102. In[1]=unbase64char(In[1]);
  103. In[2]=unbase64char(In[2]);
  104. In[3]=unbase64char(In[3]);
  105. Out[0]=In[0]<<2|(In[1]&0x30)>>4;
  106. Out[1]=(In[1]&0x0f)<<4|(In[2]&0x3c)>>2;
  107. Out[2]=(In[2]&0x03)<<6|In[3]&0x3f;
  108. d[0]=Out[0];
  109. d[1]=Out[1];
  110. d[2]=Out[2];
  111. s+=4;
  112. d+=3;
  113. }
  114. else if(In[2]=='='&&In[3]=='=')
  115. {
  116. In[0]=unbase64char(In[0]);
  117. In[1]=unbase64char(In[1]);
  118. Out[0]=In[0]<<2|(In[1]&0x30)>>4;
  119. d[0]=Out[0];
  120. s+=4;
  121. d+=1;
  122. }
  123. else if(In[2]!='='&&In[3]=='=')
  124. {
  125. In[0]=unbase64char(In[0]);
  126. In[1]=unbase64char(In[1]);
  127. In[2]=unbase64char(In[2]);
  128. Out[0]=In[0]<<2|(In[1]&0x30)>>4;
  129. Out[1]=(In[1]&0x0f)<<4|(In[2]&0x3c)>>2;
  130. d[0]=Out[0];
  131. d[1]=Out[1];
  132. s+=4;
  133. d+=2;
  134. }
  135. }
  136. *d='\0';
  137. return 1;
  138. }

上面的代码只能处理字符串,改进一下

  1. int base64(char *s,char *d,int sl)
  2. {
  3.         char CharSet[64]=
  4.         {
  5.                 'A','B','C','D','E','F','G','H',
  6.                 'I','J','K','L','M','N','O','P',
  7.                 'Q','R','S','T','U','V','W','X',
  8.                 'Y','Z','a','b','c','d','e','f',
  9.                 'g','h','i','j','k','l','m','n',
  10.                 'o','p','q','r','s','t','u','v',
  11.                 'w','x','y','z','0','1','2','3',
  12.                 '4','5','6','7','8','9','+','/'
  13.         };
  14.         unsigned char In[3];
  15.         unsigned char Out[4];
  16.         int cnt=0;
  17.        
  18.         if(sl==0)return 0;
  19.         //if(!s||!d) return 0;
  20.        
  21.         int dl=0;
  22.         //for(;*s!=0;)
  23.         for(int i=0;i<sl;i=i+3)
  24.         {
  25. /*                if(cnt+4>76)
  26.                 {
  27.                         *d++='\n';
  28.                         cnt=0;
  29.                 }
  30. */
  31.                 //if(strlen(s)>=3)
  32.                 if((sl-i)>=3)
  33.                 {
  34.                         In[0]=*s;
  35.                         In[1]=*(s+1);
  36.                         In[2]=*(s+2);
  37.                         Out[0]=In[0]>>2;
  38.                         Out[1]=(In[0]&0x03)<<4|(In[1]&0xf0)>>4;
  39.                         Out[2]=(In[1]&0x0f)<<2|(In[2]&0xc0)>>6;
  40.                         Out[3]=In[2]&0x3f;
  41.                         *d=CharSet[Out[0]];
  42.                         *(d+1)=CharSet[Out[1]];
  43.                         *(d+2)=CharSet[Out[2]];
  44.                         *(d+3)=CharSet[Out[3]];
  45.                         s+=3;
  46.                         d+=4;
  47.                 }
  48.                 //else if(strlen(s)==1)
  49.                 else if((sl-i)==1)
  50.                 {
  51.                         In[0]=*s;
  52.                         Out[0]=In[0]>>2;
  53.                         Out[1]=(In[0]&0x03)<<4|0;
  54.                         *d=CharSet[Out[0]];
  55.                         *(d+1)=CharSet[Out[1]];
  56.                         *(d+2)='=';
  57.                         *(d+3)='=';
  58.                         s+=1;
  59.                         d+=4;
  60.                 }
  61.                 //else if(strlen(s)==2)
  62.                 else if((sl-i)==2)
  63.                 {
  64.                         In[0]=*s;
  65.                         In[1]=*(s+1);
  66.                         Out[0]=In[0]>>2;
  67.                         Out[1]=(In[0]&0x03)<<4|(In[1]&0xf0)>>4;
  68.                         Out[2]=(In[1]&0x0f)<<2|0;
  69.                         *d=CharSet[Out[0]];
  70.                         *(d+1)=CharSet[Out[1]];
  71.                         *(d+2)=CharSet[Out[2]];
  72.                         *(d+3)='=';
  73.                         s+=2;
  74.                         d+=4;
  75.                 }
  76.                 cnt+=4;
  77.                 dl+=4;
  78.         }
  79.         *d='\0';
  80.         return dl;
  81. }


文章来自: 本站原创
Tags:
评论: 0 | 查看次数: 10793