Guest User

Untitled

a guest
Dec 16th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cassert>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7. const unsigned int R = 15; //random nummer
  8.  
  9.  
  10. void encryption(char& a, unsigned int ri)
  11. {
  12.  //precondtition
  13. assert(true);
  14. //postcondition char a is encryptided
  15. if (a >= 32)
  16. a=(a-32+(ri%(128-32)))%(128-32)+32;
  17. else if (a == 10)
  18. a='\n';
  19. else
  20. a=a;
  21.  
  22.  
  23. }
  24.  
  25. void dencryption(char& a, unsigned int ri)
  26. {
  27.  //precondtition
  28. assert(true);
  29. //postcondition char a is encryptided
  30. if (a >= 32)
  31. a= (a - (ri % (128 - 32)) + 64) % (128 - 32) + 32;
  32. else if (a == 10)
  33. a='\n';
  34. else
  35. a=a;
  36. }
  37.  
  38.  
  39. int main()
  40. {
  41.  srand(R);
  42.  cout <<"to encryp the text press number" <<endl;
  43.  int number;
  44.  cin >> number;
  45.  ifstream text("test.txt");
  46.  ofstream code("CodeTest.txt");
  47.     if (text)
  48.     {
  49.         while(text)
  50.         {
  51.             char a;
  52.             text.get(a);
  53.             encryption(a,rand());
  54.             cout << a ;
  55.             code.put(a);
  56.         }
  57.     }
  58.     else
  59.         cout<<"Text could not be opend/n" <<endl;
  60.  text.close();
  61.  code.close();
  62.  
  63.  cout<<"To decode Text press number\n";
  64.  cin>>number;
  65.  ifstream decode("CodeTest.txt");
  66.  ofstream newcode("NewTest.txt");
  67.  if (decode)
  68.     {
  69.         while(decode)
  70.         {
  71.             char a;
  72.             decode.get(a);
  73.             dencryption(a,rand());
  74.             cout << a ;
  75.             newcode.put(a);                     //put decoded character in the textfile
  76.         }
  77.     }
  78.     else
  79.         cout<<"Text could not be opend" <<endl;  //there is no text that can be decoded
  80.  decode.close();
  81.  newcode.close();
  82. }
Add Comment
Please, Sign In to add comment