Advertisement
NuquernaNarsil

szyfrowanie_kluczem_do_pliku

Nov 4th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1.    
  2.  
  3.     #include <iostream>
  4.     #include <conio.h>
  5.     #include <fstream>
  6.      
  7.     using namespace std;
  8.      
  9.     void usuwanie_powtorzen_kodu(string &klucz)
  10.     {
  11.     string kod2;
  12.     int temp;
  13.      
  14.             for(int i = 0; i < klucz.length(); i++)
  15.             {      
  16.                     if(klucz[i]>=65 && klucz[i]<=90)
  17.                             klucz[i]+=32;  
  18.             }
  19.            
  20.             for(int i = 0; i < klucz.length(); i++)
  21.             {
  22.             temp = 0;
  23.                     for(int j = 0; j < i; j++)
  24.                     {
  25.                             if(klucz[i] == klucz[j])
  26.                             {
  27.                                     temp++;
  28.                             }
  29.                     }      
  30.             if(temp==0)
  31.                     kod2+=klucz[i];
  32.             }
  33.             klucz=kod2;
  34.     }
  35.      
  36.     void wczytaj_teksty(string &klucz, string &tekst)
  37.     {
  38.     fstream tekstj, klu;
  39.     tekstj.open("jawny.txt", ios::in);
  40.    
  41.     if(tekstj.is_open())
  42.     {
  43.      while(tekstj)
  44.      {
  45.       getline(tekstj, tekst);
  46.      }
  47.      tekstj.close();
  48.     }
  49.     klu.open("klucz.txt", ios::in);
  50.  
  51.     if(klu.is_open())
  52.     {
  53.      while(klu)
  54.      {
  55.       getline(klu, klucz);
  56.      }
  57.      tekstj.close();
  58.     }
  59.     }
  60.      
  61.     void tworzenie_alfabetu(string klucz, string &alfabet, string &aj)
  62.     {
  63.     int temp;
  64.             for(int i=0 ; i<klucz.length(); i++)
  65.             {
  66.                     alfabet+=klucz[i];    
  67.             }
  68.            
  69.             for(int i='a'; i<='z'; i++)
  70.             {
  71.             temp=0;
  72.                     for(int j= 0; j<alfabet.length();j++)
  73.                     {
  74.                             if(char(i)==alfabet[j])
  75.                                     temp++;
  76.                     }
  77.                     if(temp==0)
  78.                             alfabet+=char(i);
  79.             }
  80.             for(int i='a';i<='z';i++)
  81.             {
  82.                     aj+=char(i);      
  83.             }  
  84.     }
  85.      
  86.     void szyfrowanie(string klucz,string alfabet,string tekst,string &szyfr)
  87.     {
  88.  
  89.             for(int i = 0; i<tekst.length(); i++)
  90.             {
  91.                     if(tekst[i]!=' ')
  92.                     {
  93.                             if(tekst[i]>=97 && tekst[i]<=122)
  94.                                     szyfr+=alfabet[int(tekst[i])-97];
  95.                             else
  96.                                     szyfr+=char(alfabet[int(tekst[i])-65]-32);
  97.                     }
  98.                     //else
  99.                             //szyfr+=tekst[i];
  100.             }
  101.             cout <<"\nKryptogram podanego tekstu: "<< szyfr;
  102.            
  103.             fstream zap;
  104.             zap.open("kryptogram.txt", ios::out);
  105.             if(zap.is_open())
  106.             {
  107.              zap << szyfr;
  108.              zap.close();
  109.             }            
  110.            
  111.     }
  112.      
  113.     void wyswietl_alfabety(string alfabet, string aj)
  114.     {
  115.     cout <<"\n\n M: ";
  116.             for(int i=0; i<alfabet.length(); i++)
  117.             {
  118.                     cout <<" "<< alfabet[i];
  119.             }
  120.     cout <<"\n D: ";
  121.             for(int i=0; i<alfabet.length(); i++)
  122.             {
  123.                     cout <<" "<< char(int(alfabet[i])-32);
  124.             }
  125.     cout <<"\n A: ";
  126.             for(int i=0; i<alfabet.length(); i++)
  127.             {
  128.                     cout <<" "<< aj[i];
  129.             }
  130.     }
  131.     main()
  132.     {
  133.     string tekst, klucz, alfabet,szyfr,aj;
  134.  
  135.    
  136.     wczytaj_teksty(klucz,tekst);
  137.     usuwanie_powtorzen_kodu(klucz);
  138.     tworzenie_alfabetu(klucz, alfabet, aj);
  139.     szyfrowanie(klucz, alfabet, tekst, szyfr);
  140.     wyswietl_alfabety(alfabet, aj);
  141.      
  142.     getch();
  143.     return 0;
  144.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement