Base64算法的资料整理(VC代码与ASM代码)

什么是Base64?
按照RFC2045的定义,Base64被定义为:Base64内容传送编码被设计用来把任意序列的8位字节描述为一种不易被人直接识别的形式。(The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.)

具体的见压缩包吧!
VC的实现是我修改过的,调用方法见头文件:Base64.h


/*************************************************************

* $Id: base64.c,v 1.2 2004/02/04 02:17:32 hno Exp $
*  2009/03/22 iawen修正了对中文支持的一些BUG


使用示例[VC]:
1、获取需要加密的字符串:
  char srcString[BASE64_RESULT_SZ];
  memset(srcString,0,BASE64_RESULT_SZN);
  GetDlgItemText(hDlg,IDC_SRC,srcString,BASE64_RESULT_SZ-1);

2、调用相应函数,并接收返回的值:
  char destString[BASE64_RESULT_SZ];
  memset(destString,0,BASE64_RESULT_SZ);
  Base64Init();
  Base64Encode(srcString,destString);
  SetDlgItemText(hDlg,IDC_DEST,destString);

**************************************************************/

#define __IAWEN_BASE64_H__

#ifdef __IAWEN_BASE64_H__

  #define BASE64_VALUE_SZ 256
  #define BASE64_RESULT_SZ 8192

  extern char base64_code[];
  void Base64Init();  //初始化设置
  void Base64Decode(const char *srcString,char *destString);
      //对字符串进行解密
      //  srcString:    指向需要加密的字符串
      //  destString:  保存加密后的字符串,用以返回
                    
  void Base64Encode(const char *srcString,char *destString);
      //对字符串进行加密
      //  srcString:    指向加密后的字符串
      //  destString:  保存解密后的字符串,用以返回

#endif

Base64.cpp的具体代码也如下:


/*
* $Id: base64.c,v 1.2 2004/02/04 02:17:32 hno Exp $
*  2009/03/22 iawen修正了对中文支持的一些BUG
*/


#include
#include"Base64.h"

int base64_value[BASE64_VALUE_SZ];
char base64_code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

void Base64Init(){
    int i;

    for (i = 0; i < BASE64_VALUE_SZ; i++)
    base64_value[i] = -1;

    for (i = 0; i < 64; i++)
    base64_value[(int) base64_code[i]] = i;
    base64_value['='] = 0;
}

void Base64Decode(const char *srcString,char *destString){
    int j;
    int c;
    long val;
    if (!srcString)
    return;

    val = c = 0;
    for (j = 0; *srcString && j + 4 < BASE64_RESULT_SZ; srcString++) {
    unsigned char ch=((unsigned char) *srcString);
    if(ch=='/')    //修正部分一:当加密字符为'/'时,替换值为0
      ch=0;
    unsigned int k = ch % BASE64_VALUE_SZ;
    if (base64_value[k] < 0)
      continue;
    val <<= 6;
    val += base64_value[k];
    if (++c < 4)
      continue;
    /* One quantum of four encoding characters/24 bit */
    destString[j++] = (val >> 16) & 0xff;  /* High 8 bits */
    destString[j++] = (val >> 8) & 0xff;  /* Mid 8 bits */
    destString[j++] = val & 0xff;  /* Low 8 bits */
    val = c = 0;
    }
    destString[j] = 0;
    return;
}

/* adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments */
void Base64Encode(const char *srcString,char *destString){
    int bits = 0;
    int char_count = 0;
    int out_cnt = 0;
    int c;

    if (!srcString)
    return;

    while ((c = (unsigned char) *srcString++) && out_cnt < sizeof(destString) - 5) {
    bits += c;
    char_count++;
    if (char_count == 3) {//修正部分二:当操作结果为0时,用'/'替换
      char tmp=base64_code[bits >> 18];
      if(tmp==0)
        destString[out_cnt++] ='/' ;
      else
        destString[out_cnt++] =tmp;

      tmp=base64_code[(bits >> 12) & 0x3f];
      if(tmp==0)
        destString[out_cnt++] ='/' ;
      else
        destString[out_cnt++] =tmp;

      tmp=base64_code[(bits >> 6) & 0x3f];
      if(tmp==0)
        destString[out_cnt++] ='/' ;
      else
        destString[out_cnt++] =tmp;
      
      tmp=base64_code[bits & 0x3f];
      if(tmp==0)
        destString[out_cnt++] ='/' ;
      else
        destString[out_cnt++] =tmp;

      bits = 0;
      char_count = 0;
    } else {
      bits <<= 8;
    }
    }
    if (char_count != 0) {
    bits <<= 16 - (8 * char_count);
    destString[out_cnt++] = base64_code[bits >> 18];
    destString[out_cnt++] = base64_code[(bits >> 12) & 0x3f];
    if (char_count == 1) {
      destString[out_cnt++] = '=';
      destString[out_cnt++] = '=';
    } else {
      destString[out_cnt++] = base64_code[(bits >> 6) & 0x3f];
      destString[out_cnt++] = '=';
    }
    }
    destString[out_cnt] = '\0';  /* terminate */
    return;
}

在使用时,只需要包含:base64.h这个头文件,就可以如示例般调用了!
ASM的实现是网上收集的,大家自己看吧,没有具体测试过。

这里将这些放在一起,是方便自己的查找与整理!
[file][attach]245[/attach][/file]

发表评论