Advertisement
Guest User

crypto 01

a guest
Jun 26th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. //============================================================================
  2. // Name : Matasano01.cpp
  3. // Version : 1.1
  4. // Copyright : None
  5. // Description : Hex to base64 conversion
  6. //============================================================================
  7.  
  8. #include <iostream>
  9. #include <cmath>
  10. #include <vector>
  11. using namespace std;
  12.  
  13. static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I', 'J', 'K', 'L', 'M', 'N', 'O', 'P','Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X','Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f','g', 'h', 'i', 'j', 'k', 'l', 'm', 'n','o', 'p', 'q', 'r', 's', 't', 'u', 'v','w', 'x', 'y', 'z', '0', '1', '2', '3','4', '5', '6', '7', '8', '9', '+', '/'};
  14.  
  15. int hexToInt (char i)
  16. {
  17.     switch (i)
  18.     {
  19.         case '0': return 0;
  20.         case '1': return 1;
  21.         case '2': return 2;
  22.         case '3': return 3;
  23.         case '4': return 4;
  24.         case '5': return 5;
  25.         case '6': return 6;
  26.         case '7': return 7;
  27.         case '8': return 8;
  28.         case '9': return 9;
  29.         case 'a': return 10;
  30.         case 'b': return 11;
  31.         case 'c': return 12;
  32.         case 'd': return 13;
  33.         case 'e': return 14;
  34.         case 'f': return 15;
  35.     }
  36.     return 0;
  37. }
  38.  
  39. string decToBin(unsigned n)
  40. {
  41.     const size_t size = sizeof(n) * 8;
  42.     char result[size];
  43.     unsigned index = size;
  44.     do {result[--index] = '0' + (n & 1);}
  45.     while (n >>= 1);
  46.     return std::string(result + index, result + size);
  47. }
  48.  
  49. int binToInt(string input)
  50. {
  51.     int i = 0;
  52.     int total = 0;
  53.     string reversed = string(input.rbegin(), input.rend());
  54.     while (i <= input.length())
  55.     {
  56.         if (reversed[i] == '1') {
  57.             total = total + pow(2,i);
  58.         }
  59.         ++i;
  60.     }
  61.     return total;
  62. }
  63.  
  64. string binaryPadding (string input)
  65. {
  66.     while (input.length() != 4)
  67.     {
  68.         input.insert(0, "0");
  69.     }
  70.     return input;
  71. }
  72.  
  73. string encodeBase64 (string binary)
  74. {
  75.     string chunk;
  76.     string output;
  77.     int i = 0;
  78.     while (i < binary.length())
  79.     {
  80.         for (int a = 0; a < 6; a++)
  81.         {
  82.             chunk += (binary[i+a]);
  83.         }
  84.         i+=6;
  85.         output += encoding_table[binToInt(chunk)];
  86.         chunk = "";
  87.     }
  88.     return output;
  89. }
  90.  
  91. string hexToBase64 (string hex)
  92. {
  93.     int stringSize = hex.length();
  94.     int intConversion[stringSize];
  95.     std::vector<string> binConversion(stringSize);
  96.     string binString;
  97.     int counter = 0;
  98.  
  99.     while (counter < hex.length())
  100.     {
  101.         intConversion[counter] = hexToInt(hex[counter]);
  102.         binConversion[counter] = binaryPadding(decToBin(intConversion[counter]));
  103.         binString += binaryPadding(decToBin(intConversion[counter]));
  104.         ++counter;
  105.     }
  106.     return encodeBase64(binString);
  107. }
  108.  
  109. int main()
  110. {
  111.     //string input = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d";
  112.  
  113.     string input;
  114.     cout << "Enter what you'd like to base64.\n";
  115.     cin >> input;
  116.     cout << hexToBase64(input) << "\n\n";
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement