Guest User

Untitled

a guest
Jan 15th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include<iostream>
  2. #include<Windows.h>
  3. #include<fstream>
  4. #include<string>
  5. #include<time.h>
  6.  
  7. using namespace std;
  8.  
  9. char litere[26];
  10. int ocupat[26], auxi;
  11. double frecventa[26],frecventa_n[26];
  12.  
  13.     HANDLE hConsole;
  14.  
  15. string plain_t,crypto_t;
  16.  
  17. string read_from_file()
  18. {
  19.     ifstream f("textdecript.txt");
  20.     if (!f.is_open())
  21.     {
  22.         return "EROARE! Fisierul nu exista";
  23.     }
  24.     string text_original, aux;
  25.     while (!f.eof())
  26.     {
  27.         f >> aux;
  28.         text_original += aux;
  29.     }
  30.     f.close();
  31.     return text_original;
  32. }
  33.  
  34. string convert_to_plain(string text_original)
  35. {
  36.     string plaintext;
  37.  
  38.     for (unsigned temp = 0; temp < text_original.size(); temp++)
  39.         if(text_original[temp] >= 65 && text_original[temp] <= 90)
  40.             plaintext += text_original[temp];
  41.         else
  42.             if(text_original[temp] >= 97 && text_original[temp] <= 122)
  43.                 plaintext += (text_original[temp] - 32);
  44.  
  45.     return plaintext;
  46. }
  47.  
  48. int esteocupat(int pozitie)
  49. {
  50.     if( ocupat[pozitie] == 0 )
  51.     {ocupat[pozitie] = 1;
  52.     return 1;}
  53.     else return 0;
  54. }
  55.  
  56. void gener()
  57. {
  58.     srand(unsigned int (time(NULL)));
  59.  
  60.     int salt = 26;
  61.     int i = 0;
  62.  
  63.     while(salt > 0)
  64.     {
  65.         auxi = rand()%26;
  66.         if( esteocupat(auxi) == 1)
  67.         {
  68.             litere[i] = auxi + 65;
  69.             i++;
  70.             salt--;
  71.         }
  72.     }
  73. }
  74.  
  75. string convert(string plaintext)
  76. {
  77.     string converted;
  78.  
  79.     for (unsigned temp = 0; temp < plaintext.size(); temp++)
  80.         converted += litere[((int) plaintext[temp]) - 65];
  81.  
  82.     return converted;
  83. }
  84.  
  85. void frecv(string cryptotext)
  86. {
  87.     for (unsigned temp = 0; temp < cryptotext.size(); temp++)
  88.         frecventa[cryptotext[temp]-65] += 1;
  89. }
  90.  
  91. void frecv_n(string plaintext)
  92. {
  93.     for (unsigned temp = 0; temp < plaintext.size(); temp++)
  94.         frecventa_n[plaintext[temp]-65] += 1;
  95. }
  96.  
  97. void afish(string text_a)
  98. {
  99.     for (unsigned temp = 0; temp < text_a.size(); temp++)
  100.         if(text_a[temp] == 'E')
  101.         {
  102.             SetConsoleTextAttribute(hConsole, 9);
  103.             cout<<text_a[temp];
  104.         }
  105.  
  106.         else
  107.  
  108.         {
  109.             SetConsoleTextAttribute(hConsole, 7);
  110.             cout<<text_a[temp];
  111.         }
  112. }
  113.  
  114.  
  115.  
  116. void main()
  117. {
  118.     hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  119.  
  120.     gener();
  121.  
  122.     for(int j=0;j<26;j++)
  123.         cout<<"__"<<char(65+j)<<"__ : __"<<litere[j]<<"__"<<endl;
  124.  
  125.     plain_t = convert_to_plain(read_from_file());
  126.     crypto_t = convert(plain_t);
  127.  
  128.     cout<<endl<<"================================================="<<endl<<endl<<"PLAIN TEXT:"<<endl<<endl;
  129.  
  130.     cout<<plain_t<<endl;
  131.  
  132.     cout<<endl<<"================================================="<<endl<<endl<<"CRYPT TEXT:"<<endl<<endl;
  133.  
  134.     afish(crypto_t);
  135.  
  136.     SetConsoleTextAttribute(hConsole, 7);
  137.    
  138.     frecv(crypto_t);
  139.     frecv_n(plain_t);
  140.  
  141.     for(int j=0;j<26;j++)
  142.         cout<<"__"<<char(65+j)<<"__ : __"<<frecventa_n[j]/plain_t.size()*100<<"__][ __"<<frecventa[j]/crypto_t.size()*100<<"__"<<endl;
  143.  
  144.     system("pause");
  145.  
  146. }
Add Comment
Please, Sign In to add comment