Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <stdint.h>
- #include <string>
- #include <vector>
- std::map<int, char> intToChar = //67 is prime
- {
- {0, 'A'},
- {1, 'B'},
- {2, 'C'},
- {3, 'D'},
- {4, 'E'},
- {5, 'F'},
- {6, 'G'},
- {7, 'H'},
- {8, 'I'},
- {9, 'J'},
- {10, 'K'},
- {11, 'L'},
- {12, 'M'},
- {13, 'N'},
- {14, 'O'},
- {15, 'P'},
- {16, 'Q'},
- {17, 'R'},
- {18, 'S'},
- {19, 'T'},
- {20, 'U'},
- {21, 'V'},
- {22, 'W'},
- {23, 'X'},
- {24, 'Y'},
- {25, 'Z'},
- {26, 'a'},
- {27, 'b'},
- {28, 'c'},
- {29, 'd'},
- {30, 'e'},
- {31, 'f'},
- {32, 'g'},
- {33, 'h'},
- {34, 'i'},
- {35, 'j'},
- {36, 'k'},
- {37, 'l'},
- {38, 'm'},
- {39, 'n'},
- {40, 'o'},
- {41, 'p'},
- {42, 'q'},
- {43, 'r'},
- {44, 's'},
- {45, 't'},
- {46, 'u'},
- {47, 'v'},
- {48, 'w'},
- {49, 'x'},
- {50, 'y'},
- {51, 'z'},
- {52, ' '},
- {53, '.'},
- {54, ','},
- {55, '?'},
- {56, '!'},
- {57, ':'},
- {58, ';'},
- {59, '@'},
- {60, '$'},
- {61, '%'},
- {62, '/'},
- {63, '+'},
- {64, '='},
- {65, '('},
- {66, ')'},
- };
- std::map<char, int> charToInt = //67 is prime
- {
- {'A', 0},
- {'B', 1},
- {'C', 2},
- {'D', 3},
- {'E', 4},
- {'F', 5},
- {'G', 6},
- {'H', 7},
- {'I', 8},
- {'J', 9},
- {'K', 10},
- {'L', 11},
- {'M', 12},
- {'N', 13},
- {'O', 14},
- {'P', 15},
- {'Q', 16},
- {'R', 17},
- {'S', 18},
- {'T', 19},
- {'U', 20},
- {'V', 21},
- {'W', 22},
- {'X', 23},
- {'Y', 24},
- {'Z', 25},
- {'a', 26},
- {'b', 27},
- {'c', 28},
- {'d', 29},
- {'e', 30},
- {'f', 31},
- {'g', 32},
- {'h', 33},
- {'i', 34},
- {'j', 35},
- {'k', 36},
- {'l', 37},
- {'m', 38},
- {'n', 39},
- {'o', 40},
- {'p', 41},
- {'q', 42},
- {'r', 43},
- {'s', 44},
- {'t', 45},
- {'u', 46},
- {'v', 47},
- {'w', 48},
- {'x', 49},
- {'y', 50},
- {'z', 51},
- {' ', 52},
- {'.', 53},
- {',', 54},
- {'?', 55},
- {'!', 56},
- {':', 57},
- {';', 58},
- {'@', 59},
- {'$', 60},
- {'%', 61},
- {'/', 62},
- {'+', 63},
- {'=', 63},
- {'(', 63},
- {')', 63},
- };
- struct KeyMatrix {
- int a, b, c;
- int d, e, f;
- int g, h, i;
- int det = 0;
- };
- struct MessageMatrix {
- int a;
- int b;
- int c;
- };
- // % operator is remainder operator, not modulous.
- int mathMod(int a) {
- // Number of characters in charset
- int numChar = intToChar.size();
- if (a >= 0) {
- return a % numChar;
- }
- else {
- return numChar - (-(a%numChar));
- }
- }
- MessageMatrix EncryptMessage(KeyMatrix key, MessageMatrix msg){
- MessageMatrix enc;
- enc.a = (key.a * msg.a) + (key.b * msg.b) + (key.c * msg.c);
- enc.b = (key.d * msg.a) + (key.e * msg.b) + (key.f * msg.c);
- enc.c = (key.g * msg.a) + (key.h * msg.b) + (key.i * msg.c);
- enc.a = mathMod(enc.a);
- enc.b = mathMod(enc.b);
- enc.c = mathMod(enc.c);
- return enc;
- }
- int getInverseModulus(int det) {
- double mod = (double)(intToChar.size() + 1)/(double)det;
- double intPart = 0;
- double fracpart = modf(mod, &intPart);
- if (fracpart != 0) {
- std::cout << "Error! det is does not work!";
- std::getchar();
- throw std::exception("Error! Det!");
- }
- return mod;
- }
- MessageMatrix DecryptMessage(KeyMatrix ikey, MessageMatrix enc) {
- MessageMatrix dec;
- //constant
- int c = getInverseModulus(ikey.det);
- dec.a = c * ((ikey.a * enc.a) + (ikey.b * enc.b) + (ikey.c * enc.c));
- dec.b = c * ((ikey.d * enc.a) + (ikey.e * enc.b) + (ikey.f * enc.c));
- dec.c = c * ((ikey.g * enc.a) + (ikey.h * enc.b) + (ikey.i * enc.c));
- dec.a = mathMod(dec.a);
- dec.b = mathMod(dec.b);
- dec.c = mathMod(dec.c);
- return dec;
- }
- // https://en.wikipedia.org/wiki/Invertible_matrix#Inversion_of_3_%C3%97_3_matrices
- KeyMatrix invertKey(KeyMatrix mat) {
- //[A B C]
- //[D E F]
- //[H I J]
- //[A D G]
- //[B E H]
- //[C F I]
- KeyMatrix imat;
- imat.a = (mat.e * mat.i - mat.f * mat.h); // A = (ei-fh)
- imat.b = -(mat.b * mat.i - mat.c * mat.h); // D = -(bi-ch)
- imat.c = (mat.b * mat.f - mat.c * mat.e); // G = (bf-ce)
- imat.d = -(mat.d * mat.i - mat.f * mat.g); // B = -(di -fg)
- imat.e = (mat.a * mat.i - mat.c * mat.g); // E = (ai-cg)
- imat.f = -(mat.a * mat.f - mat.c * mat.d); // H = -(af-cd)
- imat.g = (mat.d * mat.h - mat.e * mat.g); // C = (dh-eg)
- imat.h = -(mat.a * mat.h - mat.b * mat.g); // F = -(ah-bg)
- imat.i = (mat.a * mat.e - mat.b * mat.d); // I = (ae-bd)
- //aA + bD + cG
- //(a*(ei-fh)) + (b(-(di-fg))) + (c(dh-eg)) = 34
- //(aei-afh -bdi+bfg + cdg-ceg)
- imat.det = (mat.a * imat.a) + (mat.b * imat.d) + (mat.c * imat.g); // det = aA + bB + cC but we rotated the matrix
- if (imat.det == 0) {
- std::cout << "Error! det is 0! This key will not work!\n";
- std::getchar();
- throw std::exception("Error! Det!");
- }
- return imat;
- }
- std::vector<int> strToInts(std::string text) {
- std::vector<int> ret = {};
- for (int i = 0; i < text.length(); i++) {
- ret.push_back(charToInt.at(text[i]));
- }
- return ret;
- }
- std::string intsToStr(std::vector<int> numbs) {
- std::string ret = "";
- for (int i = 0; i < numbs.size(); i++) {
- ret += intToChar.at(numbs[i]);
- }
- return ret;
- }
- int main()
- {
- std::cout << "Matrix Encoder & Decoder!\n";
- std::cout << "k: Set the key (3x3 matric eg) 2 4 8 2 1 3 5 8 4\n";
- std::cout << "e: Encrypt message\n";
- std::cout << "d: Decrypt message\n";
- std::cout << "ci: Convert message to array of ints\n";
- std::cout << "cs: Convert array of ints to string\n";
- std::cout << "l: Mod list\n";
- KeyMatrix key;
- key.a = 2;
- key.b = 3;
- key.c = 5;
- key.d = 14;
- key.e = 8;
- key.f = 12;
- key.g = 7;
- key.h = 7;
- key.i = 10;
- MessageMatrix msg;
- msg.a = charToInt.at('E');
- msg.b = charToInt.at('G');
- msg.c = charToInt.at('G');
- MessageMatrix enc = EncryptMessage(key, msg);
- MessageMatrix dec = DecryptMessage(invertKey(key), enc);
- std::vector<int> DecodedInts;
- DecodedInts.push_back(dec.a);
- DecodedInts.push_back(dec.b);
- DecodedInts.push_back(dec.c);
- std::string DecodedMsg = intsToStr(DecodedInts);
- //KeyMatrix invKey = invertKey(key);
- // Loop forever
- for (;;) {
- std::string input;
- std::cin >> input;
- switch (input[0]) {
- case 'k':
- case 'K':
- std::cout << "Setting key!\n";
- break;
- case 'e':
- case 'E':
- std::cout << "Encrypting message";
- break;
- case 'd':
- case 'D':
- std::cout << "Decrypting message";
- break;
- case 'c':
- case 'C':
- if (input[1] == 'i') {
- std::cout << "Converting string to integers...\n";
- }
- else if (input[1] == 's') {
- std::cout << "Converting integers to stirng...\n";
- }
- else {
- std::cout << "Error, not recieved any valid commands\n";
- }
- break;
- case 'l':
- case 'L':
- for (int i = 1; i < intToChar.size(); i++) {
- double mod = (double)(intToChar.size() + 1) / (double)i;
- double intPart = 0;
- double fracpart = modf(mod, &intPart);
- if (fracpart == 0) {
- std::cout << "Mod " << mod << "\n";
- }
- }
- break;
- default:
- std::cout << "Error, not recieved any valid commands\n";
- break;
- }
- }
- std::vector<int> test = strToInts("Test");
- std::string testtwo = intsToStr(test);
- int x = 5;
- }
Add Comment
Please, Sign In to add comment