#include <stdio.h>
#define CHAR_BIT 8
#define BITMASK(b) (1 << ((b) % CHAR_BIT))
#define BITSLOT(b) ((b) / CHAR_BIT)
#define BITSET(a, b) ((a)[BITSLOT(b)] |= BITMASK(b))
#define BITTEST(a, b) ((a)[BITSLOT(b)] & BITMASK(b))
// Returns the length of the base64 string
int base64_encode(unsigned char str[], unsigned char base64[], int len)
{
int idx=0,idx2=0,base64_chars=0;
unsigned char charset[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"},t=0;
// Initialize the base64 array
memset(base64,0,(len+1) * 1.334);
// Continue converting while the bit limit for the str array has not been reached
while (idx < len * CHAR_BIT) {
// Pick off bits in chunks of 6
for (idx2=0; idx2 < 6; idx++,idx2++) {
if (BITTEST(str,idx))
BITSET(&t,idx2);
}
// Map the binary value to its ASCII representation
t = charset[t];
// Save the new converted char to the base64 array, increment the base64 len counter,
// and re-initialize the temp var
base64[base64_chars] = t;
base64_chars++;
t = 0;
}
// Return the number of Base64 characters that were used
return(base64_chars);
}
// Returns the length of the plain text string
int base64_decode(unsigned char base64_t[], unsigned char str[], int len)
{
int idx=0,idx2=0,idx3,str_chars=0;
unsigned char charset[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"},t=0,base64[1000];
// Initialize the str array
memset(str,0,(len+1) * .75);
// Un-map the ASCII characters to their binary representations
for (idx=0; idx < len; idx++) {
for (idx3=0; 1; idx3++) {
if (charset[idx3] == base64_t[idx]) {
base64[idx] = idx3;
break;
}
}
}
// Combine the significant 6-bit blocks from each byes of the base64 array
for (idx=0,idx2=0; idx < len * CHAR_BIT; idx++,idx2++) {
// Skip two "lame" bits ever six
if (!(idx2 % 6) && idx2)
idx += 2;
// Log how many full 8-bit chars have been filled
if (!(idx2 % 8) && idx2)
str_chars++;
if (BITTEST(base64,idx))
BITSET(str,idx2);
}
// Return the length of the text array
return(str_chars);
}
int main()
{
unsigned char str[100]={"sadfymkf7 6756"},base[100],t;
t = base64_encode(str,base,strlen(str));
printf("\n");
t = base64_decode(base,str,t);
getchar();
}
相关视频
相关阅读 GNOME glib库的Base64编码解码 多个整数溢出漏洞如何用asp进行base64加密奇妙的Base64编码NBA2K20配置要求高吗 NBA2K20配置要求推荐LOL云顶之弈各英雄如何搭配装备 云顶之弈全英雄装备搭配推荐NBA2K20普通和豪华版区别 NBA2K20豪华版预购奖励介绍NBA2K20什么时候出 NBA2K20发售时间一览瘟疫传说无罪配置要求如何 瘟疫传说无罪配置要求推荐
热门文章 小米路由器设置教程附共享有线路由后再接无TP-link无线路由器设置D-Link DI-524M路由器
最新文章
百度网盘解除黑名单摆百度不收录怎么办 百度
10款免费开源图表插件推荐ssid隐藏了怎么办?隐藏SSID的无线网络如何OneDNS设置教程两块网卡访问不同网络案例分享
人气排行 宽带连接图标不见了怎么办 宽带连接图标怎么dell 服务器开机总是提示按F1才能进系统解决dns是什么?dns怎么设置?buffalo无线路由器设置图文教程哪种WIFI无线各种加密方式更安全?ADSL宽带连接错误(720)及解决方法双网卡同时上内外网设置教程公司网络综合布线图解
查看所有0条评论>>