Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Affine Key Finder and Decrypter
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 #include <iostream> #include <vector> using std::cout; using std::cin; using std::endl; using std::vector; /* The purpose for this struct is to hold my possible alpha and beta values that the program found to fit the conversions. The struct is stored into a vector that holds all possible values. */ struct AB { int alpha; int beta; }; /* My main function that computes all 312 conversions from plaintext to ciphertext. Using the plain -> cipher conversion from HW1.6, the program will find the possible combinations of alpha and beta values from samples of that conversion. */ void main() { // I begin by storing all possible values of alpha and beta into two // separate arrays. I also initialize my storage containers. int alphArray[12] = {1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25}; int betaArray[26]; vector<AB> possibleAB; AB temp; // I use a loop to store the 26 values of beta instead of doing the same // process as the alphArray. for (int i = 1; i <= 26; i++) { betaArray[i-1] = i; } // This nested for loop is the main component that finds the possible // conversions using two arbitrarily chosen conversions from HW1.6 as // a key. In this case, I took the conversions R -> D (ie 17 -> 3) and // P -> Z (ie 15 ->25). for (int i = 0; i < 12; i++) { for (int j = 0; j < 26; j++) { if ( ((alphArray[i]*17) + betaArray[j]) % 26 == 3 && ((alphArray[i]*15) + betaArray[j]) % 26 == 25) { temp.alpha = alphArray[i]; temp.beta = betaArray[j]; possibleAB.push_back(temp); } } } // Luckily from these two conversions, there was only one outcome. If // there had been more, I would have added more conversions from the // example to weed out more possibilities. // This loop is just to see what were the values that were found to // satisfy the conversion. cout << "Possible combinations: " << possibleAB.size() << endl; for (int i = 0; i < possibleAB.size(); i++) { cout << "My alpha value: " << possibleAB[i].alpha << endl; cout << "My beta value: " << possibleAB[i].beta << endl << endl; } if (possibleAB.size() == 1) { int testD; int yDe; cin >> testD; while ( !cin.fail() ) { if (possibleAB[1].alpha == 1) { yDe = (1*(testD - possibleAB[1].beta)) % 26; } else if (possibleAB[1].alpha == 3) { yDe = (9*(testD - possibleAB[1].beta)) % 26; } else if (possibleAB[1].alpha == 5) { yDe = (21*(testD - possibleAB[1].beta)) % 26; } else if (possibleAB[1].alpha == 7) { yDe = (15*(testD - possibleAB[1].beta)) % 26; } else if (possibleAB[1].alpha == 9) { yDe = (3*(testD - possibleAB[1].beta)) % 26; } else if (possibleAB[1].alpha == 11) { yDe = (19*(testD - possibleAB[1].beta)) % 26; } if ( testD == 0) { cout << "The letter: A" << endl; } else if ( testD == 1) { cout << "The letter: B" << endl; } else if (testD == 2) { cout << "The letter: C" << endl; } else if (testD == 3) { cout << "The letter: D" << endl; } else if (testD == 4) { cout << "The letter: E" << endl; } else if (testD == 5) { cout << "The letter: F" << endl; } else if (testD == 6) { cout << "The letter: G" << endl; } else if (testD == 7) { cout << "The letter: H" << endl; } else if (testD == 8) { cout << "The letter: I" << endl; } else if (testD == 9) { cout << "The letter: J" << endl; } else if (testD == 10) { cout << "The letter: K" << endl; } else if (testD == 11) { cout << "The letter: L" << endl; } else if (testD == 12) { cout << "The letter: M" << endl; } else if (testD == 13) { cout << "The letter: N" << endl; } else if (testD == 14) { cout << "The letter: O" << endl; } else if (testD == 15) { cout << "The letter: P" << endl; } else if (testD == 16) { cout << "The letter: Q" << endl; } else if (testD == 17) { cout << "The letter: R" << endl; } else if (testD == 18) { cout << "The letter: S" << endl; } else if (testD == 19) { cout << "The letter: T" << endl; } else if (testD == 20) { cout << "The letter: U" << endl; } else if (testD == 21) { cout << "The letter: V" << endl; } else if (testD == 22) { cout << "The letter: W" << endl; } else if (testD == 23) { cout << "The letter: X" << endl; } else if (testD == 24) { cout << "The letter: Y" << endl; } else if (testD == 25) { cout << "The letter: Z" << endl; } cout << "Decrypts to: "; if ( yDe == 0) { cout << "A" << endl; } else if ( yDe == 1) { cout << "B" << endl; } else if (yDe == 2) { cout << "C" << endl; } else if (yDe == 3) { cout << "D" << endl; } else if (yDe == 4) { cout << "E" << endl; } else if (yDe == 5) { cout << "F" << endl; } else if (yDe == 6) { cout << "G" << endl; } else if (yDe == 7) { cout << "H" << endl; } else if (yDe == 8) { cout << "I" << endl; } else if (yDe == 9) { cout << "J" << endl; } else if (yDe == 10) { cout << "K" << endl; } else if (yDe == 11) { cout << "L" << endl; } else if (yDe == 12) { cout << "M" << endl; } else if (yDe == 13) { cout << "N" << endl; } else if (yDe == 14) { cout << "O" << endl; } else if (yDe == 15) { cout << "P" << endl; } else if (yDe == 16) { cout << "Q" << endl; } else if (yDe == 17) { cout << "R" << endl; } else if (yDe == 18) { cout << "S" << endl; } else if (yDe == 19) { cout << "T" << endl; } else if (yDe == 20) { cout << "U" << endl; } else if (yDe == 21) { cout << "V" << endl; } else if (yDe == 22) { cout << "W" << endl; } else if (yDe == 23) { cout << "X" << endl; } else if (yDe == 24) { cout << "Y" << endl; } else if (yDe == 25) { cout << "Z" << endl; } cin >> testD; } } }
run
|
edit
|
history
|
help
0
Zero length array as a class member
Increment pointer to struct
Constant table but dynamic initialization at runtime
лаб1
asock
MSVC alias template
#21.2
SO typeindex pretty_name
MSVC14 <experimental/generator> header
Hangman