首页 > 其他分享 >密码学——Base64编码

密码学——Base64编码

时间:2022-12-31 14:33:10浏览次数:54  
标签:编码 strInput Base64 unsigned char nNewLength 字符 密码学 strOutput

通过数据表替换,将6位拓展为8位,进行替换,总长度增加1/3。

 

#pragma once

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


unsigned char* Base64_Encode(unsigned char* strInput, int nLength,int *nNewLength) {
	unsigned char* p = strInput;
	
	//定义base64编码表  
    unsigned char* base64_table = (unsigned char*)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    int str_len = sizeof(p);

    //计算经过base64编码后的字符串长度  
    if (nLength % 3 == 0)
        *nNewLength = nLength / 3 * 4;
    else
        *nNewLength = (nLength / 3 + 1) * 4;

	unsigned char* strOutput;
    strOutput = (unsigned char*)malloc(sizeof(unsigned char) * *nNewLength + 1);
    strOutput[*nNewLength] = '\0';

    //以3个8位字符为一组进行编码  
    int i = 0, j = 0;
    for (; i < *nNewLength - 2; j += 3, i += 4)
    {
        strOutput[i] = base64_table[strInput[j] >> 2]; //取出第一个字符的前6位并找出对应的结果字符  
        strOutput[i + 1] = base64_table[(strInput[j] & 0x3) << 4 | (strInput[j + 1] >> 4)]; //将第一个字符的后2位与第二个字符的前4位进行组合并找到对应的结果字符  
        strOutput[i + 2] = base64_table[(strInput[j + 1] & 0xf) << 2 | (strInput[j + 2] >> 6)]; //将第二个字符的后4位与第三个字符的前2位组合并找出对应的结果字符  
        strOutput[i + 3] = base64_table[strInput[j + 2] & 0x3f]; //取出第三个字符的后6位并找出结果字符  
    }

    switch (nLength % 3)
    {
    case 1:
        strOutput[i - 2] = '=';
        strOutput[i - 1] = '=';
        break;
    case 2:
        strOutput[i - 1] = '=';
        break;
    }


    return strOutput;

}


unsigned char* Base64_Decode(unsigned char* strInput, int nLength, int* nNewLength) {
        //根据base64表,以字符找到对应的十进制数据  
        int table[] = { 0,0,0,0,0,0,0,0,0,0,0,0,
                 0,0,0,0,0,0,0,0,0,0,0,0,
                 0,0,0,0,0,0,0,0,0,0,0,0,
                 0,0,0,0,0,0,0,62,0,0,0,
                 63,52,53,54,55,56,57,58,
                 59,60,61,0,0,0,0,0,0,0,0,
                 1,2,3,4,5,6,7,8,9,10,11,12,
                 13,14,15,16,17,18,19,20,21,
                 22,23,24,25,0,0,0,0,0,0,26,
                 27,28,29,30,31,32,33,34,35,
                 36,37,38,39,40,41,42,43,44,
                 45,46,47,48,49,50,51
        };

        //判断编码后的字符串后是否有=  
        if (strstr((char*)strInput, "=="))
            *nNewLength = nLength / 4 * 3 - 2;
        else if (strstr((char*)strInput, "="))
            *nNewLength = nLength / 4 * 3 - 1;
        else
            *nNewLength = nLength / 4 * 3;

        unsigned char* strOutput;
        strOutput = (unsigned char*)malloc(sizeof(unsigned char) * *nNewLength + 2);//防止内存越界
        strOutput[*nNewLength] = '\0';
    
        //以4个字符为一位进行解码  
        int i = 0, j = 0;
        for (; i < nLength - 2; j += 3, i += 4)
        {
            strOutput[j] = ((unsigned char)table[strInput[i]]) << 2 | (((unsigned char)table[strInput[i + 1]]) >> 4); //取出第一个字符对应base64表的十进制数的前6位与第二个字符对应base64表的十进制数的后2位进行组合  
            strOutput[j + 1] = (((unsigned char)table[strInput[i + 1]]) << 4) | (((unsigned char)table[strInput[i + 2]]) >> 2); //取出第二个字符对应base64表的十进制数的后4位与第三个字符对应bas464表的十进制数的后4位进行组合  
            strOutput[j + 2] = (((unsigned char)table[strInput[i + 2]]) << 6) | ((unsigned char)table[strInput[i + 3]]); //取出第三个字符对应base64表的十进制数的后2位与第4个字符进行组合  
        }

        return strOutput;
}

 

标签:编码,strInput,Base64,unsigned,char,nNewLength,字符,密码学,strOutput
From: https://www.cnblogs.com/wuruixin/p/17016624.html

相关文章