Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- string text;
- int shift;
- void save(string text)
- {
- fstream saveFile;
- string fileName = "a.txt";
- saveFile.open(fileName.c_str(),ios::out);
- saveFile << text;
- saveFile.close();
- }
- string load(string name)
- {
- fstream loadFile;
- string line;
- loadFile.open(name);
- getline(loadFile,line);
- loadFile.close();
- return line;
- }
- string cezar_encrypt( string inp , int shift)
- {
- for(int i = 0; i <= inp.length() ; i++)
- {
- if( inp[i] >= 65 && inp[i] <= 90 - shift || inp[i] >= 97 && inp[i] <= 122 - shift) inp[i] = int(text[i]) + shift;
- else if ( inp[i] >= 91 - shift && inp[i] <= 90 || inp[i] >= 123 - shift && inp[i] <= 122) inp[i] = int(text[i]) - 26 + shift;
- //else cout << int(inp[i]) << endl;
- }
- return inp;
- }
- string cezar_decrypt( string inp , int shift)
- {
- for(int i = 0; i <= inp.length() ; i++)
- {
- if( inp[i] >= 65 && inp[i] <= 90 - shift || inp[i] >= 97 && inp[i] <= 122 - shift) inp[i] = int(text[i]) - shift;
- else if ( inp[i] >= 91 - shift && inp[i] <= 90 || inp[i] >= 123 - shift && inp[i] <= 122) inp[i] = int(text[i]) + 26 - shift;
- //else cout << int(inp[i]) << endl;
- }
- return inp;
- }
- int main()
- {
- string text2 , encrypted_text;
- cout << "Podaj tekst do zaszyfrowania" << endl;
- cin >> text;
- cout << "Podaj przesunięcie" << endl;
- cin >> shift;
- cout << "Wynik:" << endl;
- encrypted_text = cezar_encrypt(text, shift);
- cout << encrypted_text << endl;
- save(encrypted_text);
- //cout << "Podaj klucz do deszyfrowania" << endl;
- //cin >> shift;
- text2 = load("a.txt");
- text = cezar_decrypt(text2 , shift);
- cout << "Wiadomosc po deszyfrowaniu: " << text << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement