base64编解码

43 篇文章 0 订阅

以前写的编解码代码现在在项目中用上了,改了一些问题,由于以前的博客现在貌似不好修改了,所以重发一次

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define SN_SUCCESS   (0)
#define SN_ERROR     (-1)

char Int2Char(int P_nParam)
{
	if(P_nParam <= 25)
	{
		return 'A'+(P_nParam-0);
	}
	else if((P_nParam > 25)&&(P_nParam <= 51))
	{
		return 'a'+(P_nParam-26);
	}
	else if((P_nParam > 51)&&(P_nParam <= 61))
	{
		return '0'+(P_nParam-52);
	}
	else if(P_nParam == 62)
	{
		return '+';
	}
	else if(P_nParam == 63)
	{
		return '/';
	}
	else if(P_nParam == '=')
	{
		//return P_nParam;
	} 

	return SN_SUCCESS;
}

int Char2Int(char p_ch)
{
	if((p_ch >= 'A')&&(p_ch <= 'Z'))
	{
		return p_ch-'A';
	}
	else if((p_ch >= 'a')&&(p_ch <= 'z'))
	{
		return p_ch-'a'+26;
	}
	else if((p_ch >= '0')&&(p_ch <= '9'))
	{
		return p_ch - '0'+52;
	}
	else if(p_ch == '=')
	{
		return p_ch;
	}
	else if(p_ch == '+')
	{
		return 62;
	}
	else if(p_ch == '/')
	{
		return 63;
	}

	return SN_SUCCESS;
}


int LittleToBig(char *p_pStr,int p_nLen)
{
	int i    = 0;
	int nTemp = 0;
	for(i = 0; i < p_nLen; i++)
	{
		int nStr = p_pStr[i]&0xFF;
        nTemp |= (nStr << (p_nLen-i-1)*8);        
	}
	//Debug_Printf("%#x \n",temp); 
	return nTemp;
}

int BigToLittle(char *p_pStr,int p_nLen)
{
	int i           = 0;
	char temp[1024] = { 0 };
	memcpy(temp,p_pStr,p_nLen);

	for(i = 0; i < p_nLen; i++)
	{
		p_pStr[i] = temp[p_nLen-i-1]; 

	}   
	return SN_SUCCESS;
}

/*编码*/
int Base64Encod(char *p_pInStr, int p_nLen, char **p_pOutStr)
{
	assert(p_pInStr);
	assert(p_pOutStr);
	assert(p_nLen > 0);
	int i,j,k;
	char strtemp[4] = { 0 };  /*每次拷贝三个字节出来,存储*/
	int temp = 0;
	char * pbuffer = (char * )malloc(p_nLen/3*4+4+1);
	assert(pbuffer);
	memset(pbuffer,0, p_nLen/3*4+4+1);

	for(i = 0,j = 0; i < p_nLen/3; i++)
	{            
		memcpy(strtemp,p_pInStr+3*i,3);    
		temp = LittleToBig(strtemp,3);

		pbuffer[j++] = Int2Char((temp>>18)&0x3f);   
		pbuffer[j++] = Int2Char((temp>>12)&0x3f);  
		pbuffer[j++] = Int2Char((temp>>6)&0x3f);   
		pbuffer[j++] = Int2Char((temp>>0)&0x3f);

		if(j == 75)
		{
			pbuffer[j++] = Int2Char('\n');
		}
	}

	if(p_nLen%3 == 1)
	{
		memcpy(strtemp,p_pInStr+3*i,1);
		temp = (int)strtemp[0];     
		pbuffer[j++] = Int2Char(temp>>2);
		pbuffer[j++] = Int2Char((temp&0x03)<<4);
		pbuffer[j++] = '=';
		pbuffer[j++] = '=';
	}
	else if(p_nLen%3 == 2)
	{
		memcpy(strtemp,p_pInStr+3*i,2);
		temp = LittleToBig(strtemp,2);
		pbuffer[j++] = Int2Char(temp>>10);
		pbuffer[j++] = Int2Char((temp>>4)&0x3f);
		pbuffer[j++] = Int2Char((temp<<2)&0x3f);
		pbuffer[j++] = '=';
	}
	pbuffer[j] = '\0';
	*p_pOutStr = pbuffer;

	//printf("==== Base64Encod:\n%s\n \n", *p_pOutStr);

	return SN_SUCCESS;
}

/*解码*/
int Base64Decod(char *p_pInStr, int p_nLen, char *p_pOutStr, int p_nOutBuffLen, int *p_nOutDataLen)
{
	assert(p_pInStr);
	assert(p_nOutDataLen);
	assert(p_nLen > 0);
	assert(p_nOutBuffLen > 0);

	int i    = 0;
	int j    = 0;
	int temp = 0;
	int a = 0;
	int nDataLen = 0;
	int nEqualsNum = 0;
	char * pbuffer = (char * )malloc(p_nLen + 3);
	assert(pbuffer);
	int nRet = 0;

	memset(p_pOutStr, 0, p_nOutBuffLen);
	/*有的可能会省略"="号*/
	if(p_nLen%4 == 1)
	{
		printf("err base64 fomat !!!\n");
		return SN_ERROR;
	}
	else if(p_nLen%4 > 1)
	{
		nEqualsNum = (4 - p_nLen%4);
		p_nLen += nEqualsNum;
	}

	//printf("======================input data len = %d:\n", p_nLen);
	for(i = 0;i < p_nLen;i++)
	{   	
		/*把填充的=号处理掉*/
		if(p_pInStr[i] == '=')
		{
			p_pInStr[i] = 0;
			continue;
		}
		pbuffer[i] = Char2Int(p_pInStr[i]);
	}

	for(i = 0;i < p_nLen/4;i++)
	{
		temp = 0;
		for(j = 0;j < 4;j++)
		{   
			a = 0;
			a = pbuffer[4*i+j];			           
			temp |= a<<((3-j)*6);
		}

		if(nDataLen > p_nOutBuffLen - 1 - 3)
		{
			nRet = SN_ERROR;
			printf("Not enough memory !!! \n");
			break;
		}

		memcpy(p_pOutStr+i*3,(char*)&temp,3);
		BigToLittle(p_pOutStr+3*i,3);
		nDataLen += 3;	
	}
	*p_nOutDataLen = (nDataLen - nEqualsNum);
	printf("Base64Decod Data Len = %d \n",*p_nOutDataLen);

	/*for(int i = 0; i < *p_nOutDataLen; ++i)
	{
		if(i%16 == 0)
		{
			printf("\n");
		}
		else if((i%4 == 0)&&(i != 0))
		{
			printf("    ");
		}

		printf("%02x ", p_pOutStr[i]);
	}
	printf("\n");*/

	if(NULL != pbuffer)
	{
		free(pbuffer);
		pbuffer = NULL;
	}

	return nRet;
}


int main()
{
    int type = 0;
    int len  = 0;
    char buf[1024] = { 0 };
	char *outBuf = NULL;
	int outLen = 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: /*编码*/
        {
            Base64Encod(buf, len, &outBuf);
			printf("Encod: %s \n", outBuf);
        }
        break;
        case 2: /*解码*/
        {
			outBuf = (char *)malloc(2024);
            Base64Decod(buf,len, outBuf, 2024, &outLen);
			printf("Decod: %s \n", outBuf);
        }
        break;
        default:
            printf("input %d is err ! please input 1 or 2 \n",type);
    }

	if(outBuf)
	{
		free(outBuf);
		outBuf = NULL;
	}
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿木小呆呆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值