Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <regex>
- #include <fstream>
- using namespace std;
- void Szyfrowanie(){
- string tekst;
- cout<<"Podaj tekst: ";
- cin>>tekst;
- //usuwanie białych znaków z wiadomości
- //tekst = regex_replace(tekst, regex("\S+"), ""); //regex zajmuje sie wyrazeniamy regularnymi
- cout<<"Podaj klucz: ";
- int klucz;
- cin>>klucz;
- char plotek[klucz][tekst.length()];
- //STWORZENIE PUSTEJ TABLICY
- for(int i=0; i<klucz; i++){
- for(int j=0; j<tekst.length(); j++) {
- plotek[i][j] = '.';
- }
- }
- //umieszczanie liter jedna po druegiej w macierzy w zig-zag
- int row = 0;
- int check = 0;
- for(int i=0; i<tekst.length(); i++){
- if(check==0){
- plotek[row][i]=tekst[i];
- row++;
- if(row==klucz){
- check = 1;
- row--;
- }
- }
- else if(check==1){
- row--;
- plotek[row][i]=tekst[i];
- if(row==0){
- check=0;
- row=1;
- }
- }
- }
- //tworzenie zaszyfrowanego tekstu
- string Zaszyfrowany = "";
- for(int i=0; i<klucz; i++){
- for(int j=0; j<tekst.length(); j++){
- cout << plotek[i][j];
- Zaszyfrowany += plotek[i][j];
- }
- cout<<endl;
- }
- //usunięcie kropek z zaszyfrowanego tekstu
- Zaszyfrowany = regex_replace(Zaszyfrowany, regex("\\."), ""); //DZIĘKI TEJ LINIJCE USUWAMY KROPKI
- cout <<"Zaszyfrowany tekst: " <<endl <<Zaszyfrowany;
- //ZAPIS ZASZYFROWANEGO TEKSTU DO PLIKU
- fstream plik;
- plik.open("zaszyfrowany.txt",ios::out);
- plik<<Zaszyfrowany<<endl;
- plik.close();
- plik.clear();
- }
- int main()
- {
- cout<<"1. Szyfruj "<<endl;
- cout<<"2. Deszyfruj "<<endl;
- int wybor;
- cin>>wybor;
- if(wybor==1)
- {
- cout<<"Szyfrowanie "<<endl;
- Szyfrowanie();
- }
- /* else if(wybor==2)
- {
- cout<<"Deszyfrowanie "<<endl;
- Deszyfrowanie();
- }*/
- else
- {
- cout<<"Zly wybor. Sprobuj jeszcze raz. "<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement