博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
base64 源码
阅读量:7028 次
发布时间:2019-06-28

本文共 3456 字,大约阅读时间需要 11 分钟。

/* 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    *base="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
union
{
 struct
 {
  unsigned long d:6;
  unsigned long c:6;
  unsigned long b:6;
  unsigned long a:6;
 }s;
 unsigned char  c[3];
}u;
int     decode=0;
main(int argc,char **argv)
{
 extern char    *optarg;
 extern int     optind;
 int            c;
 FILE           *fp;
 while ((c=getopt(argc,argv,"u"))!=EOF)
 {
  switch(c)
  {
   case 'u': decode=1; break;
   default: usage(); break;
  }
 }
 if (argc==optind)
 {
  if (isatty(0)) usage();
  if (decode) base64decode(stdin,stdout); else base64encode(stdin,stdout);
 }
 else
 for (;optind<argc;optind++)
 {
  fp=fopen(argv[optind],"r");
  if (fp==0) { fprintf(stderr,"%s: fopen %s error.\n",argv[0],argv[optind]); continue; }
  if (decode) base64decode(fp,stdout); else base64encode(fp,stdout);
  fclose(fp);
 }
 exit(0);
}
usage()
{
 fprintf(stderr,"base64: MIME BASE64 encoder and decoder, by zzz ( ) at 2000.1.11.m\n");
 fprintf(stderr,"Usage: base64 [-u] [file1] [file2] ...\n");
 exit(1);
}
base64encode(FILE *fp,FILE *fq)
{
 int    ch,n,h=0,flag=0;
 while (1)
 {
  u.c[0]=u.c[1]=u.c[2]=0;
  for (n=0;n<3;n++)
  {
   ch=fgetc(fp);
   if (ch==EOF) break;
   u.c[2-n]=ch;
  }
  if (n==0) break;
  flag=1;
  switch(n)
  {
   case 1: fprintf(fq,"%c%c==",base[u.s.a],base[u.s.b]);break;
   case 2: fprintf(fq,"%c%c%c=",base[u.s.a],base[u.s.b],base[u.s.c]);break;
   case 3: fprintf(fq,"%c%c%c%c",base[u.s.a],base[u.s.b],base[u.s.c],base[u.s.d]);break;
  }
  if (h==15) { h=0; fprintf(fq,"\n"); } else h++;
 }
 if (flag&&h) fprintf(fq,"\n");
}
base64decode(FILE *fp,FILE *fq)
{
 int    c1,c2,c3,c4;
 while (1)
 {
  /* c1 */
  c1=fgetc(fp);
  while (c1!=EOF&&isspace(c1)) c1=fgetc(fp);
  if (c1==EOF) break;
  if (strchr(base,c1)==NULL&&c1!='=')
  {
   fprintf(fq,"Error: %c : not BASE64 code!\n",c1);
   return 1;
  }
  /* c2 */
  c2=fgetc(fp);
  if (c2==EOF)
  {
   fprintf(fq,"Error: unexpected EOF in BASE64 code!\n");
   return 1;
  }
  if (strchr(base,c2)==NULL&&c2!='=')
  {
   fprintf(fq,"Error: %c : not BASE64 code!\n",c2);
   return 1;
  }
  /* c3 */
  c3=fgetc(fp);
  if (c3==EOF)
  {
   fprintf(fq,"Error: unexpected EOF in BASE64 code!\n");
   return 1;
  }
  if (strchr(base,c3)==NULL&&c3!='=')
  {
   fprintf(fq,"Error: %c : not BASE64 code!\n",c3);
   return 1;
  }
  /* c4 */
  c4=fgetc(fp);
  if (c4==EOF)
  {
   fprintf(fq,"Error: unexpected EOF in BASE64 code!\n");
   return 1;
  }
  if (strchr(base,c4)==NULL&&c4!='=')
  {
   fprintf(fq,"Error: %c : not BASE64 code!\n",c4);
   return 1;
  }
  /* check format:[A-Za-z0-9+/][A-Za-z0-9+/][A-Za-z0-9+/=][A-Za-z0-9+/=] */
  if (strchr(base,c1)&&strchr(base,c2)&&(strchr(base,c3)||c3=='=')&&
        ((c3=='='&&c4=='=')||(c3!='='&&(c4=='='||strchr(base,c4)))))
  {
   u.s.a=strchr(base,c1)-base;
   u.s.b=strchr(base,c2)-base;
   if (c3=='=')
   {
    /* one byte output */
    fprintf(fq,"%c",u.c[2]);
   }
   else
   if (c4=='=')
   {
    /* two byte output */
    u.s.c=strchr(base,c3)-base;
    fprintf(fq,"%c%c",u.c[2],u.c[1]);
   }
   else
   {
    /* three byte output */
    u.s.c=strchr(base,c3)-base;
    u.s.d=strchr(base,c4)-base;
    fprintf(fq,"%c%c%c",u.c[2],u.c[1],u.c[0]);
   }
  }
  else
  {
   fprintf(fq,"Error: %c%c%c%c : not BASE64 code!\n",c1,c2,c3,c4);
   return 1;
  }
 }
 return 0;
}
 
 
网上找到的,
保存到一个.c的文件,比如说base64.c,然后用gcc编译一下,就可以用来,gcc不指定输出,直接编译成一个a.out的文件,把a.out重命名成base64就行了。
 
 gcc base64.c -o base64 就行了。
 
加密解密测试:
 
[root@localhost ~]# echo "zhaoyun" | ./base64
emhhb3l1bgo=
[root@localhost ~]# echo "emhhb3l1bgo=" | ./base64 -u
zhaoyun
[root@localhost ~]#
 
 本文转自zhaoyun00 51CTO博客,原文链接:http://blog.51cto.com/zhaoyun/716839
 

转载地址:http://vqlxl.baihongyu.com/

你可能感兴趣的文章
C++ 输出到文本文件
查看>>
sql Lloader
查看>>
使用python学习【机器学习】需要安装的库~
查看>>
第一次作业+105032014098
查看>>
Codeforces 832B: Petya and Exam
查看>>
axios链接带参数_VUE升级(全面解析vuecil3/vuecil4的vue.config.js等常用配置,配置axios)...
查看>>
vue warning如何去掉_详解vue组件三大核心概念
查看>>
qt mysql md5加密_Qt 给密码进行MD5加密
查看>>
用java swing做连连看_java基于swing实现的连连看代码
查看>>
java关键字定义字符变量_Java 关键字和标识符
查看>>
oracle12 se1和se2,小屏党的最终执念,12Mini和SE2,到底谁更值得入手?
查看>>
WebRequest 超时不起作用
查看>>
OpenGL 函数作用列表
查看>>
12篇学通C#网络编程——第四篇 TCP应用编程
查看>>
ASP.NET MVC的DropDownList
查看>>
B-tree vs hash_我思故我在_百度空间
查看>>
spring.net 结合简单三层实例
查看>>
phpcms(v9)添加模型
查看>>
LINQ 图解
查看>>
状态目标bfs+哈希表 + 三杯水
查看>>