base64编码和解码
来源:互联网 发布:淘宝网日本忍者服装 编辑:程序博客网 时间:2024/05/17 21:51
写了个base64编解码程序,由于是自己写着玩的,所以写的比较随意,功能已经测试过了,都很正常。
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <assert.h>static int ZJF_Mail_int2char_FUN(int a){ if(a <= 25) { return 'A'+(a-0); } else if((a > 25)&&(a <= 51)) { return 'a'+(a-26); } else if((a > 51)&&(a < 61)) { return '0'+(a-52); } else if(a == '=') { return a; } else if(a == 62) { return '+'; } else if(a == 63) { return '/'; } return 0;}static int ZJF_Mail_char2int_FUN(char ch){ if((ch >= 'A')&&(ch <= 'Z')) { return ch-'A'; } else if((ch >= 'a')&&(ch <= 'z')) { return ch-'a'+26; } else if((ch >= '0')&&(ch <= '9')) { return ch - '0'+52; } else if(ch == '=') { return ch; } else if(ch == '+') { return 62; } else if(ch == '/') { return 63; } return 0;}static int ZJF_Mail_LittleToBig_FUN(char *str1,int len){ int i = 0; int temp = 0; for(i = 0; i < len; i++) { temp |= ((int)str1[i] << (len-i-1)*8); } //printf("%#x \n",temp); return temp;}static int ZJF_Mail_BigToLittle_FUN(char *str1,int len){ int i = 0; char temp[1024] = { 0 }; memcpy(temp,str1,len); for(i = 0; i < len; i++) { str1[i] = temp[len-i-1]; } return 0;}/*编码*/static int ZJF_Mail_Base64Encod_FUN(char * data,int len){ assert(data); assert(len>0); int i,j,k; char strtemp[4] = { 0 }; /*每次拷贝三个字节出来,存储*/ int temp = 0; char * pbuffer = (char * )malloc(len/3*4+4); for(i = 0,j = 0; i < len/3; i++) { memcpy(strtemp,data+3*i,3); temp = ZJF_Mail_LittleToBig_FUN(strtemp,3); pbuffer[j++] = (temp>>18)&0x3f; pbuffer[j++] = (temp>>12)&0x3f; pbuffer[j++] = (temp>>6)&0x3f; pbuffer[j++] = (temp>>0)&0x3f; if(j == 75) { //pbuffer[j++] = '\n'; } } if(len%3 == 1) { memcpy(strtemp,data+3*i,1); temp = (int)strtemp[0]; pbuffer[j++] = temp>>2; pbuffer[j++] = (temp&0x03)<<4; pbuffer[j++] = '='; pbuffer[j++] = '='; } else if(len%3 == 2) { memcpy(strtemp,data+3*i,2); temp = ZJF_Mail_LittleToBig_FUN(strtemp,2); pbuffer[j++] = temp>>10; pbuffer[j++] = (temp>>4)&0x3f; pbuffer[j++] = (temp<<2)&0x3f; pbuffer[j++] = '='; } pbuffer[j++] = '\0'; printf("编码数据:"); for(i = 0;i < j-1;i++) { printf("%c",ZJF_Mail_int2char_FUN(pbuffer[i])); } printf("\n"); if(pbuffer != NULL) { free(pbuffer); pbuffer = NULL; } return 0;}/*解码*/static int ZJF_Mail_Base64Decod_FUN(char * data,int len){ assert(data); assert(len>0); int i = 0; int j = 0; int temp = 0; int a = 0; char buf[1024] = { 0 }; char * pbuffer = (char * )malloc(len); for(i = 0;i < len;i++) { pbuffer[i] = ZJF_Mail_char2int_FUN(data[i]); } for(i = 0;i < len/4;i++) { temp = 0; for(j = 0;j < 4;j++) { a = 0; a = pbuffer[4*i+j]; /*先把等号处理掉*/ if(a == '=') { a = 0; } temp |= a<<((3-j)*6); } memcpy(buf+i*3,(char*)&temp,3); ZJF_Mail_BigToLittle_FUN(buf+3*i,3); } printf("解码后数据:%s \n",buf); if(NULL != pbuffer) { free(pbuffer); pbuffer = NULL; } return 0;}int main(){ int type = 0; int len = 0; char buf[1024] = { 0 }; char ch; printf("1: 编码\n2: 解码\n\n");INPUT: printf("请输入:"); scanf("%s",buf); fflush(stdin); type = atoi(buf); if((type != 1)&&(type != 2)) { printf("输入 %d 错误 ! 请输入 1 或者 2 \n请输入:",type); goto INPUT; } printf("%s初始化成功,请输入数据:",type == 1 ? "编码器":"解码器"); scanf("%s",buf); len = strlen(buf); switch(type) { case 1: /*编码*/ { ZJF_Mail_Base64Encod_FUN(buf,len); } break; case 2: /*解码*/ { ZJF_Mail_Base64Decod_FUN(buf,len); } break; default: printf("input %d is err ! please input 1 or 2 \n",type); } return 0;}
0 0
- base64编码和解码
- BASE64编码和解码
- base64编码和解码
- base64编码和解码
- BASE64 编码和解码
- Base64编码和解码
- BASE64编码和解码
- base64编码和解码
- BASE64编码和解码
- Base64编码和解码
- BASE64编码和解码
- base64编码和解码
- BASE64编码和解码
- BASE64编码和解码 .
- base64编码和解码
- base64编码和解码
- Base64编码和解码
- JavaScript Base64编码和解码
- [CF632E]选择
- 【NOIP2016提高A组8.11】钱仓
- Android中实现部分字体颜色改变的方式
- 析构函数定义为虚函数原因
- 字符集与编码
- base64编码和解码
- ProcessBar简单应用
- tomcat启动或者clean都报错 一种错
- 解决MySQL删除外键时报错
- 编程珠玑第一章第一题的解法,常用的排序
- shader笔记(2)
- shell脚本学习笔记之grep&正则表达式
- Spring AOP @Before @Around @After 等 advice 的执行顺序
- Android 创建单例模式的几种方法