Chinaunix首页 | 论坛 | 博客
  • 博客访问: 87507
  • 博文数量: 19
  • 博客积分: 487
  • 博客等级: 下士
  • 技术积分: 205
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-18 15:57
文章分类

全部博文(19)

文章存档

2011年(19)

我的朋友

分类: C/C++

2011-03-24 11:18:07

Base64.hpp
  1. #ifndef _BASE64_H
  2. #define _BASE64_H
  3. #include <string>

  4. namespace PWRD{
  5.     namespace Marshal{
  6.         class Base64{
  7.             public:
  8.                 static std::string encode(unsigned char const* , unsigned int len);
  9.                 static std::string decode(std::string const& s);
  10.             private:
  11.                 static std::string base64_chars;
  12.                 static bool is_base64(unsigned char c);
  13.         };
  14.     };

  15. };
  16. #endif

 

Base64.cpp

  1. #include "Base64.hpp"
  2. #include <iostream>

  3. namespace PWRD{
  4.     namespace Marshal{
  5.         std::string Base64::base64_chars =
  6.             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  7.             "abcdefghijklmnopqrstuvwxyz"
  8.             "0123456789+/";


  9.         bool Base64::is_base64(unsigned char c) {
  10.             return (isalnum(c) || (c == '+') || (c == '/'));
  11.         }

  12.         std::string Base64::encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
  13.             std::string ret;
  14.             int i = 0;
  15.             int j = 0;
  16.             unsigned char char_array_3[3];
  17.             unsigned char char_array_4[4];

  18.             while (in_len--) {
  19.                 char_array_3[i++] = *(bytes_to_encode++);
  20.                 if (i == 3) {
  21.                     char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  22.                     char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  23.                     char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  24.                     char_array_4[3] = char_array_3[2] & 0x3f;

  25.                     for(i = 0; (i <4) ; i++)
  26.                         ret += base64_chars[char_array_4[i]];
  27.                     i = 0;
  28.                 }
  29.             }

  30.             if (i)
  31.             {
  32.                 for(j = i; j < 3; j++)
  33.                     char_array_3[j] = '\0';

  34.                 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  35.                 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  36.                 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  37.                 char_array_4[3] = char_array_3[2] & 0x3f;

  38.                 for (j = 0; (j < i + 1); j++)
  39.                     ret += base64_chars[char_array_4[j]];

  40.                 while((i++ < 3))
  41.                     ret += '=';

  42.             }

  43.             return ret;

  44.         }

  45.         std::string Base64::decode(std::string const& encoded_string) {
  46.             int in_len = encoded_string.size();
  47.             int i = 0;
  48.             int j = 0;
  49.             int in_ = 0;
  50.             unsigned char char_array_4[4], char_array_3[3];
  51.             std::string ret;

  52.             while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  53.                 char_array_4[i++] = encoded_string[in_]; in_++;
  54.                 if (i ==4) {
  55.                     for (i = 0; i <4; i++)
  56.                         char_array_4[i] = base64_chars.find(char_array_4[i]);

  57.                     char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  58.                     char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  59.                     char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

  60.                     for (i = 0; (i < 3); i++)
  61.                         ret += char_array_3[i];
  62.                     i = 0;
  63.                 }
  64.             }

  65.             if (i) {
  66.                 for (j = i; j <4; j++)
  67.                     char_array_4[j] = 0;

  68.                 for (j = 0; j <4; j++)
  69.                     char_array_4[j] = base64_chars.find(char_array_4[j]);

  70.                 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  71.                 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  72.                 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

  73.                 for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
  74.             }

  75.             return ret;
  76.         }
  77.     };
  78. };
阅读(766) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~