bird1000000

Untitled

Dec 8th, 2020 (edited)
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <stdint.h>
  4. #include <string>
  5. #include <vector>
  6.  
  7. std::map<int, char> intToChar = //67 is prime
  8. {
  9.     {0, 'A'},
  10.     {1, 'B'},
  11.     {2, 'C'},
  12.     {3, 'D'},
  13.     {4, 'E'},
  14.     {5, 'F'},
  15.     {6, 'G'},
  16.     {7, 'H'},
  17.     {8, 'I'},
  18.     {9, 'J'},
  19.     {10, 'K'},
  20.     {11, 'L'},
  21.     {12, 'M'},
  22.     {13, 'N'},
  23.     {14, 'O'},
  24.     {15, 'P'},
  25.     {16, 'Q'},
  26.     {17, 'R'},
  27.     {18, 'S'},
  28.     {19, 'T'},
  29.     {20, 'U'},
  30.     {21, 'V'},
  31.     {22, 'W'},
  32.     {23, 'X'},
  33.     {24, 'Y'},
  34.     {25, 'Z'},
  35.     {26, 'a'},
  36.     {27, 'b'},
  37.     {28, 'c'},
  38.     {29, 'd'},
  39.     {30, 'e'},
  40.     {31, 'f'},
  41.     {32, 'g'},
  42.     {33, 'h'},
  43.     {34, 'i'},
  44.     {35, 'j'},
  45.     {36, 'k'},
  46.     {37, 'l'},
  47.     {38, 'm'},
  48.     {39, 'n'},
  49.     {40, 'o'},
  50.     {41, 'p'},
  51.     {42, 'q'},
  52.     {43, 'r'},
  53.     {44, 's'},
  54.     {45, 't'},
  55.     {46, 'u'},
  56.     {47, 'v'},
  57.     {48, 'w'},
  58.     {49, 'x'},
  59.     {50, 'y'},
  60.     {51, 'z'},
  61.     {52, ' '},
  62.     {53, '.'},
  63.     {54, ','},
  64.     {55, '?'},
  65.     {56, '!'},
  66.     {57, ':'},
  67.     {58, ';'},
  68.     {59, '@'},
  69.     {60, '$'},
  70.     {61, '%'},
  71.     {62, '/'},
  72.     {63, '+'},
  73.     {64, '='},
  74.     {65, '('},
  75.     {66, ')'},
  76. };
  77.  
  78.  
  79. std::map<char, int> charToInt = //67 is prime
  80. {
  81.     {'A', 0},
  82.     {'B', 1},
  83.     {'C', 2},
  84.     {'D', 3},
  85.     {'E', 4},
  86.     {'F', 5},
  87.     {'G', 6},
  88.     {'H', 7},
  89.     {'I', 8},
  90.     {'J', 9},
  91.     {'K', 10},
  92.     {'L', 11},
  93.     {'M', 12},
  94.     {'N', 13},
  95.     {'O', 14},
  96.     {'P', 15},
  97.     {'Q', 16},
  98.     {'R', 17},
  99.     {'S', 18},
  100.     {'T', 19},
  101.     {'U', 20},
  102.     {'V', 21},
  103.     {'W', 22},
  104.     {'X', 23},
  105.     {'Y', 24},
  106.     {'Z', 25},
  107.     {'a', 26},
  108.     {'b', 27},
  109.     {'c', 28},
  110.     {'d', 29},
  111.     {'e', 30},
  112.     {'f', 31},
  113.     {'g', 32},
  114.     {'h', 33},
  115.     {'i', 34},
  116.     {'j', 35},
  117.     {'k', 36},
  118.     {'l', 37},
  119.     {'m', 38},
  120.     {'n', 39},
  121.     {'o', 40},
  122.     {'p', 41},
  123.     {'q', 42},
  124.     {'r', 43},
  125.     {'s', 44},
  126.     {'t', 45},
  127.     {'u', 46},
  128.     {'v', 47},
  129.     {'w', 48},
  130.     {'x', 49},
  131.     {'y', 50},
  132.     {'z', 51},
  133.     {' ', 52},
  134.     {'.', 53},
  135.     {',', 54},
  136.     {'?', 55},
  137.     {'!', 56},
  138.     {':', 57},
  139.     {';', 58},
  140.     {'@', 59},
  141.     {'$', 60},
  142.     {'%', 61},
  143.     {'/', 62},
  144.     {'+', 63},
  145.     {'=', 63},
  146.     {'(', 63},
  147.     {')', 63},
  148.  
  149. };
  150.  
  151.  
  152. struct KeyMatrix {
  153.     int a, b, c;
  154.     int d, e, f;
  155.     int g, h, i;
  156.  
  157.     int det = 0;
  158. };
  159.  
  160. struct MessageMatrix {
  161.     int a;
  162.     int b;
  163.     int c;
  164. };
  165.  
  166.  
  167. // % operator is remainder operator, not modulous.
  168. int mathMod(int a) {
  169.  
  170.     // Number of characters in charset
  171.     int numChar = intToChar.size();
  172.  
  173.     if (a >= 0) {
  174.         return a % numChar;
  175.     }
  176.     else {
  177.         return numChar - (-(a%numChar));
  178.     }
  179.  
  180.  
  181. }
  182.  
  183. MessageMatrix EncryptMessage(KeyMatrix key, MessageMatrix msg){
  184.     MessageMatrix enc;
  185.  
  186.     enc.a = (key.a * msg.a) + (key.b * msg.b) + (key.c * msg.c);
  187.     enc.b = (key.d * msg.a) + (key.e * msg.b) + (key.f * msg.c);
  188.     enc.c = (key.g * msg.a) + (key.h * msg.b) + (key.i * msg.c);
  189.  
  190.     enc.a = mathMod(enc.a);
  191.     enc.b = mathMod(enc.b);
  192.     enc.c = mathMod(enc.c);
  193.  
  194.     return enc;
  195. }
  196.  
  197. int getInverseModulus(int det) {
  198.    
  199.     double mod = (double)(intToChar.size() + 1)/(double)det;
  200.     double intPart = 0;
  201.     double fracpart = modf(mod, &intPart);
  202.  
  203.     if (fracpart != 0) {
  204.         std::cout << "Error! det is does not work!";
  205.         std::getchar();
  206.         throw std::exception("Error! Det!");
  207.     }
  208.  
  209.     return mod;
  210. }
  211.  
  212. MessageMatrix DecryptMessage(KeyMatrix ikey, MessageMatrix enc) {
  213.  
  214.     MessageMatrix dec;
  215.  
  216.     //constant
  217.     int c = getInverseModulus(ikey.det);
  218.  
  219.     dec.a = c * ((ikey.a * enc.a) + (ikey.b * enc.b) + (ikey.c * enc.c));
  220.     dec.b = c * ((ikey.d * enc.a) + (ikey.e * enc.b) + (ikey.f * enc.c));
  221.     dec.c = c * ((ikey.g * enc.a) + (ikey.h * enc.b) + (ikey.i * enc.c));
  222.  
  223.     dec.a = mathMod(dec.a);
  224.     dec.b = mathMod(dec.b);
  225.     dec.c = mathMod(dec.c);
  226.  
  227.  
  228.     return dec;
  229. }
  230.  
  231.  
  232.  
  233. // https://en.wikipedia.org/wiki/Invertible_matrix#Inversion_of_3_%C3%97_3_matrices
  234. KeyMatrix invertKey(KeyMatrix mat) {
  235.  
  236. //[A B C]
  237. //[D E F]
  238. //[H I J]
  239.  
  240. //[A D G]
  241. //[B E H]
  242. //[C F I]
  243.  
  244.  
  245.     KeyMatrix imat;
  246.  
  247.     imat.a = (mat.e * mat.i - mat.f * mat.h);   // A = (ei-fh)
  248.     imat.b = -(mat.b * mat.i - mat.c * mat.h);  // D = -(bi-ch)
  249.     imat.c = (mat.b * mat.f - mat.c * mat.e);   // G = (bf-ce)
  250.  
  251.     imat.d = -(mat.d * mat.i - mat.f * mat.g);  // B = -(di -fg)
  252.     imat.e = (mat.a * mat.i - mat.c * mat.g);   // E = (ai-cg)
  253.     imat.f = -(mat.a * mat.f - mat.c * mat.d);  // H = -(af-cd)
  254.  
  255.     imat.g = (mat.d * mat.h - mat.e * mat.g);   // C = (dh-eg)
  256.     imat.h = -(mat.a * mat.h - mat.b * mat.g);  // F = -(ah-bg)
  257.     imat.i = (mat.a * mat.e - mat.b * mat.d);   // I = (ae-bd)
  258.  
  259.     //aA + bD + cG
  260.     //(a*(ei-fh)) + (b(-(di-fg))) + (c(dh-eg)) = 34
  261.     //(aei-afh -bdi+bfg + cdg-ceg)
  262.    
  263.     imat.det = (mat.a * imat.a) + (mat.b * imat.d) + (mat.c * imat.g);  // det = aA + bB + cC but we rotated the matrix
  264.  
  265.     if (imat.det == 0) {
  266.         std::cout << "Error! det is 0! This key will not work!\n";
  267.         std::getchar();
  268.         throw std::exception("Error! Det!");
  269.     }
  270.  
  271.     return imat;
  272.    
  273. }
  274.  
  275. std::vector<int> strToInts(std::string text) {
  276.  
  277.     std::vector<int> ret = {};
  278.  
  279.     for (int i = 0; i < text.length(); i++) {
  280.         ret.push_back(charToInt.at(text[i]));
  281.     }
  282.  
  283.     return ret;
  284. }
  285.  
  286. std::string intsToStr(std::vector<int> numbs) {
  287.  
  288.     std::string ret = "";
  289.  
  290.     for (int i = 0; i < numbs.size(); i++) {
  291.         ret += intToChar.at(numbs[i]);
  292.     }
  293.  
  294.     return ret;
  295. }
  296.  
  297.  
  298. int main()
  299. {
  300.     std::cout << "Matrix Encoder & Decoder!\n";
  301.     std::cout << "k: Set the key (3x3 matric eg) 2 4 8 2 1 3 5 8 4\n";
  302.     std::cout << "e: Encrypt message\n";
  303.     std::cout << "d: Decrypt message\n";
  304.     std::cout << "ci: Convert message to array of ints\n";
  305.     std::cout << "cs: Convert array of ints to string\n";
  306.     std::cout << "l: Mod list\n";
  307.  
  308.  
  309.     KeyMatrix key;
  310.     key.a = 2;
  311.     key.b = 3;
  312.     key.c = 5;
  313.  
  314.     key.d = 14;
  315.     key.e = 8;
  316.     key.f = 12;
  317.    
  318.     key.g = 7;
  319.     key.h = 7;
  320.     key.i = 10;
  321.  
  322.     MessageMatrix msg;
  323.     msg.a = charToInt.at('E');
  324.     msg.b = charToInt.at('G');
  325.     msg.c = charToInt.at('G');
  326.  
  327.     MessageMatrix enc = EncryptMessage(key, msg);
  328.  
  329.     MessageMatrix dec = DecryptMessage(invertKey(key), enc);
  330.  
  331.     std::vector<int> DecodedInts;
  332.     DecodedInts.push_back(dec.a);
  333.     DecodedInts.push_back(dec.b);
  334.     DecodedInts.push_back(dec.c);
  335.  
  336.     std::string DecodedMsg = intsToStr(DecodedInts);
  337.    
  338.  
  339.     //KeyMatrix invKey = invertKey(key);
  340.  
  341.     // Loop forever
  342.     for (;;) {
  343.         std::string input;
  344.         std::cin >> input;
  345.  
  346.         switch (input[0]) {
  347.         case 'k':
  348.         case 'K':
  349.             std::cout << "Setting key!\n";
  350.             break;
  351.  
  352.         case 'e':
  353.         case 'E':
  354.             std::cout << "Encrypting message";
  355.             break;
  356.  
  357.         case 'd':
  358.         case 'D':
  359.             std::cout << "Decrypting message";
  360.             break;
  361.  
  362.         case 'c':
  363.         case 'C':
  364.             if (input[1] == 'i') {
  365.                 std::cout << "Converting string to integers...\n";
  366.             }
  367.             else if (input[1] == 's') {
  368.                 std::cout << "Converting integers to stirng...\n";
  369.             }
  370.             else {
  371.                 std::cout << "Error, not recieved any valid commands\n";
  372.             }
  373.  
  374.             break;
  375.  
  376.         case 'l':
  377.         case 'L':
  378.  
  379.             for (int i = 1; i < intToChar.size(); i++) {
  380.                 double mod = (double)(intToChar.size() + 1) / (double)i;
  381.                 double intPart = 0;
  382.                 double fracpart = modf(mod, &intPart);
  383.  
  384.                 if (fracpart == 0) {
  385.                     std::cout << "Mod " << mod << "\n";
  386.                 }
  387.             }
  388.  
  389.             break;
  390.  
  391.         default:
  392.             std::cout << "Error, not recieved any valid commands\n";
  393.             break;
  394.  
  395.  
  396.         }
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.     }
  404.  
  405.     std::vector<int> test = strToInts("Test");
  406.  
  407.     std::string testtwo = intsToStr(test);
  408.  
  409.     int x = 5;
  410.  
  411. }
Add Comment
Please, Sign In to add comment