Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <random>
  4. #include <boost/multiprecision/cpp_int.hpp>
  5.  
  6. using namespace std;
  7.  
  8. #define mylongtype boost::multiprecision::cpp_int //long long //boost::multiprecision::cpp_int
  9. #define str wstring
  10.  
  11. vector<bool> toBinVec(mylongtype number)
  12. {
  13. vector<bool> numBin;
  14.  
  15. bool modulo = 0;
  16. mylongtype n2 = number;
  17.  
  18. if (number == 0)
  19. {
  20. numBin.push_back(0);
  21. } else {
  22. while (true)
  23. {
  24. //modulo = n2 % 2;
  25. if (n2 % 2 == 1)
  26. modulo = 1;
  27. else
  28. modulo = 0;
  29.  
  30. n2 = (n2 - modulo) / 2;
  31. numBin.insert(numBin.cbegin(), modulo);
  32.  
  33. if (n2 == 0 && modulo == 1)
  34. break;
  35. }
  36. }
  37.  
  38. return numBin;
  39. }
  40.  
  41. mylongtype fastpowmod(mylongtype base_, mylongtype pow_, mylongtype mod_)
  42. {
  43. mylongtype result = 1;
  44. vector<bool> binpow = toBinVec(pow_);
  45.  
  46. for (vector<bool>::iterator it = binpow.begin(); it != binpow.end(); it++)
  47. {
  48. result = result * result;
  49.  
  50. if (*it == 1)
  51. result *= base_;
  52.  
  53. result %= mod_;
  54. }
  55.  
  56. return result;
  57. }
  58.  
  59. mylongtype euclideanAlg(mylongtype a, mylongtype b)
  60. {
  61. mylongtype min = std::min(a, b);
  62. mylongtype max = std::max(a, b);
  63.  
  64. mylongtype modulo = 1;
  65. while (modulo != 0)
  66. {
  67. modulo = max % min;
  68. //std::cout << max << "/" << min << " = " << max/min << " (modulo " << modulo << ")\n";
  69. max = min;
  70. min = modulo;
  71. }
  72.  
  73. return max;
  74. }
  75.  
  76. const wstring alphabet = L"абвгдеёжзийклмнопрстуфхцчшщъыьэюя";
  77.  
  78. string codeAlphabet(wchar_t letter)
  79. {
  80. for (unsigned int i = 1; i <= alphabet.length(); i++)
  81. {
  82. if (letter == alphabet[i-1])
  83. {
  84. string ret;
  85. if (i < 10)
  86. {
  87. ret = std::to_string(0);
  88. }
  89.  
  90. ret.append(to_string(i));
  91.  
  92. return ret;
  93. }
  94. }
  95.  
  96. return 0;
  97. }
  98.  
  99. std::wstring StringToWstring(const std::string & source)
  100. {
  101. std::wstring target(source.size()+1, L' ');
  102. std::size_t newLength=std::mbstowcs(&target[0], source.c_str(), target.size());
  103. target.resize(newLength);
  104. return target;
  105. }
  106.  
  107. string codeText(string text)
  108. {
  109. string result;
  110. wstring text2 = StringToWstring(text);
  111. for (uint i = 0; i < text.length(); i++)
  112. {
  113. string code = codeAlphabet(text2[i]);
  114.  
  115. if (i == 0 && code[0] == '0') //avoid leading zero
  116. {
  117. result += code[1];
  118. } else {
  119. result.append(code);
  120. }
  121.  
  122. cout << i << " " << text[i] << " " << code << " | " << result << endl;
  123. }
  124.  
  125. return result;
  126. }
  127.  
  128. void eulerAlgMatrix(mylongtype a, mylongtype b, mylongtype &x, mylongtype &y)
  129. {
  130. mylongtype q;
  131. mylongtype r;
  132.  
  133. mylongtype e11 = 1, e12 = 0,
  134. e21 = 0, e22 = 1;
  135. while (true)
  136. {
  137. q = a / b;
  138. r = a % b;
  139.  
  140. // wcout << "q=" << q << " , r=" << r << '\n';
  141.  
  142. if (r == 0)
  143. {
  144. x = e12;
  145. y = e22;
  146. break;
  147. } else {
  148. mylongtype te12 = e11 * 1 + e12 * -q;
  149. mylongtype te22 = e21 * 1 + e22 * -q;
  150.  
  151. e11 = e12; e12 = te12;
  152. e21 = e22; e22 = te22;
  153.  
  154. a = b;
  155. b = r;
  156.  
  157. // wcout << "E:\n";
  158. // wcout << e11 << " " << e12 << "\n";
  159. // wcout << e21 << " " << e22 << "\n";
  160. }
  161. }
  162. }
  163.  
  164. int main()
  165. {
  166. setlocale(LC_CTYPE, "");
  167.  
  168. string text = "динозавр";
  169. mylongtype p = 77733343;
  170. mylongtype q = 98764579;
  171.  
  172.  
  173.  
  174. mylongtype m = p * q;
  175. mylongtype phi_m = (p - 1) * (q - 1);
  176.  
  177. //code source text
  178. mylongtype num_text = stoll(codeText(text)) % m;
  179.  
  180. cout << text << " ---> " << num_text << "\n";
  181.  
  182. //choose encrypt key
  183. //random_device rd;
  184. //mt19937 generator(rd());
  185. mt19937 generator;
  186. uniform_int_distribution<int> distribution(pow(10, 3), pow(10, 4));
  187.  
  188. int enc_key = distribution(generator);
  189. while (euclideanAlg(enc_key, phi_m) != 1)
  190. enc_key = distribution(generator);
  191.  
  192. cout << "e=" << enc_key << ", (e, phi(m))=" << euclideanAlg(enc_key, phi_m) << "\n";
  193.  
  194. mylongtype enc_num_text = fastpowmod(num_text, enc_key, m);
  195. cout << "Encrypted message: " << enc_num_text << "\n";
  196.  
  197.  
  198. mylongtype a = 0; mylongtype b = 0;
  199. eulerAlgMatrix(enc_key, phi_m, a, b);
  200. if (a < 0)
  201. a += m;
  202.  
  203. mylongtype dec_key = a;
  204.  
  205. cout << "d=" << dec_key << "\n";
  206. cout << "e * d = " << enc_key * dec_key % m << " mod m\n";
  207.  
  208.  
  209. mylongtype dec_enc_num_text = fastpowmod(enc_num_text, dec_key, m);
  210. cout << dec_enc_num_text << "\n";
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement