fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. string RSA(string buf, string alf, int ed, int n)
  7. {
  8. string TC;
  9. for (int j = 0; j < buf.length(); j++)
  10. {
  11. for (int i = 0; i < alf.length(); i++)
  12. {
  13. if(buf[j] == alf[i])
  14. {
  15. cout << i <<" ";
  16. int En = i;
  17. int step = En;
  18. for (int x=2; x<= ed; x++)
  19. step = (step*En)%n;
  20. En = step;
  21. cout << En << endl;
  22. TC += alf[En];
  23. }
  24. }
  25. }
  26. return TC;
  27. }
  28.  
  29.  
  30.  
  31. int main()
  32. {
  33. int p = 3;
  34. int q = 7, e = 5, d = 17;
  35. int n= p*q;
  36. int fi = (p-1)*(q-1);
  37. string alf = "abcdefghijklmnopqrstuvwxyz ";
  38. string buf;
  39. cout << "Vvodi stroku: ";
  40. getline (cin, buf);
  41. cout << buf << endl;
  42. string TC = RSA (buf, alf, e, n);
  43. cout <<"Encrypt -> "<< TC << endl;
  44. string UnTC = RSA (TC, alf, d, n);
  45. cout <<"Decrypt -> "<< UnTC << endl;
  46.  
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 4524KB
stdin
abc
stdout
Vvodi stroku: abc
0 0
1 1
2 11
Encrypt -> abl
0 0
1 1
11 2
Decrypt -> abc