Advertisement
Garey

Danny Code V2

Mar 11th, 2018
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. string getInput();
  8. int gcd(int a, int b);
  9. void getAB(int & a, int & b);
  10. bool withinLower(char i);
  11. bool withinUpper(char i);
  12. int modInverse(int a);
  13. void encryptText(string & input, int a, int b);
  14. void decryptText(string & input, int a, int b);
  15.  
  16. int main() {
  17.     string input = getInput();
  18.  
  19.     int a, b;
  20.     getAB(a, b);
  21.  
  22.     cout << "\nEncrypted: ";
  23.  
  24.     encryptText(input, a, b);
  25.  
  26.     cout << "\n\nDecrypted: ";
  27.     decryptText(input, a, b);
  28.     cout << "\n";
  29.     return 0;
  30. }
  31.  
  32. string getInput() {
  33.     cout << "\nInput string: ";
  34.     string input;
  35.     getline(cin, input);
  36.     return input;
  37. }
  38.  
  39. int gcd(int a, int b) {
  40.     return b == 0 ? a : gcd(b, a % b);
  41. }
  42.  
  43. void getAB(int & a, int & b) {
  44.     do {
  45.         cout << "\na and b must be coprime\na = ";
  46.         cin >> a;
  47.  
  48.         if (!cin.fail()) {
  49.             cout << "b = ";
  50.             cin >> b;
  51.         }
  52.  
  53.         cin.clear();
  54.         cin.ignore(numeric_limits<streamsize>::max(), '\n');
  55.     } while (cin.fail() || gcd(a, b) != 1);
  56. }
  57.  
  58. bool withinLower(char i) {
  59.     return i >= 'a' && i <= 'z' ? true : false;
  60. }
  61.  
  62. bool withinUpper(char i) {
  63.     return i >= 'A' && i <= 'Z' ? true : false;
  64. }
  65.  
  66. // from https://rosettacode.org/wiki/Modular_inverse#C.2B.2B
  67. int modInverse(int a) {
  68.     int b = 26, t, q, x0 = 0, x1 = 1;
  69.     while (a > 1) {
  70.         q = a / b;
  71.         t = b, b = a % b, a = t;
  72.         t = x0, x0 = x1 - q * x0, x1 = t;
  73.     }
  74.     if (x1 < 0) x1 += 26;
  75.     return x1;
  76. }
  77.  
  78. void encryptText(string & input, int a, int b) {
  79.     for (unsigned int i = 0; i < input.length(); ++i) {
  80.         if (withinLower(input[i])) {
  81.             input[i] = (char)((a * (input[i] - 'a') + b) % 26 + 'a');
  82.         }
  83.         else if (withinUpper(input[i])) {
  84.             input[i] = (char)((a * (input[i] - 'A') + b) % 26 + 'A');
  85.         }
  86.         else {
  87.             input[i] = input[i];
  88.         }
  89.     }
  90.     cout << input;
  91. }
  92.  
  93. void decryptText(string & input, int a, int b) {
  94.     for (unsigned int i = 0; i < input.length(); ++i) {
  95.         if (withinLower(input[i])) {
  96.             input[i] = (char)(modInverse(a) * (26 + input[i] - 'a' - b) % 26 + 'a');
  97.         }
  98.         else if (withinUpper(input[i])) {
  99.             input[i] = (char)(modInverse(a) * (26 + input[i] - 'A' - b) % 26 + 'A');
  100.         }
  101.         else {
  102.             input[i] = input[i];
  103.         }
  104.     }
  105.  
  106.     cout << input;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement