c语言 base64编码实例,base64编码(c语言实现)(示例代码)

#include

#include

#include

#include

char base64_table[] = {

‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,‘H‘,‘I‘,‘J‘,

‘K‘,‘L‘,‘M‘,‘N‘,‘O‘,‘P‘,‘Q‘,‘R‘,‘S‘,‘T‘,

‘U‘,‘V‘,‘W‘,‘X‘,‘Y‘,‘Z‘,‘a‘,‘b‘,‘c‘,‘d‘,

‘e‘,‘f‘,‘g‘,‘h‘,‘i‘,‘j‘,‘k‘,‘l‘,‘m‘,‘n‘,

‘o‘,‘p‘,‘q‘,‘r‘,‘s‘,‘t‘,‘u‘,‘v‘,‘w‘,‘x‘,

‘y‘,‘z‘,‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,

‘8‘,‘9‘,‘+‘, ‘-‘, ‘\0‘

};

void base64_map(uint8_t *in_block, int len) {

for(int i = 0; i < len; ++i) {

in_block[i] = base64_table[in_block[i]];

//printf("%d %c",in_block[i], base64_table[in_block[i]]);

}

if(len % 4 == 3)

in_block[len] = ‘=‘;

else if(len % 4 == 2)

in_block[len] = in_block[len+1] = ‘=‘;

return ;

}

void base64_unmap(char *in_block) {

int i;

char *c;

for(int i = 0; i < 4; ++i) {

c = in_block + i;

if(*c>=‘A‘ && *c<=‘Z‘) {

*c -= ‘A‘;

continue;

}

if(*c>=‘a‘ && *c<=‘z‘) {

*c -= ‘a‘;

*c += 26;

continue;

}

if(*c == ‘+‘) {

*c = 62;

continue;

}

if(*c == ‘/‘) {

*c = 63;

continue;

}

if(*c == ‘=‘) {

*c = 0;

continue;

}

*c -= ‘0‘;

*c += 52;

}

}

int base64_encode(char *in, int inlen, uint8_t *out) {

char *in_block;

uint8_t *out_block;

char temp[3];

out_block = out;

in_block = in;

for(int i = 0; i < inlen; i += 3) {

memset(temp, 0, 3);

memcpy(temp, in_block, i + 3 < inlen ? 3 : inlen - i);

memset(out_block, 0, 4);

out_block[0] = (temp[0] >> 2) & 0x3f;

out_block[1] = ((temp[0] << 4) & 0x30) | ((temp[1] >> 4) & 0x0f);

out_block[2] = ((temp[1] << 2) & 0x3c) | ((temp[2] >> 6) & 0x03);

out_block[3] = (temp[2]) & 0x3f;

//printf("%.2x %.2x %.2x\n", temp[0], temp[1], temp[2]);

//printf("%.2x %.2x %.2x %.2x\n", out_block[0], out_block[1], out_block[2], out_block[3]);

out_block += 4;

in_block += 3;

}

base64_map(out, ((inlen * 4) - 1) / 3 + 1);

}

int base64_decode(char *in, int inlen, uint8_t *out) {

char *in_block;

uint8_t *out_block;

char temp[4];

out_block = out;

in_block = in;

for(int i = 0; i < inlen; i += 4) {

if(*in_block == ‘=‘)

return 0;

memcpy(temp, in_block, 4);

memset(out_block, 0, 3);

base64_unmap(temp);

out_block[0] = ((temp[0]<<2) & 0xfc) | ((temp[1]>>4) & 3);

out_block[1] = ((temp[1]<<4) & 0xf0) | ((temp[2]>>2) & 0xf);

out_block[2] = ((temp[2]<<6) & 0xc0) | ((temp[3] ) & 0x3f);

out_block += 3;

in_block +=4;

}

return 0;

}

int main() {

char cipher_text[64];

while(scanf("%s", cipher_text) != EOF) {

printf("%s\n", cipher_text);

uint8_t *tran_cipher = (uint8_t *)malloc(sizeof(uint8_t) * 64);

memset(tran_cipher, 0, sizeof(uint8_t) * 64);

#define ENCODE

#define DECODE

#ifdef ENCODE

printf("----------------ENCODE-----------------");

base64_encode(cipher_text, strlen(cipher_text), tran_cipher);

int len = (strlen(cipher_text) * 4 - 1) / 3 + 1;

len = len % 4 == 3 ? len + 1 : len + 2;

for(int i = 0; i < len ; ++i)

printf("%c", tran_cipher[i]);

printf("\n");

#endif // ENCODE

#ifdef DECODE

printf("----------------DECODE-----------------");

base64_decode(cipher_text, strlen(cipher_text), tran_cipher);

len = strlen(cipher_text);

int n = len;

while(cipher_text[--n] == ‘=‘)

;

if(n == len-2)

len = (len >> 2) * 3 - 1;

else if(n == len-3)

len = (len >> 2) * 3 - 2;

else if(n == len-1)

len = (len >> 2) * 3;

for(int i = 0; i < len; ++i)

printf("%c", tran_cipher[i]);

printf("\n");

#endif // DECODE

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力回答您的问题。在C语言中,封装可以通过结构体来实现。结构体中的成员变量可以被定义为私有的,只能通过结构体提供的接口函数来访问。继承可以通过结构体嵌套来实现,子结构体可以继承父结构体中的成员变量和函数指针,并可以添加自己的成员变量和函数指针。多态可以通过函数指针来实现,父结构体中的函数指针可以指向不同的子结构体中的同名函数,从而实现多态。 下面是一个示例代码,演示了如何在C语言实现封装、继承和多态的特性: ```c #include <stdio.h> // 父结构体 typedef struct { int x; } Base; // 子结构体 typedef struct { Base base; // 嵌套父结构体 int y; } Derived; // 父结构体的接口函数 void setX(Base *base, int x) { base->x = x; } int getX(Base *base) { return base->x; } // 子结构体的接口函数 void setY(Derived *derived, int y) { derived->y = y; } int getY(Derived *derived) { return derived->y; } // 多态的函数 void printX(Base *base) { printf("%d\n", base->x); } int main() { Derived d; setX(&d.base, 1); setY(&d, 2); printf("%d %d\n", getX(&d.base), getY(&d)); printX(&d.base); return 0; } ``` 在上面的示例代码中,我们定义了一个父结构体`Base`和一个子结构体`Derived`。`Derived`结构体嵌套了`Base`结构体,并添加了自己的成员变量`y`。我们也定义了一些接口函数来操作这些成员变量。`printX`函数是一个多态的函数,可以接受`Base`结构体的指针作为参数,实现了多态的特性。 希望这个示例代码可以帮助您更好地理解如何在C语言实现封装、继承和多态的特性。如果还有其他问题,请随时询问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值