Advertisement
BMSTU1LOVE

BMSTU1LOVE

Nov 13th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<cmath>
  4.  
  5. using namespace std;
  6.  
  7. void check()
  8. {
  9.  
  10. }
  11.  
  12. string Text()
  13. {
  14. string text;
  15. cout << "Enter the text: "; getline(cin, text);
  16. return text;
  17. }
  18.  
  19. void Key()
  20. {
  21. int key;
  22. cout << "Enter the key: "; cin >> key;
  23. srand(key);
  24. }
  25.  
  26. void XOR(string text, int gamma, int len)
  27. {
  28. int *faza = new int[len];
  29. int Gamma[2];
  30. int gammatemp = gamma;
  31. if (gamma < 256)
  32. {
  33. Gamma[0] = gamma;
  34. Gamma[1] = 0;
  35. }
  36. else
  37. {
  38. for (int i = 15; i > 7; i--)
  39. {
  40. if (gammatemp >= pow(2, i))
  41. gammatemp -= pow(2, i);
  42. }
  43. Gamma[0] = gammatemp;
  44. Gamma[1] = gamma - gammatemp;
  45. }
  46.  
  47. // cout << gamma << " " << Gamma[0] << " " << Gamma[1];
  48.  
  49. for (int i = 0; i < len; i++)
  50. {
  51. // cout << endl << /*text[i] - '0' << "\t" << */int(text[i]) - 48;
  52. faza[i] = (int(text[i]) - 48) ^ Gamma[i % 2];
  53. // cout << "\t" << faza[i] << char(faza[i]);
  54. }
  55. }
  56.  
  57. void Encrypt(string text, int gamma, int len)
  58. {
  59. if (len % 2 != 0)
  60. text[len] = '0';
  61. /*string faza = */XOR(text, gamma, len);
  62. }
  63.  
  64. void Decrypt(string text, int gamma, int len)
  65. }
  66. W processe
  67. }
  68.  
  69.  
  70. int main()
  71. {
  72. char choice;
  73. cout << "Encrypt(E) or Decrypt(D)? "; cin >> choice;
  74.  
  75. cin.ignore();
  76. string text = Text();
  77. int len = text.length();
  78.  
  79. Key();
  80. int gamma = rand();
  81.  
  82. if (choice == 'E')
  83. {
  84. Encrypt(text, gamma, len);
  85. }
  86. else
  87. {
  88. if (choice == 'D')
  89. Decrypt(text, gamma, len);
  90. else
  91. cout << "Error 1\n";
  92. }
  93.  
  94. system("pause");
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement