文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页网络冲浪网络技术 → 如何让base64加密解密代码?

如何让base64加密解密代码?

时间:2009/11/2 15:05:00来源:本站整理作者:我要评论(0)

#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)及解决方法双网卡同时上内外网设置教程公司网络综合布线图解