小7的大店铺【淘宝店】 游戏引擎剖析-全篇 公积金相关帖子汇总 魅族M8SDK学习系列不断更新中

[C\C++] C++的Base64编码源代码

[ 2070 查看 / 3 回复 ]

  1. /************************************************
  2. 头文件ZBase64.h
  3. ************************************************/

  4. #ifndef _ZBASE64
  5. #define _ZBASE64

  6. #pragma warning(disable:4786)
  7. #include
  8. using namespace std;

  9. class ZBase64
  10. {

  11. private:

  12. //Base64编码解码表
  13. char* m_Base64_Table;

  14. public:

  15. //构造
  16. ZBase64();

  17. //编码
  18. string EncodeBase64(const string strSource);

  19. //解码
  20. string DecodeBase64(const string strSource);
  21. };
  22. #endif
复制代码
  1. /************************************************
  2. 实现文件ZBase64.cpp
  3. ************************************************/

  4. #include "stdAfx.h"
  5. #include "Base64.h"

  6. ZBase64::ZBase64()
  7. {
  8. //Base64编码表
  9. this->m_Base64_Table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  10. }

  11. string ZBase64::EncodeBase64(const string strSource)
  12. {
  13. /*
  14. * 以下是操作二进制数时用到的
  15. * 11111100  0xFC
  16. * 11000000  0x3
  17. * 11110000  0xF0
  18. * 00001111  0xF
  19. * 11000000  0xC0
  20. * 00111111  0x3F
  21. */

  22. string strEncode;
  23. char cTemp[4];

  24. //行长,MIME规定Base64编码行长为76字节
  25. int LineLength=0;

  26. for(int i=0;i
  27. {
  28.   memset(cTemp,0,4);

  29.   cTemp[0]=strSource[i];
  30.   cTemp[1]=strSource[i+1];
  31.   cTemp[2]=strSource[i+2];

  32.   int len=strlen(cTemp);
  33.   if(len==3)
  34.   {
  35.     strEncode+=this->m_Base64_Table[((int)cTemp[0] & 0xFC)>>2];
  36.     strEncode+=this->m_Base64_Table[((int)cTemp[0] & 0x3)<<4 | ((int)cTemp[1] & 0xF0)>>4];
  37.     strEncode+=this->m_Base64_Table[((int)cTemp[1] & 0xF)<<2 | ((int)cTemp[2] & 0xC0)>>6];
  38.     strEncode+=this->m_Base64_Table[(int)cTemp[2] & 0x3F];
  39.     if(LineLength+=4>=76) strEncode+="\r\n";
  40.   }
  41.   else if(len==2)
  42.   {
  43.     strEncode+=this->m_Base64_Table[((int)cTemp[0] & 0xFC)>>2];
  44.     strEncode+=this->m_Base64_Table[((int)cTemp[0] & 0x3 )<<4 | ((int)cTemp[1] & 0xF0 )>>4];
  45.     strEncode+=this->m_Base64_Table[((int)cTemp[1] & 0x0F)<<2];
  46.     if(LineLength+=4>=76) strEncode+="\r\n";
  47.     strEncode+="=";
  48.   }
  49.   else if(len==1)
  50.   {
  51.     strEncode+=this->m_Base64_Table[((int)cTemp[0] & 0xFC)>>2];
  52.     strEncode+=this->m_Base64_Table[((int)cTemp[0] & 0x3 )<<4];
  53.     if(LineLength+=4>=76) strEncode+="\r\n";
  54.     strEncode+="==";
  55.   }
  56.   memset(cTemp,0,4);
  57. }
  58. return strEncode;
  59. }

  60. string ZBase64::DecodeBase64(const string strSource)
  61. {
  62. //返回值
  63. string strDecode;
  64. char cTemp[5];

  65. for(int i=0;i
  66. {
  67.   memset(cTemp,0,5);

  68.   cTemp[0]=strSource[i];
  69.   cTemp[1]=strSource[i+1];
  70.   cTemp[2]=strSource[i+2];
  71.   cTemp[3]=strSource[i+3];

  72.   int asc[4];
  73.   for(int j=0;j<4;j++)
  74.   {
  75.     for(int k=0;k<(int)strlen(this->m_Base64_Table);k++)
  76.     {
  77.     if(cTemp[j]==this->m_Base64_Table[k]) asc[j]=k;
  78.     }
  79.   }
  80.   if('='==cTemp[2] && '='==cTemp[3])
  81.   {
  82.     strDecode+=(char)(int)(asc[0] << 2 | asc[1] << 2 >> 6);
  83.   }
  84.   else if('='==cTemp[3])
  85.   {
  86.     strDecode+=(char)(int)(asc[0] << 2 | asc[1] << 2 >> 6);
  87.     strDecode+=(char)(int)(asc[1] << 4 | asc[2] << 2 >> 4);
  88.   }
  89.   else
  90.   {
  91.     strDecode+=(char)(int)(asc[0] << 2 | asc[1] << 2 >> 6);
  92.     strDecode+=(char)(int)(asc[1] << 4 | asc[2] << 2 >> 4);
  93.     strDecode+=(char)(int)(asc[2] << 6 | asc[3] << 2 >> 2);
  94.   } 
  95. }
  96. return strDecode;
  97. }
复制代码
本主题由 皇帝 5207 于 2009-6-26 20:37:16 执行 设置精华/取消 操作
分享 转发
相信与不相信都是矛盾的.  5207宣!
欢迎您来到迷你论坛
TOP

XyLNnAPqgvdZofOJjs

http://microsoftontheissues.com/cs/members/insomnia-and-getting-back-to-sleep-routine.aspx insomnia and getting back to sleep routine | <a href="http://microsoftontheissues.com/cs/members/insomnia-and-getting-back-to-sleep-routine.aspx"> insomnia and getting back to sleep routine </a>
http://microsoftontheissues.com/cs/members/levitra-masturbation.aspx levitra masturbation | <a href="http://microsoftontheissues.com/cs/members/levitra-masturbation.aspx"> levitra masturbation </a>
http://microsoftontheissues.com/cs/members/estradiol-vaginal-tablets.aspx estradiol vaginal tablets | <a href="http://microsoftontheissues.com/cs/members/estradiol-vaginal-tablets.aspx"> estradiol vaginal tablets </a>
http://microsoftontheissues.com/cs/members/levitra-acquisto.aspx levitra acquisto | <a href="http://microsoftontheissues.com/cs/members/levitra-acquisto.aspx"> levitra acquisto </a>
http://microsoftontheissues.com/cs/members/amoxicillin-alcohol.aspx amoxicillin alcohol | <a href="http://microsoftontheissues.com/cs/members/amoxicillin-alcohol.aspx"> amoxicillin alcohol </a>
http://microsoftontheissues.com/cs/members/fluoxetine-biopharmaceutic-biowaiver.aspx fluoxetine biopharmaceutic biowaiver | <a href="http://microsoftontheissues.com/cs/members/fluoxetine-biopharmaceutic-biowaiver.aspx"> fluoxetine biopharmaceutic biowaiver </a>
http://microsoftontheissues.com/cs/members/glucosamine-side-effects-and-rash.aspx glucosamine side effects and rash | <a href="http://microsoftontheissues.com/cs/members/glucosamine-side-effects-and-rash.aspx"> glucosamine side effects and rash </a>
http://microsoftontheissues.com/cs/members/acetaminophen-ibuprofen.aspx acetaminophen ibuprofen | <a href="http://microsoftontheissues.com/cs/members/acetaminophen-ibuprofen.aspx"> acetaminophen ibuprofen </a>
http://microsoftontheissues.com/cs/members/erythromycin-antibiotic-without-a-prescription.aspx erythromycin antibiotic without a prescription | <a href="http://microsoftontheissues.com/cs/members/erythromycin-antibiotic-without-a-prescription.aspx"> erythromycin antibiotic without a prescription </a>
http://microsoftontheissues.com/cs/members/doxycycline-and-weight-gain.aspx doxycycline and weight gain | <a href="http://microsoftontheissues.com/cs/members/doxycycline-and-weight-gain.aspx"> doxycycline and weight gain </a>
http://microsoftontheissues.com/cs/members/compatibility-for-darvocet-and-motrin.aspx compatibility for darvocet and motrin | <a href="http://microsoftontheissues.com/cs/members/compatibility-for-darvocet-and-motrin.aspx"> compatibility for darvocet and motrin </a>
http://microsoftontheissues.com/cs/members/insomnia-relief.aspx insomnia relief | <a href="http://microsoftontheissues.com/cs/members/insomnia-relief.aspx"> insomnia relief </a>
http://microsoftontheissues.com/cs/members/aspirin-for-horses.aspx aspirin for horses | <a href="http://microsoftontheissues.com/cs/members/aspirin-for-horses.aspx"> aspirin for horses </a>
http://microsoftontheissues.com/cs/members/erythromycin-ointment.aspx erythromycin ointment | <a href="http://microsoftontheissues.com/cs/members/erythromycin-ointment.aspx"> erythromycin ointment </a>
http://microsoftontheissues.com/cs/members/diflucan-150mg-no-prescription.aspx diflucan 150mg no prescription | <a href="http://microsoftontheissues.com/cs/members/diflucan-150mg-no-prescription.aspx"> diflucan 150mg no prescription </a>
http://microsoftontheissues.com/cs/members/what-is-hydrocodone-gg-syrup-used-for.aspx what is hydrocodone gg syrup used for | <a href="http://microsoftontheissues.com/cs/members/what-is-hydrocodone-gg-syrup-used-for.aspx"> what is hydrocodone gg syrup used for </a>
http://microsoftontheissues.com/cs/members/levitra-senza-ricetta.aspx levitra senza ricetta | <a href="http://microsoftontheissues.com/cs/members/levitra-senza-ricetta.aspx"> levitra senza ricetta </a>
http://microsoftontheissues.com/cs/members/what-is-tramadol.aspx what is tramadol | <a href="http://microsoftontheissues.com/cs/members/what-is-tramadol.aspx"> what is tramadol </a>
http://microsoftontheissues.com/cs/members/what-is-erythromycin-used-for.aspx what is erythromycin used for | <a href="http://microsoftontheissues.com/cs/members/what-is-erythromycin-used-for.aspx"> what is erythromycin used for </a>
http://microsoftontheissues.com/cs/members/how-adipex-works.aspx how adipex works | <a href="http://microsoftontheissues.com/cs/members/how-adipex-works.aspx"> how adipex works </a>
http://microsoftontheissues.com/cs/members/ambien-and-fluoxetine.aspx ambien and fluoxetine | <a href="http://microsoftontheissues.com/cs/members/ambien-and-fluoxetine.aspx"> ambien and fluoxetine </a>
http://microsoftontheissues.com/cs/members/side-effects-tramadol.aspx side effects tramadol | <a href="http://microsoftontheissues.com/cs/members/side-effects-tramadol.aspx"> side effects tramadol </a>
http://microsoftontheissues.com/cs/members/adrenal-stress-insomnia.aspx adrenal stress insomnia | <a href="http://microsoftontheissues.com/cs/members/adrenal-stress-insomnia.aspx"> adrenal stress insomnia </a>
http://microsoftontheissues.com/cs/members/albuterol-dexamethasone-nebulizer.aspx albuterol dexamethasone nebulizer | <a href="http://microsoftontheissues.com/cs/members/albuterol-dexamethasone-nebulizer.aspx"> albuterol dexamethasone nebulizer </a>
http://microsoftontheissues.com/cs/members/ambien-effectiveness.aspx ambien effectiveness | <a href="http://microsoftontheissues.com/cs/members/ambien-effectiveness.aspx"> ambien effectiveness </a>
http://microsoftontheissues.com/cs/members/allegra-erika.aspx allegra erika | <a href="http://microsoftontheissues.com/cs/members/allegra-erika.aspx"> allegra erika </a>
http://microsoftontheissues.com/cs/members/atenolol-mexico.aspx atenolol mexico | <a href="http://microsoftontheissues.com/cs/members/atenolol-mexico.aspx"> atenolol mexico </a>
http://microsoftontheissues.com/cs/members/atenolol-stage-freight.aspx atenolol stage freight | <a href="http://microsoftontheissues.com/cs/members/atenolol-stage-freight.aspx"> atenolol stage freight </a>
http://microsoftontheissues.com/cs/members/applying-androgel-cream-to-the-scrotum.aspx applying androgel cream to the scrotum | <a href="http://microsoftontheissues.com/cs/members/applying-androgel-cream-to-the-scrotum.aspx"> applying androgel cream to the scrotum </a>
http://microsoftontheissues.com/cs/members/buy-ambien-online.aspx buy ambien online | <a href="http://microsoftontheissues.com/cs/members/buy-ambien-online.aspx"> buy ambien online </a>
TOP

回复 2# 游客 的帖子

发的是什么?
相信与不相信都是矛盾的.  5207宣!
欢迎您来到迷你论坛
TOP

g广告吧?
TOP