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

📄 base64my.pc

📁 自己编写的BASE64算法
💻 PC
字号:
/* base64.c: MIME BASE64编码与解码器,读标准输入. 赵建军, 2000.1.12.a *//* 2000.2.19.A: 增文件处理. */#include "stdio.h"#include "string.h"#include "stdlib.h"#include "ctype.h"char	*sBaseStr="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";union{   struct{    unsigned long four:6;    unsigned long three:6;    unsigned long two:6;    unsigned long one:6;   }short_string;   char	new_other[3];}union_string;int	decode=0;main(int argc,char **argv){  extern char	*optarg;  extern int	optind;  char temp_string[500];  char out_string[500];  int		i;  char cChar;  decode=1;  memset(&temp_string,0,sizeof(temp_string));  memset(&out_string,0,sizeof(out_string));  strcpy(temp_string,"27jrWz2sxrVbR+pnyg6jWHhgNk4sZo46");  for(i=0;i<strlen(temp_string);i++){  	cChar=temp_string[i];  	printf("%c,",cChar);  	}  base64decode(temp_string,out_string);  printf("out string:\n");  for(i=0;i<strlen(out_string);i++){  	printf("[%c],[%d]",out_string[i],out_string[i]);  	}   printf("\n");    exit(0);}/*编码*/char *base64encode(char *input,char *output){ char	cString; char sTemp[1000]; char sTmp[10]; int i,n; memset(&sTemp,0,sizeof(sTemp)); strcpy(sTemp,input); for(i=0;i<strlen(sTemp);i+=3) {  union_string.new_other[0]=union_string.new_other[1]=union_string.new_other[2]=0;  for (n=0;n<3;n++)  {   cString=sTemp[i+n];   if (cString==EOF) break;   union_string.new_other[2-n]=cString;  }  if (n==0) break;  switch(n)  {   case 1:       memset(&sTmp,0,sizeof(sTmp));      sprintf(sTmp,"%c%c==",sBaseStr[union_string.short_string.one],sBaseStr[union_string.short_string.two]);      strcat(sTemp,sTmp);      break;   case 2:       memset(&sTmp,0,sizeof(sTmp));      sprintf(sTmp,"%c%c%c=",sBaseStr[union_string.short_string.one],sBaseStr[union_string.short_string.two],sBaseStr[union_string.short_string.three]);      strcat(sTemp,sTmp);      break;   case 3:       memset(&sTmp,0,sizeof(sTmp));      sprintf(sTmp,"%c%c%c%c",sBaseStr[union_string.short_string.one],sBaseStr[union_string.short_string.two],sBaseStr[union_string.short_string.three],sBaseStr[union_string.short_string.four]);      strcat(sTemp,sTmp);      break;  } } strcpy(output,sTemp); return(output);}/*解码*/char *base64decode(char *input,char *output){ char cStr1,cStr2,cStr3,cStr4; char sTmp[10]; char sTemp[1000]; char sReturn[1000]; int i; memset(&sTemp,0,sizeof(sTemp)); memset(&sReturn,0,sizeof(sReturn)); strcpy(sTemp,input); for(i=0;i<strlen(sTemp);i+=4){  cStr1=sTemp[i];  printf("cStr1:[%02X],cStr1:[%c]\n",cStr1,cStr1);  if (cStr1==EOF||cStr1=='\n'||cStr1=='\0'){  	 break;  }    if (strchr(sBaseStr,cStr1)==NULL&&cStr1!='=')  {   printf("Error: %c : not BASE64 code!\n",cStr1);   strcpy(output,"");   return output;  }  cStr2=sTemp[i+1];  printf("cStr2:[%02X],cStr2:[%c]\n",cStr2,cStr2);  if (cStr2==EOF)  {   printf("Error: unexpected EOF in BASE64 code!\n");   strcpy(output,"");   return output;  }  if (strchr(sBaseStr,cStr2)==NULL&&cStr2!='=')  {   printf("Error: %c : not BASE64 code!\n",cStr2);   strcpy(output,"");   return output;  }  cStr3=sTemp[i+2];  printf("cStr3:[%02X],cStr3:[%c]\n",cStr3,cStr3);  if (cStr3==EOF)  {   printf("Error: unexpected EOF in BASE64 code!\n");   strcpy(output,"");   return output;  }  if (strchr(sBaseStr,cStr3)==NULL&&cStr3!='=')  {   printf("Error: %c : not BASE64 code!\n",cStr3);   strcpy(output,"");   return output;  }  cStr4=sTemp[i+3];  if (cStr4==EOF)  {   printf("Error: unexpected EOF in BASE64 code!\n");   strcpy(output,"");   return output;  }  if (strchr(sBaseStr,cStr4)==NULL&&cStr4!='=')  {   printf("Error: %c : not BASE64 code!\n",cStr4);   strcpy(output,"");   return output;  }  if (strchr(sBaseStr,cStr1)&&strchr(sBaseStr,cStr2)&&(strchr(sBaseStr,cStr3)||cStr3=='=')&&  	((cStr3=='='&&cStr4=='=')||(cStr3!='='&&(cStr4=='='||strchr(sBaseStr,cStr4)))))  {   union_string.short_string.one=strchr(sBaseStr,cStr1)-sBaseStr;   union_string.short_string.two=strchr(sBaseStr,cStr2)-sBaseStr;   if (cStr3=='=')   {    memset(&sTmp,0,sizeof(sTmp));    sprintf(sTmp,"%c",union_string.new_other[2]);    strcat(sReturn,sTmp);   }   else   if (cStr4=='=')   {    union_string.short_string.three=strchr(sBaseStr,cStr3)-sBaseStr;    memset(&sTmp,0,sizeof(sTmp));    sprintf(sTmp,"%c%c",union_string.new_other[2],union_string.new_other[1]);    strcat(sReturn,sTmp);   }   else   {    union_string.short_string.three=strchr(sBaseStr,cStr3)-sBaseStr;    union_string.short_string.four=strchr(sBaseStr,cStr4)-sBaseStr;    memset(&sTmp,0,sizeof(sTmp));    sprintf(sTmp,"%c%c%c",union_string.new_other[2],union_string.new_other[1],union_string.new_other[0]);    strcat(sReturn,sTmp);       }  }  else  {   printf("Error: %c%c%c%c : not BASE64 code!\n",cStr1,cStr2,cStr3,cStr4);   strcpy(output,"");   return output;  } } strcpy(output,sReturn); return(output);}

⌨️ 快捷键说明

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