Advertisement
Guest User

Untitled

a guest
Sep 28th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <string>
  4.  
  5. class Base64
  6. {
  7. public:
  8.  
  9.     static std::string base64_encode(unsigned char const*, unsigned int len);
  10.     static std::string base64_decode(std::string const& s);
  11. };
  12.  
  13. #include "Base64.h"
  14.  
  15. static const std::string base64_chars =
  16. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  17. "abcdefghijklmnopqrstuvwxyz"
  18. "0123456789+/";
  19.  
  20. static inline bool is_base64(unsigned char c) {
  21.     return (isalnum(c) || (c == '+') || (c == '/'));
  22. }
  23.  
  24. std::string Base64::base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
  25.     std::string ret;
  26.     int i = 0;
  27.     int j = 0;
  28.     unsigned char char_array_3[3];
  29.     unsigned char char_array_4[4];
  30.  
  31.     while (in_len--) {
  32.         char_array_3[i++] = *(bytes_to_encode++);
  33.         if (i == 3) {
  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.  
  39.             for (i = 0; (i < 4); i++)
  40.                 ret += base64_chars[char_array_4[i]];
  41.             i = 0;
  42.         }
  43.     }
  44.  
  45.     if (i)
  46.     {
  47.         for (j = i; j < 3; j++)
  48.             char_array_3[j] = '\0';
  49.  
  50.         char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  51.         char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  52.         char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  53.         char_array_4[3] = char_array_3[2] & 0x3f;
  54.  
  55.         for (j = 0; (j < i + 1); j++)
  56.             ret += base64_chars[char_array_4[j]];
  57.  
  58.         while ((i++ < 3))
  59.             ret += '=';
  60.  
  61.     }
  62.  
  63.     return ret;
  64.  
  65. }
  66.  
  67. std::string Base64::base64_decode(std::string const& encoded_string) {
  68.     int in_len = encoded_string.size();
  69.     int i = 0;
  70.     int j = 0;
  71.     int in_ = 0;
  72.     unsigned char char_array_4[4], char_array_3[3];
  73.     std::string ret;
  74.  
  75.     while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  76.         char_array_4[i++] = encoded_string[in_]; in_++;
  77.         if (i == 4) {
  78.             for (i = 0; i < 4; i++)
  79.                 char_array_4[i] = base64_chars.find(char_array_4[i]);
  80.  
  81.             char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  82.             char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  83.             char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  84.  
  85.             for (i = 0; (i < 3); i++)
  86.                 ret += char_array_3[i];
  87.             i = 0;
  88.         }
  89.     }
  90.  
  91.     if (i) {
  92.         for (j = i; j < 4; j++)
  93.             char_array_4[j] = 0;
  94.  
  95.         for (j = 0; j < 4; j++)
  96.             char_array_4[j] = base64_chars.find(char_array_4[j]);
  97.  
  98.         char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  99.         char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  100.         char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  101.  
  102.         for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
  103.     }
  104.  
  105.     return ret;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement