⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 base64.cpp

📁 用C语言实现的发送 邮件 SMTP的代码. 可以发送附件.大小无限制.
💻 CPP
字号:
// base64.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdlib.h"
#include "windows.h"

const int  BASE64_MAXLINE = 76;
const char EOL[] = "\r\n";
const char BASE64_TAB[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
      "abcdefghijklmnopqrstuvwxyz0123456789+/";
int Base64ToAnsi( char *dst,int nOutLen, char *src,int nInLen);
int ANSIToBase64(const char *szInANSI, int nInLen, char *szOutBase64, int nOutLen)
{
 //Input Parameter validation
 if ((szInANSI == NULL) || (nInLen == 0) || (szOutBase64 == NULL) || (nOutLen == 0))
  return 0;
 if (nOutLen < (nInLen*4/3 + 1 + nInLen*4/3/BASE64_MAXLINE*2 + 1 + 4))
  return 0;

 //Set up the parameters prior to the main encoding loop
 int nInPos  = 0;
 int nOutPos = 0;
 int nLineLen = 0;
 int c1, c2, c3;
 int i;

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

  //Encode into the 4 6 bit characters
  szOutBase64[nOutPos++] = BASE64_TAB[c1 >> 2];
  szOutBase64[nOutPos++] = BASE64_TAB[((c1 << 4) | (c2 >> 4)) & 0x3F];
  szOutBase64[nOutPos++] = BASE64_TAB[((c2 << 2) | (c3 >> 6)) & 0x3F];
  szOutBase64[nOutPos++] = BASE64_TAB[c3 & 0x3F];
  nLineLen += 4;
 
  //Handle the case where we have gone over the max line boundary
  if (nLineLen > BASE64_MAXLINE - 4)
  {
   szOutBase64[nOutPos++] = EOL[0];
   szOutBase64[nOutPos++] = EOL[1];
   nLineLen = 0;
  }
 }
// Encode the remaining one or two characters in the input buffer
 switch (nInLen % 3)
 {
  case 0:
  {
   szOutBase64[nOutPos++] = EOL[0];
   szOutBase64[nOutPos++] = EOL[1];
   break;
  }
  case 1:
  {
   c1 = szInANSI[nInPos] & 0xFF;
   szOutBase64[nOutPos++] = BASE64_TAB[(c1 & 0xFC) >> 2];
   szOutBase64[nOutPos++] = BASE64_TAB[((c1 & 0x03) << 4)];
   szOutBase64[nOutPos++] = '=';
   szOutBase64[nOutPos++] = '=';
   szOutBase64[nOutPos++] = EOL[0];
   szOutBase64[nOutPos++] = EOL[1];
   break;
  }
  case 2:
  {
   c1 = szInANSI[nInPos++] & 0xFF;
   c2 = szInANSI[nInPos] & 0xFF;
   szOutBase64[nOutPos++] = BASE64_TAB[(c1 & 0xFC) >> 2];
   szOutBase64[nOutPos++] = BASE64_TAB[((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4)];
   szOutBase64[nOutPos++] = BASE64_TAB[((c2 & 0x0F) << 2)];
   szOutBase64[nOutPos++] = '=';
   szOutBase64[nOutPos++] = EOL[0];
   szOutBase64[nOutPos++] = EOL[1];
   break;
  }
  default:
  {
   return 0;
  }
 }

 szOutBase64[nOutPos] = 0;

 return nOutPos;
}
////////////////////////////////////////////////////////////////
int Base64ToAnsi( char *dst,int nOutLen, char *src,int nInLen)
{if(dst==0 || src==0) return 0;
 int n,i,j;
  char *p;

 n=strlen(src);
 for(i=0;i<n;i++) /* map base64 ASCII character to 6 bit value */
 {
  p=strchr(BASE64_TAB,src[i]);
  if(!p)
   break;
  src[i]=p-BASE64_TAB;
 }
 memset(dst,0,n*3/4+1);
 for(i=0,j=0;i<n;i+=4,j+=3)
 {
  dst[j]=(src[i]<<2) + ((src[i+1]&0x30)>>4);
  dst[j+1]=((src[i+1]&0x0F)<<4) + ((src[i+2]&0x3C)>>2);
  dst[j+2]=((src[i+2]&0x03)<<6) + src[i+3];
 }
 return j+2;
}

void main()
{
	char  src[]="hello world!this is wagon";
	char dest[50]; char ansi[50];
	ANSIToBase64(src,26,dest, 50);
	printf("\n%s\n",dest);
    Base64ToAnsi(ansi,50,dest,50);
	printf("\n%s\n",ansi);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -